Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

python - Trying to make loop for a function that stops after the result is lower than a certain value

I'm taking a beginner python class and part of an exercise we were given was this:

The point x with the property x= sin(x)?ax+ 30 is called a fixed point of the function f(x) = sin(x)?ax+ 30. It can be computed with the so-called fixed point iteration: x(i+1)= sin(x(i))?ax(i)+ 30, where the superscript (i) denotes an iteration counter. Here you find a piece of Python code which performs the first 200 steps of this

x=0.5
a=0.5
for i in range(200):
  x = sin(x) - a*x + 30
print('The result after {num} iterations is {res}.'.format(num=i, res=x))

Modify the code in such a way that it stops iterating as soon as |x(i+1)?x(i)| < 10?8. Furthermore it should print a little message if this condition was not met within 200 iteration steps. Test your code with a= 0.5 and a= 8

I've tried to do this but am not getting anywhere with it. This is what I have so far, any help would be much appreciated.

x = 0.5
a = 8
iterations = 0

for i in range(y):
    x = sin(x) - a*x + 30
    if abs(x**(i+1) - x**i) < 1.e-8:
        break
    elif i > 200:
        iterations += 1

print('The result after {num} iterations is {res}.'.format(num=i, res=x))

if iterations > 0:
    print('The condition was not met within 200 iteration steps.')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To see what happens during the iteration, I advise adding print(i, x) to the loop in either the original code (after being corrected with the needed indent) or your code. You can remove it before submitting.

The original and your code lack from math import sin. Code posted should be ready to run, including needed imports.

Your posted code neglects to define y=200, but there is no need to add that for the problem as specified. In any case, for i in range(y) will give i the values 0, 1, ..., 199. Your condition i > 200 will never be true. The easiest way to do something if the loop does not break is to use an else: clause. Or you can replace iterations with the more descripting failure and correct your condition.

Combined with Tom K's comment, possible code that works is

from math import sin

x = 0.5
a = 0.5
for i in range(200):
    x1 = sin(x) - a*x + 30
    print(i, x)  # remove before submitting
    if abs(x1 - x) < 1.e-8:
        break
    x = x1
else:
    print('The condition was not met within 200 iteration steps.')

print('The result after {num} iterations is {res}.'.format(num=i, res=x))

This prints for a = .5 and 8:

The result after 59 iterations is 20.649274368307022.

The condition was not met within 200 iteration steps.
The result after 199 iterations is -1.1949487767945635e+181.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...