For loop not calculating properly
古いコメントを表示
My instructions are to calculate the result and absolute error for each partial sum: N = 10, 160, and 640. I am supposed to be getting the sum and error value for each N, but I am only getting the sum and error values for 640 only. I do not know why this is the case, as N equals 10, 160 and 640.
PLEASE DO NOT GIVE ME A COMPLETE ANSWER. I WOULD JUST LIKE TO KNOW WHAT I AM DOING WRONG. THANK U.
This is what I have so far:
for N=[10 160 640]
sum=0;
for n=1:N
y=1/n^2
sum=sum+y;
end
sum
error=((pi^2)/6)-sum
end
3 件のコメント
David Hill
2020 年 1 月 14 日
You could vectorize the whole thing.
n=1:640;
s=cumsum(1./(n.^2));
e=abs(pi^2/6-s);
Kyle Donk
2020 年 1 月 14 日
Image Analyst
2020 年 1 月 14 日
Kyle, did you see my Answer (below)??? I already gave you hints for solving it using your current for loop.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!