How can I use relatively simple code to make bisections in a function until I find the zero?

2 visualizaciones (últimos 30 días)
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;

Respuestas (1)

Kevin Chng
Kevin Chng el 12 de Oct. de 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.

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by