I need to find the smallest possible root.

2 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2017-2-9
fM=sin(3*xM)-2cos(xM);
MATLAB does not support implicit multiplication. You need a "*" or ".*" between the "2" and the "cos"
  3 个评论
Walter Roberson
Walter Roberson 2017-2-9
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by