I need to find the smallest possible root.
조회 수: 5 (최근 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!