while loop doesn't produce correct result
7 views (last 30 days)
Show older comments
sum = 0;
n = 0;
C = (2/(pi*(1-4*n^2)))^2;
while sum < (1/2)*0.99
n = n+1;
sum = sum + C;
end
disp(['number of terms:',num2str(n),'sum',num2str(sum)])
These are my codes. I want matlab to keep calculating until sum is greater than 99% of a half. However it stops at the first calculation and the result is clearly wrong. Any help would be greatly appreciated! Thanks!
0 Comments
Accepted Answer
Roger Stafford
on 12 Apr 2017
You have not placed the line "C = (2/(pi*(1-4*n^2)))^2;" inside the 'while' loop where it could be updated with changing values of 'n', so 'C' stays constant. That will produce erroneous computation for you.
2 Comments
More Answers (0)
See Also
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!