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 CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by