using the while loop to get the number of divisions

4 views (last 30 days)
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

Answers (1)

Rik
Rik on 29 Apr 2020
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}')

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!