secant method for nonlinear equations?

조회 수: 10 (최근 30일)
Javier Vargas
Javier Vargas 2017년 5월 4일
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개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by