Summation and while loop...

3 次查看(过去 30 天)
S Weinberg
S Weinberg 2015-2-15
编辑: Stephen23 2015-2-16
I am so stuck with this question i want to pull my hair out lol. The question is:
Evaluate the following series ∞ Σ n=1, un in which un is not known explicitly but is given in terms of a recurrence relation. You should stop the summation when un < 10^-8 un+1 = (Un-1)^2 + (un)^2 with u1 = 0.5, u2 = 0.6
So firstly the difference is 0.1 since 0.6 - 0.5 = 0.1. However un+3 = (0.5)^2 + (0.6)^2 does not work out to this difference of 0.1. So i just stuck with 0.1. Then i did:
d=0.1;
a=0.5
while a,10^-8
a=a-d
end
a
This runs to infinity. Any thoughts will be greatly appreciated!!!

回答(2 个)

Stephen23
Stephen23 2015-2-15
编辑:Stephen23 2015-2-15
Why is the difference relevant? What you quoted from the problem set does not mention the difference anywhere, not in the formula or even the end-condition (which is a single value).
Homework is aimed at giving your the opportunity to learn something. Here are a few tips to get you started. You will need to keep track of the following values:
  • un for N-1
  • un for N
  • the sum
Start by defining the two un values with the initial values that you have been given. Then within the while loop calculate the new un and the sum based on the formula that you have been given. Use the while-loop condition to detect when un reaches the limit.
  2 个评论
S Weinberg
S Weinberg 2015-2-16
Thanks Stephen, i tried your method but ran into problems due my lack of knowledge of Matlab. Currently i am trying
a=0.5; b=0.6; c=0.5; while c >10^1e-8; a=a^2; b=b^2; c=a+b; end c
Thanks for your help once again!
Stephen23
Stephen23 2015-2-16
编辑:Stephen23 2015-2-16
It does actually help us if you format your code correctly on this forum. Please use the {} Code button above the textbox, and review the preview pane below the textbox.
That code looks much better. Does it do what you expect it to?
"lack of knowledge of Matlab": actually you already know enough MATLAB to solve this problem. The challenge now is in figuring out the algorithm, not in writing the code. You could even figure out the algorithm on a piece of paper first: think about iterations: what happens on the first iteration, the second, etc., and which variables have to have what values.

请先登录,再进行评论。


John D'Errico
John D'Errico 2015-2-15
编辑:John D'Errico 2015-2-15
Stephen said some good things. I might also add one problem with the while loop. This will not do as you apparently think:
while a,10^-8
If you wanted to test whether a was less than some value, you might write it as
while a < 1e-8
if you are unsure if a might be negative, and you just wanted to test the magnitude,
while abs(a) < 1e-8
So if your code is essentially in an infinite loop, the problem is most likely that while loop. In fact, what does the line you wrote do anyway?
while a,10^-8
stuff
end
The MATLAB parser actually looks at the line with the while as TWO separate lines. First, the while
while a
does a test. If a is NON-zero, then it continues to loop. It will stop only when a is EXACTLY zero.
Then MATLAB sees the ",10^-8" fragment. It simply sees this as a second line of code, with nothing to do but dump the value of 10^-8 to the command line, EVERY TIME THROUGH THE LOOP.
For example, I'll write a simple loop, that was equivalent to yours.
a = 1;
while a,10^-8
a = a + 1;
end
ans =
1e-08
ans =
1e-08
ans =
1e-08
ans =
1e-08
ans =
1e-08
ans =
1e-08
ans =
1e-08
ans =
1e-08
ans =
1e-08
ans =
1e-08
ans =
1e-08
...
In fact, this code block ran through 13798 iterations in the bare second before I was able to break out using control c. At every iteration, it dumped out that same value to my command window, and then into the bit bucket.
And finally, see that I wrote 1e-8, NOT 10^-8. While the two are equivalent in terms of the value, the exponential notation form does not require MATLAB to perform an exponentiation. So you should prefer to use 1e-8. Why throw CPU cycles on the floor?
  2 个评论
S Weinberg
S Weinberg 2015-2-16
I see that the 1e-08 is better, although my study guide does not indicate this to me. The problem that i have is that i am working with not just "a" but "a" and "b". So thats why i am trying to make the sum of a and b = to a c and loop the c. Here is what i have so far...
a=0.5; b=0.6; c=0.5; while c >10^1e-8; a=a^2; b=b^2; c=a+b; end c
Please let me know your thoughts! Thanks!
Stephen23
Stephen23 2015-2-16
编辑:Stephen23 2015-2-16
"I see that the 1e-08 is better": it is not just about ie-08 being better. John D'Errico tried (very carefully) to explain that your line of code while a,10^-8 does not actually do what you think it is doing. You should read his explanation more carefully.
Note that 10^1e-8 is equivalent to 10^(10^-8), which is not the same as what is written in the original problem: 10^-8, and is also different to what John D'Errico wrote.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by