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

2 ビュー (過去 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 Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by