# using the while loop to get the number of divisions

4 views (last 30 days)
WILLBES BANDA on 28 Apr 2020
Answered: Rik on 29 Apr 2020
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

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));
for i=1:numel(N)
break
end
R_vec(i)=Rs;
end
figure(1),clf(1)
subplot(1,2,1)
plot(N,R_vec),title('value of Rs')
subplot(1,2,2)

### Categories

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

### Community Treasure Hunt

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

Start Hunting!