How can I use the subplot command to plot the root estimates vs iterations and the error vs iterations

조회 수: 1 (최근 30일)
this is my code:
Use bisection method to find the root
f = @(x) x.^3 - (9)*x.^2 + 3.8197
xl = -1000
xu = 1000
xm = (xl+xu)/2
error = 20
while error > 0.001
if (f(xm).*f(xl))<0
xu=xm
xm2 = (xl+xu)/2
error = (xm2-xm)/xm2
error = abs(error)
xm = xm2
else
xl=xm
xm2 = (xl+xu)/2
error = (xm2-xm)/xm2
error = abs(error)
xm = xm2
end
xm= (xl+xu)/2
end
the root is -0.6299 with error = 0.0757%
How can I collect the error and root estimate at each run, and use subplot command to plot 1: root estimates vs iterations and 2:error vs iterations\
thank you very much!

답변 (1개)

darova
darova 2021년 4월 4일
Try this
figure
hold on
while %condition
% some code
subplot(2,1,1)
plot(iter,root)
subplot(2,1,2)
plot(iter,error)
iter = iter + 1;
end
  댓글 수: 4
wenchong chen
wenchong chen 2021년 4월 4일
the code runs and two subplot pops out but no plot on them, what wrong with my code?
wenchong chen
wenchong chen 2021년 4월 4일
hold on
f = @(x) x.^3 - (9)*x.^2 + 3.8197
xl = -1000
xu = 1000
iter = 1
xm = (xl+xu)/2
error = 20
while error > 0.001
if (f(xm).*f(xl))<0
xu=xm
xm2 = (xl+xu)/2
error = (xm2-xm)/xm2
error = abs(error)
xm = xm2
else
xl=xm
xm2 = (xl+xu)/2
error = (xm2-xm)/xm2
error = abs(error)
xm = xm2
end
xm= (xl+xu)/2
subplot(2,1,1)
plot(iter,xm)
subplot(2,1,2)
plot(iter,error)
iter = iter + 1;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by