secant method for nonlinear equations?
11 次查看(过去 30 天)
显示 更早的评论
Hi I’m supposed to design programs to find roots of nonlinear equations by the Secant method. I have 3 functions and I should work one code with three anonymous function, one for each function (which you selectively activate or deactivate). My problem is that I don’t know how I’m going to activate or deactivate the different anonymous functions and how my program is going to work for 3 completely different functions (not the same variables). Here is what I have for one of the functions. Also is someone can help and tell me if the program is wrong or has error please. clc,clear,close
%The anonymouse function
f=@(x)x.^5-16*x.^4+95*x.^3-260*x.^2+324*x-144;
%Graph the function
ezplot(f);
grid on
%The two guesses
a=input('Enter initial guess for smaller bracket');
b=input('Enter guess for smaller bracket');
tol=10^-5;
% the created function
secant( f,a,b,tol)
function [ y ] = secant( f,a,b,tol )
flag=1;
fa=feval(f,a);
fb=feval(f,b);
c= b - (fb/((fb-fa)/(b-a)));
fc=feval(f,c);
error= abs((c-b)/c);
disp(' Xn-1 f(Xn-1) Xn f(Xn) Xn+1 f(Xn+1) error');
disp([a fa b fb c fc error]);
while error > tol
a = b;
b = c;
c = b - (fb/((fb-fa)/(b-a)));
error= abs((c-b)/c);
disp([a fa b fb c fc error]);
flag = flag + 1;
if(flag == 100)
break;
end
end
display(['Root is x = ' num2str(c)]);
y = c;
end
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!