false position method to find zeros, not plotting zeros

1 view (last 30 days)
Can someone tell me why this code is not finding/ plotting the roots for my function? I created a plot for function. Then, I wrote the code for false position method to solve for all the zeros of that function (using three x ranges, where I know the three roots are within). Then, I tried to overlay the plot of these zeros onto my old plot.
this is my function file:
function result = lalafunction(x)
result= (2*x.*exp(cos(3*x)).*exp(-x)) + 70 ;
this is my plot file:
plot=figure(2),
x1= -3.5:0.01:-1.5;
for i=1:length(x1)
g(i)=lalafunction(x1(i));
end
plot(x1,g)
this is the file that should plot the zeros. It finds the first two, but not the last one correctly
plot(x1,g)
hold on
xo= [-1.99,-1.90; -2.8,-2.5; -3.3,-3.2];
for ii=1:3
xleft=xo(ii,1);
xright=xo(ii,2);
iteration=1;
while iteration<=100 && abs(xleft-xright)> 10^-3
yleft=lalafunction(xleft);
yright=lalafunction(xright);
xm = xright-((yright*(xright-xleft))/(yright-yleft));
if yleft*lalafunction(xm) < 0
xright=xm;
else
xleft=xm;
end
iteration = iteration +1;
end
xx(ii)= (xleft+xright)/2
yy(ii)= lalafunction(xx(ii))
end
plot (xx,yy,'o')

Answers (0)

Community Treasure Hunt

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

Start Hunting!