using the while loop to get the number of divisions

2 ビュー (過去 30 日間)
WILLBES BANDA
WILLBES BANDA 2020 年 4 月 28 日
回答済み: Rik 2020 年 4 月 29 日
Hi, I am trying to add the while loop to my code such that the code should run while the difference between the present and previous value of Rs is greater than 1e-5 and then record n, the number of times the addition happens and i cant figure out where to add it and how. Below is my code, please help including an alternative way beside the while loop to find n.
f=@(y) 2*pi*(sqrt((y-44.56)/(-0.16))+1)*sqrt(1+((1)/(-0.32*((sqrt((y-44.56)/(-0.16))+1))+0.32))^2);
dt=0.0001;
N=0:dt:40;
Rs=0;
for i=1:numel(N)
Rs=Rs+f(N(i))*dt;
end

回答 (1 件)

Rik
Rik 2020 年 4 月 29 日
The term you are adding never reaches that low threshold. If it would, the code below would do the trick. You can test it by setting the threshold to something above 4.6e-3
threshold=6e-3;
f=@(y) 2*pi*(sqrt((y-44.56)/(-0.16))+1)*sqrt(1+((1)/(-0.32*((sqrt((y-44.56)/(-0.16))+1))+0.32))^2);
dt=0.0001;
N=0:dt:40;
Rs=0;
R_vec=NaN(size(N));
add_vec=NaN(size(N));
for i=1:numel(N)
added_term=f(N(i))*dt;
if abs(added_term)<threshold
number_of_additions=i-1;
break
end
Rs=Rs+added_term;
R_vec(i)=Rs;
add_vec(i)=added_term;
end
figure(1),clf(1)
subplot(1,2,1)
plot(N,R_vec),title('value of Rs')
subplot(1,2,2)
plot(N,add_vec),title('\Delta_{iterations}')

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by