Please help me identifying how this person makes his code
1 view (last 30 days)
Show older comments
muhammad hafiz
on 3 Dec 2021
Commented: Walter Roberson
on 4 Dec 2021
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)
Accepted Answer
Walter Roberson
on 3 Dec 2021
funct = @(x) 1-(400/9.81)*(3+x)/((3*x + 0.5*x^2)^3);
secant (funct, 0.5,2.5,0.01)
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 Comments
Walter Roberson
on 4 Dec 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.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!