Need help debugging numerical Basel Problem solution
    3 次查看(过去 30 天)
  
       显示 更早的评论
    
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 :)
0 个评论
回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!