How to store each iteration value and plot it ?

1 回表示 (過去 30 日間)
engineer
engineer 2018 年 8 月 31 日
編集済み: dpb 2018 年 9 月 1 日
Hi everyone I would like to store each L2 values after each iteration and plot it vs iteration number. How can I do it with the attached code?
clc;
clear all;
n=input('Enter the no of iterations : \n');
N=n+1;
a=input('\n Enter lower limit : \n');
b=input('\n\nEnter upper limit : \n');
fold=1;
fnew=1;
func = @(x)(.65-[.75/(1+x^2)]-.65*x*atan(1/x));
for i=1:N
if i==1 || i==2
f(i)=1;
continue;
end
f(i)=fold+fnew;
fold=fnew;
fnew=f(i);
end
L2=(b-a)*f(N-2)/f(N);
j=2;
while j<N
L1=(b-a);
if L2>L1/2
anew=b-L2;
bnew=a+L2;
else
if L2<=L1/2
anew=a+L2;
bnew=b-L2;
end
end
k1=func(anew);
k2=func(bnew);
if k2>k1
b=bnew;
L2=f(N-j)*L1/f(N-j+2);
else
if k2<k1
a=anew;
L2=f(N-j)*L1/f(N-(j-2));
else
if k2==k1
b=bnew;
L2=f(N-j)*[b-a]/f(N-(j-2));
j=j+1;
end
end
end
j=j+1;
end
disp(a);
disp(b);
  1 件のコメント
engineer
engineer 2018 年 8 月 31 日
when I run , it only shows the last iteration result.

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

採用された回答

dpb
dpb 2018 年 8 月 31 日
Preallocate for L2 and keep and increment an index inside the loop and store into an array
L2=zeros(N,1); % preallocate
L2(1)=(b-a)*f(N-2)/f(N); % save initial value
j=2;
while j<N
L1=(b-a);
if L2>L1/2
...
every store is then
L2(j)=...;
Or, change to counted for loop instead of while since there's no conditional exit.
L2(1)=...;
for j=2:N-1
...
L2(j)=...;
...
BTW, Matlab "elseif" is one string, I had to reformat code to try to read it for indention levels being all fouled up where you wrote
...
else if L2<=L1/2
...
Rewrite
if L2>L1/2
anew=b-L2;
bnew=a+L2;
else
if L2<=L1/2
anew=a+L2;
bnew=b-L2;
end
end
as
if L2>L1/2
anew=b-L2;
bnew=a+L2;
elseif L2<=L1/2
anew=a+L2;
bnew=b-L2;
end
which since the 'elseif' is the negation of the 'if' condition, this is really
if L2>L1/2
anew=b-L2;
bnew=a+L2;
else
anew=a+L2;
bnew=b-L2;
end
there's no need for the conditional if on the else clause here.
  4 件のコメント
engineer
engineer 2018 年 9 月 1 日
sure.
if true
clc;
clear all;
n=input('Enter the no of iterations : \n');
N=n+1;
a=input('\n Enter lower limit : \n');
b=input('\n\nEnter upper limit : \n');
fold=1;
fnew=1;
func = @(x)0.3039*exp(-((x-0.1524)/0.3671).^2) + 6.593e+13*exp(-((x+495.5)/86.41).^2) + 0.245*exp(-((x-0.1098)/0.1452).^2) + 0.06194*exp(-((x-0.3992)/0.2167).^2) + 0.09388*exp(-((x--0.3931)/0.5412).^2) + 0.143 *exp(-((x-1.001)/1.158).^2);
for i=1:N
if i==1 || i==2
f(i)=1;
continue;
end
f(i)=fold+fnew;
fold=fnew;
fnew=f(i);
end
L2=(b-a)*f(N-2)/f(N);
L2=zeros(N,1); % preallocate
L2(1)=(b-a)*f(N-2)/f(N); % save initial value
j=2;
while j<N
L1=(b-a);
if L2>L1/2
anew=b-L2;
bnew=a+L2;
else
if L2<=L1/2
anew=a+L2;
bnew=b-L2;
end
end
k1=func(anew);
k2=func(bnew);
if k2>k1
b=bnew;
L2(j)=f(N-j)*L1/f(N-j+2);
else
if k2<k1
a=anew;
L2=f(N-j)*L1/f(N-(j-2));
else
if k2==k1
b=bnew;
L2(j)=f(N-j)*(b-a)/f(N-(j-2));
j=j+1;
end
end
end
j=j+1;
end
disp(a);
disp(b);
end
dpb
dpb 2018 年 9 月 1 日
編集済み: dpb 2018 年 9 月 1 日
  1. Don't need the L2= line before preallocating
  2. You missed at least one path in the loop of the (j) subscript on L2 so that will write the one value into the whole array
Make sure you got each and every "L2=..." line corrected.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by