I need to find the smallest possible root.
9 views (last 30 days)
Show older comments
I don't understand how to make this work.I need to find the smallest possible root and have an output/printout which needs to include the root x^*, the value f(x^*), the number of iterations performed and whether convergence was reached or the maximum number of iterations was reached before convergence was achieved. With the stopping criterion f(x^*)<10^(-5), maximum iterations set to 20 and start with xL=0 and xR=2.5. This what I have, but it won't run, also I don't know how to do printout.
clear all;
xL=0
xR=2.5
for j=1:20
xM=(xL+xR)/2;
fM=sin(3*xM)-2cos(xM);
fL=sin(3*xL)-2cos(xL);
fR=sin(3*xR)-2cos(xR);
if fM*fL<0
xR=xM
elseif fM*fR<0
xL=xM
end
if abs(fM)<10^(-5)
break
end
end
0 Comments
Answers (1)
Walter Roberson
on 9 Feb 2017
fM=sin(3*xM)-2cos(xM);
MATLAB does not support implicit multiplication. You need a "*" or ".*" between the "2" and the "cos"
3 Comments
Walter Roberson
on 9 Feb 2017
xL=0
xR=2.5
root_found = false;
for j=1:20
fprintf('Iteration #%d\n', j);
xM=(xL+xR)/2;
fM=sin(3*xM)-2*cos(xM);
fL=sin(3*xL)-2*cos(xL);
fR=sin(3*xR)-2*cos(xR);
if fM*fL<0
xR=xM
else
xL=xM
end
if abs(fM)<10^(-5)
root_found = true;
break
end
end
xM
fM
See Also
Categories
Find more on File Operations 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!