Need help debugging numerical Basel Problem solution

5 views (last 30 days)
Need help debugging my numerical Basel Problem solution. It keeps increasing "n" until the error is below some set value in relation to known theoretical solution.
My code is below. Executed manually step by step it seems to work but the script seems to hang (unable to shut down via ctrl+c) but keeps chugging away. Not sure whats wrong.
%SOLVES BASEL PROMLEM NUMERICALLY using matlab SUM function
%set starting conditions
n = 1;
MYerror = 100;
Strue = (pi^2)/6;
Scalc = 1/(n^2);
values = [];
%looping algorythm SUM()
while MYerror > 10
n = n+1;
Scalc = (1/(n^2));
values = [values Scalc];
valuesSUM = sum(values);
MYerror = 100*((abs(Strue-valuesSUM))/Strue);
end
----------------------------------------------
This code I wrote without using the "sum()" function seems to work great but I need to get the one with sum() function to work but I'll include this for reference.
%SOLVES BASEL PROMLEM NUMERICALLY
%set starting conditions
n = 1;
MYerror = 100;
Strue = (pi^2)/6;
Scalc = 1/(n^2);
%looping algorythm
while MYerror > 0.001
n = n+1;
Scalc = Scalc + (1/(n^2));
MYerror = 100*((abs(Strue-Scalc))/Strue);
end
----------------------------------------------
Thanks in advance :)

Answers (0)

Community Treasure Hunt

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

Start Hunting!