尝试为结果低于某个值后停止的函数设置循环 [英] Trying to make loop for a function that stops after the result is lower than a certain value

查看:140
本文介绍了尝试为结果低于某个值后停止的函数设置循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


具有属性的点x x = sin(x)-ax + 30被称为函数f(x)= sin(x)-ax + 30的不动点。可以用所谓的不动点迭代来计算:x(i + 1)= sin(x(i)) - ax(i)+ 30,其中上标(i)表示迭代计数器。在这里你可以找到一段执行这个



  x = 0.5 
a = 0.5
在范围内(200):
x = sin(x) - a * x + 30
print('{}}迭代后的结果是{格式(num = i,res = x))




<修改代码的方式是,尽快停止迭代| x(i + 1)-x(i)| < 10-8。此外,如果在200次迭代步骤中没有满足这个条件,它应该打印一些信息。用a = 0.5和a = 8来测试你的代码我已经试过这样做了,但是没有得到任何答案。这是我到目前为止,任何帮助将不胜感激。

  x = 0.5 
a = 8
迭代= 0

在范围内(y):
x = sin(x) - a * x + 30
如果abs(x **(i + 1) )-x ** i) 1.e-8:
break
elif i> 200:
迭代+ = 1

print('{ite} {{}}。
如果迭代> 0:
print('在200次迭代步骤中没有达到条件')


print(i,x)正在使用所需的缩进更正)或您的代码。您可以在提交之前将其删除。



原始代码和您的代码在数学导入罪中缺少。发布的代码应该已经准备好运行,包括需要的导入。



您的发布代码忽略了定义 y = 200 ,但是没有必要为指定的问题添加这个问题。在任何情况下,对于范围(y)中的i,将给出值0,1,...,199。 200 永远不会是真的。如果循环没有中断,最简单的方法是使用 else:子句。或者你可以使用更多描述失败来替换迭代,并更正你的条件。



与Tom K的评论相结合,可能的代码是数学导入sin
$中的

  (200):b $ bx = 0.5 
a = 0.5
如果abs(x1-x)<0,则提交
之前移除1.e-8:
break
x = x1
else:
print('条件在200次迭代中没有达到。')

print {'{num}迭代之后的结果是{res}。'format(num = i,res = x))

打印出一个= .5和8:

  59次迭代后的结果是20.649274368307022。 

在200次迭代步骤中没有达到条件。
迭代199次后的结果是-1.1949487767945635e + 181。


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.')

解决方案

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.

这篇关于尝试为结果低于某个值后停止的函数设置循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆