My question is:
Calculate the result and absolute error for each partial sum: N = 10, 160, and 640. You should use for-loops and not commands like symsum.
I keep on getting weird answes in Matlab and was wondering if my code was missing something:
%(a) Calculate the result and absolute error for each partial sum: N = 10, 160, and 640.
sum=0;
for N=[10 160 640]
for n=1:1:N
y=1/n^2
end
sum=sum+1
error=((pi^2)/6)-sum
end
disp(sum)
PLEASE DO NOT GIVE ME THE COMPLETE ANSWER TO THE QUESTION. I WOULD JUST LIKE AN EXPLANATION OF WHAT I AM DOING WRONG.

 채택된 답변

ME
ME 2020년 1월 10일

0 개 추천

The main problem here is that you arent actually summing the y values as you go along. You are not adding y to the sum like you should, instead you are calculating all of the y values, ignoring them and adding 1 to the sum instead.
You would also probably be better off indexing your sum and error values so that you get one value for each of the Ns you input.
You have asked not to see the code so I haven't included it, but please comment below if you'd like me to add it!

댓글 수: 5

Kyle Donk
Kyle Donk 2020년 1월 10일
Oh, ok I get what you mean. What do you mean by "indexing"?
ME
ME 2020년 1월 10일
편집: ME 2020년 1월 10일
When I say indexing I mean storing values in the form:
sum(1) = value1
sum(2) = value2
sum(3) = value3
Here, the numbers in brackets are the index values. That will allow you to save your three different sum values as an array of three numbers.
As a hint, you probably want to use the value that you loop over inside of the brackets.
ME
ME 2020년 1월 17일
Did you manage to get this working in the end?
I still have the code if you haven't been able to figure it out.
Also, if you found my answers helpful, then could you possibly accept my answer?
Image Analyst
Image Analyst 2020년 1월 18일
The secondary problems are using sum and error for variable names. Don't use built-in function names for variable names.
ME
ME 2020년 1월 18일
Absolutely, this is a good point. It won’t cause any issues in this simple case but could be more problematic if this was to be built into something larger.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2020년 1월 10일

댓글:

ME
2020년 1월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by