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
0 件のコメント
回答 (1 件)
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
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 Center および File Exchange で Graphics Object Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!