While Loop with approximate error and true error

11 views (last 30 days)
Hey everyone,
I'm fairly new to matlab and I'm not sure where to turn. I've done some research on this type of question and found some useful functions but I'm not sure how to incorporate it into a while loop.
Here's the actual problem I am trying to solve.
I want to write a while loop that sums the following: Sum(1/n^2) from 0 to Inf within an approximate error margin of 0.001%. I would like to display the the value of the sum (which should be pi^2/6), the approximate error and the true error. I know how to calculate the approximate error and I know that the true error is actual - approximate.
So far I know how to solve the sum by doing the following:
>>syms n
>>symsum(1/n^2,1,Inf)
>>
>> ans =
>> pi^2/6
I got one part of the answer, but I'm supposed to have it in a while loop and I don't know if you can use sym, or symsum in a while loop in matlab, and I'm not really sure where to go from here for the rest of the problem.
Originally I had this, but it's completely wrong.
>>x=0;
>>n=1;
>>es=0.00001;
>>while(1)
xold=x;
x=x+(1/n.^2));
if x~=0
ea=abs((x-xold)/x)*100;
end
if ea<=es, break
end
>>end
>>disp(x)
However, this didn't really do anything and it sure didn't give me the correct answer of pi^2/6 like sym, and symsum does. I'd appreciate any help, tips, or feedback on where to go from here.
Thanks!

Answers (0)

Community Treasure Hunt

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

Start Hunting!