How to stop the loop when percentage error is reached?
Show older comments
It is known that the values of the following three sums, accurate to 12 decimal places, are:

For each k, the objective of this problem is to determine N so that the approximate value of the sum is such that the relative percentile error is less than or equal to 0.025%
In the main script input the value of k = 3; 5; or 7.
Depending on the value of k, evaluate the exact sum S and call the function sumserf(k,S) which should return the approximate value of the sum, the relative error, and the value of N needed to meet the above percentile accuracy. If you input k that is not 3, 5, or 7, the script should display an error message and ask you to input a correct value of k.
You should also use a while loop which allows you to input different values of k during one run of the code.
---------------------------------------------------------
The output should look like this:
Enter the exponent k:2
ERROR: You entered incorrect k. Possible choices are: 3, 5, and 7
Enter the exponent k:3
Sum for k = 3, exact sum = 1.20206, approx sum = 1.20177 for N = 41
rel percent error = 0.024
Do you want another k: Yes (1) or No (0)
1
Enter the exponent k:5
Sum for k = 5, exact sum = 1.03693, approx sum = 1.03679 for N = 6
rel percent error = 0.013
Do you want another k: Yes (1) or No (0)
1
Enter the exponent k:7
Sum for k = 7, exact sum = 1.00835, approx sum = 1.00827 for N = 3
rel percent error = 0.008
Do you want another k: Yes (1) or No (0)
0
---------------------------------------------------------
while(1)
k=input('Enter the exponent ');
if(k==3)
sumSerf(k,1.202056903150)
fprintf('Sum for k = %d, exact sum = %.5f, approx sum = %.5f for N = %d', k, S, sum, n)
fprintf('rel percent error = %.3f', error)
elseif(k==5)
sumSerf(k,1.036927755143)
fprintf('Sum for k = %d, exact sum = %.5f, approx sum = %.5f for N = %d', k, S, sum, n)
fprintf('rel percent error = %.3f', error)
elseif(k==7)
sumSerf(k,1.008349277382)
fprintf('Sum for k = %d, exact sum = %.5f, approx sum = %.5f for N = %d', k, S, sum, n)
fprintf('rel percent error = %.3f', error)
else
fprintf('ERROR: You entered incorrect k. Possible choices are: 3, 5, and 7\n')
end
c=input('Do you want another k: Yes (1) or No (0)');
if(c==0)
break
end
end
and here's the function:
function [sum,n,error] = sumSerf(k,S)
error=1;
sum=1;
n=1;
i=2;
while(error>=0.025*abs(sum-S)/sum)
sum=sum+(1/ power(i,k));
n=n+1;
i=i+1;
end
error=0.025*abs(sum-S)/sum;
end
When I tested it, it just keep looping without giving an answer. How to stop the loop? Is there anything I did wrong that need to be fixed?
Answers (1)
KSSV
on 30 Nov 2017
Check the below pseudo code..to exit a loop if condition is met:
N = 100 ;
for i = 1:N
x = rand ;
if x<0.1
fprintf('Condition met: exititng the loop; iterations:%d\n',i)
break
end
end
2 Comments
Ariel Chou
on 30 Nov 2017
KSSV
on 30 Nov 2017
It is an example code on how to exit loop when a condition is met......understand it and implement it in your code.
Categories
Find more on Programming 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!