How to plot a function dependent on no. of iterations, provided it plots till nth iteration where n is the number where the function is converged?

4 ビュー (過去 30 日間)
ISHA ARORA
ISHA ARORA 2022 年 1 月 28 日
編集済み: VBBV 2022 年 1 月 29 日
Hello All,
I am trying to plot a function vs no. of iterations. In the for loop, i have given the convergence condition, but I am unable to plot for f(x) vs no. iterations (till convergence). Please help.
Its of the form:
for m=1:n
pu(m)=(-1)^m)/((2*m)+1))*(exp(-((2*m)+1)))
e = e+pu;
if (abs(pu)/e)<(10^(-6)) %convergence check
break
end
end
hold all
plot(1:n,pu,'linestyle','none','marker','*')

採用された回答

VBBV
VBBV 2022 年 1 月 28 日
編集済み: VBBV 2022 年 1 月 29 日
n= 10;
e = 0;
for m=1:n
pu(m)=(-1)^m/((2*m)+1)*(exp(-((2*m)+1)))
e = e+pu(m);
if (abs(pu(m))/e)>(10^(-6)) %convergence check
break
end
end
pu = -0.0166
pu = 1×2
-0.0166 0.0013
pu = 1×3
-0.0166 0.0013 -0.0001
pu = 1×4
-0.0166 0.0013 -0.0001 0.0000
pu = 1×5
-0.0166 0.0013 -0.0001 0.0000 -0.0000
pu = 1×6
-0.0166 0.0013 -0.0001 0.0000 -0.0000 0.0000
pu = 1×7
-0.0166 0.0013 -0.0001 0.0000 -0.0000 0.0000 -0.0000
pu = 1×8
-0.0166 0.0013 -0.0001 0.0000 -0.0000 0.0000 -0.0000 0.0000
pu = 1×9
-0.0166 0.0013 -0.0001 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000
pu = 1×10
-0.0166 0.0013 -0.0001 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000
hold all
plot(1:n,pu,'linestyle','-','marker','.')
Check the convergence condition
  7 件のコメント
VBBV
VBBV 2022 年 1 月 29 日
u=zeros(n,npoints); % change also This line in beginning
u(m,cidx)=pu(m);
hold all
plot(1:m,u(1:m,cidx),'linestyle','none','marker',mrkr{cidx},'Color',clr{cidx})
You also need to change these two lines inside the for loop. As you can see the u value is dependent on pu which is again varying with each outer iteration of for loop.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by