I'm trying to create a function that finds the root of an equation , error , and number of iterations by (bisection method ) but I'm getting errors that I don't understand

1 view (last 30 days)
So I did my code but errors kept showing and i don't know how to fix the problem , so a little help will be appreciated .
what I want to do is to have two M-files (scripts) one contain the function that calculates the root of an equation X , the error e , and the number of iterations i as a function of the input X0 , X1 and F(equation in term of X ) .the second M-file that i will run gives the input values and call the function of the other M file.
so this my code in the first M-file ( named: men_bisect.m)
function [X,i,e]= mem_bisect(F,Xo,X1)
X=(Xo+X1)/2;
% X=feval(F,X);
if(F(Xo)==0)
Xo=X;
end
if(F(X1)==0)
X1=X;
end
X=(Xo+X1)/2 ;
if(F(X)==0)
end
if(F(Xo)*F(X1)>0)
disp('A solution is not guaranteed because f(Xo) * f(X1)>0')
end
if(F(Xo)*F(X1)<0)
X=(Xo+X1)/2;
i=0;
eps=1*10.^-5 ;
e=abs(F(X));
while(i<1000 && e>eps)
if((F(X)*F(X1))<0)
Xo=X;
else
X1=X;
end
i=i+1;
X=(Xo+X1)/2;
e=abs(F(X));
end
disp('the root is',X,'and the error is ',e,'and the number of iterations is',i);
end
and this is my second M-file ( named : mem.m ) that I will run from:
clear all
clear variables
clc
% The range
Xo=0;
X1=1.5;
% The equation
a=@(X) X;
F=@(X)(sin(2*pi*X)+exp(1.2*X)+X-2.5);
% Calling the function mem_bisect
mem_bisect(F,Xo,X1);
I kept getting these errors :
?? Error using ==> disp
Too many input arguments.
Error in ==> mem_bisect at 40
disp('the root is',X,'and the error is ',e,'and the number of iterations is',i);
Error in ==> men at 11
mem_bisect(F,Xo,X1);
I think the problem is with the function and the order of the code but i don't to how to fix them.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Nov 2015
fprintf('the root is %g and the error is %g and the number of iterations is %d\n', X, e, i);

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!