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

2 Ansichten (letzte 30 Tage)
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!

Antworten (1)

darova
darova am 4 Apr. 2021
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 Kommentare
wenchong chen
wenchong chen am 4 Apr. 2021
the code runs and two subplot pops out but no plot on them, what wrong with my code?
wenchong chen
wenchong chen am 4 Apr. 2021
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by