Please help me identifying how this person makes his code

1 visualizzazione (ultimi 30 giorni)
i am trying to learn how this person execute his codes but i am new to the MATLAB application. please help in identifying this code
function xs = secant( funct, x0, xi, es )
% Numerically evaluate root of func using Secant Method
% Solve a nonlinear equation using the secant method
% func : a function handle;
% a,b : x-interval between initial and enclosing value;
% es : is the desired error;
% returns xs: which is the final numerical solution of func;
x(1)=x0; % initial guess 1
x(2)=xi; % initial guess 2
f(1)=feval(func,x(1)); % function for initial guess 1
f(2)=feval(func,x(2)); % function for initial guess 2
for i=1:10
x(3)=x(2)-( f(2)*(x(1)-x(2)) )/( f(1)-f(2) ); % Formula for Secant Method
x(1)=x(2); f(1)=feval(func,x(1));
x(2)=x(3); f(2)=feval(func,x(2));
xnv(i)=x(3); % Value of x
fxv(i)=feval(func,xnv(i)); % Value of function x
ea(i)=abs((x(2)-x(1))/x(2)); % Absolute Error
if (ea(i) < es)
break
end % stop iterating if error less than tolerance
end
fprintf('iteration\t|\t\txi\t\t|\tf(xi)\t\t\t|\tea\n');
fprintf('----------------------------------------------------------------------------------------------------\n');
for i=2:length(xnv)
fprintf('%5d\t\t|\t%10.5f\t\t|\t%10.5f\t\t|\t%10.5f\n',i-1,xnv(i),fxv(i),ea(i));
end
fprintf('-----------------------------------------------------------------------------------------------------\n');
xs=xnv(length(xnv));
fprintf('\nfinal solution: \n\tx = %-10.10f\n',xnv(length(xnv)));
end
clear all
funct = @(x) 1-(400/9.81)*(3+x)/((3*x + 0.5*x^2)^3);
secant (funct, 0.5,2.5,0.01)
  1 Commento
muhammad hafiz
muhammad hafiz il 3 Dic 2021
it suppose to generate a table but unfortunately no table presented, i am at my wits end.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 3 Dic 2021
funct = @(x) 1-(400/9.81)*(3+x)/((3*x + 0.5*x^2)^3);
secant (funct, 0.5,2.5,0.01)
iteration | xi | f(xi) | ea ---------------------------------------------------------------------------------------------------- 1 | -0.53791 | 32.66419 | 5.55617 2 | 2.52586 | 0.81952 | 1.21296 3 | 2.60470 | 0.83761 | 0.03027 4 | -1.04453 | 5.59958 | 3.49367 5 | 3.24659 | 0.92468 | 1.32173 6 | 4.09536 | 0.96725 | 0.20725 7 | -15.19096 | 1.00146 | 1.26959 8 | 549.37294 | 1.00000 | 1.02765 9 | 386945.37155 | 1.00000 | 0.99858 ----------------------------------------------------------------------------------------------------- final solution: x = 386945.3715490098
ans = 3.8695e+05
function xs = secant( func, x0, xi, es )
% Numerically evaluate root of func using Secant Method
% Solve a nonlinear equation using the secant method
% func : a function handle;
% a,b : x-interval between initial and enclosing value;
% es : is the desired error;
% returns xs: which is the final numerical solution of func;
x(1)=x0; % initial guess 1
x(2)=xi; % initial guess 2
f(1)=feval(func,x(1)); % function for initial guess 1
f(2)=feval(func,x(2)); % function for initial guess 2
for i=1:10
x(3)=x(2)-( f(2)*(x(1)-x(2)) )/( f(1)-f(2) ); % Formula for Secant Method
x(1)=x(2); f(1)=feval(func,x(1));
x(2)=x(3); f(2)=feval(func,x(2));
xnv(i)=x(3); % Value of x
fxv(i)=feval(func,xnv(i)); % Value of function x
ea(i)=abs((x(2)-x(1))/x(2)); % Absolute Error
if (ea(i) < es)
break
end % stop iterating if error less than tolerance
end
fprintf('iteration\t|\t\txi\t\t|\tf(xi)\t\t\t|\tea\n');
fprintf('----------------------------------------------------------------------------------------------------\n');
for i=2:length(xnv)
fprintf('%5d\t\t|\t%10.5f\t\t|\t%10.5f\t\t|\t%10.5f\n',i-1,xnv(i),fxv(i),ea(i));
end
fprintf('-----------------------------------------------------------------------------------------------------\n');
xs=xnv(length(xnv));
fprintf('\nfinal solution: \n\tx = %-10.10f\n',xnv(length(xnv)));
end
  4 Commenti
muhammad hafiz
muhammad hafiz il 4 Dic 2021
i see thank you for the help anyways i'll tak my time identifying how it works myself. ty
Walter Roberson
Walter Roberson il 4 Dic 2021
We get people posting saying that they had never taken any programming course until they started 4 days before. When you do not guide us about what information you are looking for, I have to assume that you are one of those people, who has no idea how programming works and who has no idea about calculus or approximation theory. I would have to go back to basics such as fundamental principles of arithmetic. This is not a productive use of your time or my time, when you could simply describe what concepts in the program that you are having difficulty with.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by