How can I use relatively simple code to make bisections in a function until I find the zero?
Show older comments
My coding project asks me to find the zero of a function within a certain domain (-10,10) and I have what I believe is most of the function correct, however I get the error: "Not enough inputs to inline function."
XL=-10;XR=10;XM=0;
F = inline('(sqrt(log(x)^2+1))/cosh(x)-(x-(pi/6))+(40/3)*(exp(x))^(-abs(x/5))','XL','XR','num1');
count=1;
j=linspace(-10,10,1000);
while abs(F(XM))>10^(-num1)
if F(XL)*F(XM)<0
XM=XR;
else
XM=XL;
end
end
count=count+1;
Answers (1)
Kevin Chng
on 12 Oct 2018
Hi David Sahed,
There are two ways
1) Use in built MATLAB function
F = inline('(sqrt(log(x)^2+1))/cosh(x)-(x-(pi/6))+(40/3)*(exp(x))^(-abs(x/5))');
fzero(F,5);
But this is not bisection method
2) I have modified your logic in your program
XL=-10;XR=10;XM=0;
F = inline('(sqrt(log(x)^2+1))/cosh(x)-(x-(pi/6))+(40/3)*(exp(x))^(-abs(x/5))');
maxiteration=1000;
Tol = 0.001;
for i=1:1:maxiteration
XM = (XL+XR)/2;
if F(XM) == 0
break;
elseif (XR-XL)<Tol
break;
end
if F(XL)*F(XM)>0
XL=XM;
else
XR=XM;
end
end
display(XM)
Try to understand the logic of bisection once again.
If my answer is working for you, kindly accept it. Thanks.
Categories
Find more on Multirate Signal Processing 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!