Please help me identifying how this person makes his code
Show older comments
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 Comment
muhammad hafiz
on 3 Dec 2021
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!