Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 任何熟悉穆勒的人';s算法??在编码方面有困难。_Python_Python 3.x - Fatal编程技术网

Python 任何熟悉穆勒的人';s算法??在编码方面有困难。

Python 任何熟悉穆勒的人';s算法??在编码方面有困难。,python,python-3.x,Python,Python 3.x,对于一个只涉及基本python编程的类,我很难让Muller算法的代码正常工作。我的程序还没有像输出那样包含任何虚数,我的停止条件也不能正常工作,但现在我最关心的是如何正确打印下面输出中的数字。我把输出作为代码发布,因为我是这个网站的新手,它不断给我一个错误,说我的“代码格式不正确” 非常感谢您的帮助 When f=(x-5)*(x-4)*(x+7) and initial guesses are 1,3,and -10 and tolerance = 0.000001 The output

对于一个只涉及基本python编程的类,我很难让Muller算法的代码正常工作。我的程序还没有像输出那样包含任何虚数,我的停止条件也不能正常工作,但现在我最关心的是如何正确打印下面输出中的数字。我把输出作为代码发布,因为我是这个网站的新手,它不断给我一个错误,说我的“代码格式不正确”

非常感谢您的帮助

When f=(x-5)*(x-4)*(x+7)
and initial guesses are 1,3,and -10 
and tolerance = 0.000001

The output of this code should look like this:
The initial estimates to the root are:
f( 1 )= 96
f( 3 )= 20
f( -10 )= -630
0 : estimate to the root is f( 1 )= 96

1 : estimate to the root is f( (-4.102012878968893+0j) )= (213.71097514696675+0j)  

2 : estimate to the root is f( (3.463202253458595+0j) )= (8.63161417086553+0j)

3 : estimate to the root is f( (3.6797449479714586+0j) )= (4.515592141362759+0j) 

4 : estimate to the root is f( (3.9234514667540585+0j) )= (0.9001820953746444+0j)

5 : estimate to the root is f( (3.9988300605239253+0j) )= (0.012883020219233893+0j)

6 : estimate to the root is f( (3.9999973985379675+0j) )= (2.861615003307639e-05+0j)

The approximation to the root is f( (3.999999999978821+0j) ) = (2.329696435810382e-
10+0j)
这是我的实际代码:

import string
from math import *
from cmath import *

def evalFunction(f,x):
    x=eval(f)
    return x

def main():
    f= input("Input the function: ")
    p0=eval(input("Input the first estimate to the root of the function: "))
    p1=eval(input("Input the second estimate to the root of the function: "))
    p2=eval(input("Input the third estimate to the root of the function: "))
    t=eval(input("Enter the tolerance "))

    fp0=evalFunction(f,p0)
    fp1=evalFunction(f,p1)
    fp2=evalFunction(f,p2)

    print("The initial estimates to the root are:")
    print("f(",p0,")=",fp0)
    print("f(",p1,")=",fp1)
    print("f(",p2,")=",fp2)
    count=0
    print(count,": estimate to the root is f(",p0,")=",fp0)
    fp3=1

    while count<30:
        while (abs(fp3))>=t:
            fp0=evalFunction(f,p0)
            fp1=evalFunction(f,p1)
            fp2=evalFunction(f,p2)


            #Computes a,b,c 
            o=fp1-fp2
            n=fp0-fp2
            s=p1-p2
            r=p0-p2
            denom=r*s*(p0-p1)
            a=(s*n-r*o)/denom
            b=((r**2)*o-(s**2)*n)/denom
            c=fp2
            print()

            count=count+1

            #Computes the roots
            x1= (-2*c)/(b+(b**2-4*a*c)**.5)
            x2=(-2*c)/(b-(b**2-4*a*c)**.5)

            if b>0:
                p3= p2+x1
                fp3=evalFunction(f,p3)
                print(count,": estimate to the root is f(",p3,")=",fp3)
                print()

            else:
                p3= p2+x2
                fp3=evalFunction(f,p3)
                print(count,": estimate to the root is f(",p3,")=",fp3)
                print()

            p2=p3        

main()
导入字符串
从数学导入*
从cmath进口*
def蒸发功能(f,x):
x=评估值(f)
返回x
def main():
f=输入(“输入函数:”)
p0=eval(输入(“将第一个估计值输入到函数的根:”)
p1=eval(输入(“将第二个估计值输入到函数的根:”)
p2=eval(输入(“将第三个估计值输入到函数的根:”)
t=eval(输入(“输入公差”))
fp0=评估函数(f,p0)
fp1=评估功能(f,p1)
fp2=评估功能(f,p2)
打印(“根的初始估计值为:”)
打印(“f(,p0,)=”,fp0)
打印(“f(“,p1,”)=”,fp1)
打印(“f(“,p2,”)=”,fp2)
计数=0
打印(计数:“:根的估计值为f(“,p0,”)=”,fp0)
fp3=1
当count=t时:
fp0=评估函数(f,p0)
fp1=评估功能(f,p1)
fp2=评估功能(f,p2)
#计算a,b,c
o=fp1-fp2
n=fp0-fp2
s=p1-p2
r=p0-p2
denom=r*s*(p0-p1)
a=(s*n-r*o)/denom
b=((r**2)*o-(s**2)*n)/denom
c=fp2
打印()
计数=计数+1
#计算根
x1=(-2*c)/(b+(b**2-4*a*c)***.5)
x2=(-2*c)/(b-(b**2-4*a*c)**.5)
如果b>0:
p3=p2+x1
fp3=评估功能(f,p3)
打印(计数:“:根的估计值为f(“,p3,”)=”,fp3)
打印()
其他:
p3=p2+x2
fp3=评估功能(f,p3)
打印(计数:“:根的估计值为f(“,p3,”)=”,fp3)
打印()
p2=p3
main()

目前尚不清楚程序的哪些方面对您不起作用,可能除了满足公差后挂起的代码,因为
计数在内部循环中递增,在满足公差后不再执行,导致外部循环,
while count感谢您的投入和改进!为了弄清楚程序的哪些方面对我不起作用,需要重新分配点(p0、p1、p2)。如果在循环结束时让p2=p3、p1=p2和p0=p1,我的程序将收敛到函数的根,但不是给定“正确”输出所需的正确根。我也不明白在我的原始代码中,我是如何得到第一次迭代的正确输出的,第二次迭代几乎是精确的,除了末尾的1个位数,如果你说
p2=p3;p1=p2;p0=p1
则p0、p1、p2三者都将设置为p3。语句
p0,p1,p2=p1,p2,p3
没有这个问题。如果是错误的根,则可能是您的
x1=…
x2=…
如果b>0:
序列造成的。也就是说,当您选择一个分母或另一个分母时,您可能选择错误(或至少不同),这当然会将计算转向不同的根。另外,在除法之前,我会选择分母,而不是同时计算
x1
x2
Oops,我在实际的程序中做了正确的计算,但在这里输入时把它弄糟了。我同意这可能与你建议的顺序有关。我一直在摆弄它,我要么得到了相同的输出,要么得到了不同的输出,但从来没有得到正确的输出。你认为我可能需要在我已经得到的一个if语句之后再使用另一个if语句,根据哪个p值最接近二次曲线的根来重新分配变量吗?这有意义吗?不,以不同的方式重新分配变量没有意义。我给出的维基百科链接上说“符号的选择应确保分母的大小尽可能大”。例如,测试abs(b-(b2-4*ac).5)与abs(b+(b2-4*ac).5)或复数运算中的等效物。注意,您可以为x1、x2等添加一个
print
语句,以及一个
b=int(input())
语句来手动引导计算,以了解更多关于发生了什么的信息。如果你发现一个b序列得到了想要的结果,那么就分析denom、b、x1、x2或其他条件来得到这个序列。
The initial estimates to a root are:
f( 1.0 ) =  96.0
f( 3.0 ) =  20.0
f( -10.0 ) =  -630.0
 0: estimate to a root is f(  1.00000000000000) =  96.00000000000000
 1: estimate to a root is f( -4.10201287896889) = 213.71097514696675
 2: estimate to a root is f(  3.46320225345860) =   8.63161417086548
 3: estimate to a root is f(  3.90342357843416) =   1.15470992046251
 4: estimate to a root is f(  3.98002449809592) =   0.22371275706982
 5: estimate to a root is f(  3.99575149263503) =   0.04691400247817
 6: estimate to a root is f(  3.99909106270745) =   0.01000657113716
 7: estimate to a root is f(  3.99980529453210) =   0.00214213924168
 8: estimate to a root is f(  3.99995828046651) =   0.00045893227349
 9: estimate to a root is f(  3.99999106024078) =   0.00009833815063
10: estimate to a root is f(  3.99999808434378) =   0.00002107225517
11: estimate to a root is f(  3.99999958950253) =   0.00000451547380
12: estimate to a root is f(  3.99999991203627) =   0.00000096760109
#!/usr/bin/env python3.2
from math import *
from cmath import *

def evalFunction(f,x):
    return eval(f)

def main():
    from sys import argv
    f  = argv[1]           if len(argv)>1 else input("Input the function: ")
    p0 = float(argv[2])    if len(argv)>2 else eval(input("Input the first estimate to a root of the function: "))
    p1 = float(argv[3])    if len(argv)>3 else eval(input("Input the next  estimate to a root of the function: "))
    p2 = float(argv[4])    if len(argv)>4 else eval(input("Input the third estimate to a root of the function: "))
    toler = float(argv[5]) if len(argv)>5 else eval(input("Enter the tolerance: "))

    fp0 = evalFunction(f,p0)
    fp1 = evalFunction(f,p1)
    fp2 = evalFunction(f,p2)

    print("The initial estimates to a root are:")
    print("f(",p0,") = ",fp0)
    print("f(",p1,") = ",fp1)
    print("f(",p2,") = ",fp2)
    count = 0
    print ('{:2}: estimate to a root is f({:18.14f}) = {:18.14f}'.format(count,p0,fp0))
    fp3 = 1e9

    while count<30 and abs(fp3) >= toler:
        fp0 = evalFunction(f,p0)
        fp1 = evalFunction(f,p1)
        fp2 = evalFunction(f,p2)

        #Computes a,b,c 
        o = fp1-fp2
        n = fp0-fp2
        s = p1-p2
        r = p0-p2
        denom = r*s*(p0-p1)
        a = (s*n-r*o)/denom
        b = ((r**2)*o-(s**2)*n)/denom
        c = fp2
        count += 1

        #Compute roots
        x1 = (-2*c)/(b+(b**2-4*a*c)**.5)
        x2 = (-2*c)/(b-(b**2-4*a*c)**.5)

        if b>0:
            p3 = p2+x1
        else:
            p3 = p2+x2

        fp3=evalFunction(f,p3)
        print ('{:2}: estimate to a root is f({:18.14f}) = {:18.14f}'.format(count,p3,fp3))
        p2 = p3        

main()
tini ~/sp/math > ./mullermethod.py '(x-5)*(x-4)*(x+7)' 1 3 -10 0.000001
The initial estimates to a root are:
f( 1.0 ) =  96.0
f( 3.0 ) =  20.0
f( -10.0 ) =  -630.0
 0: estimate to a root is f(  1.00000000000000) =  96.00000000000000
 1: estimate to a root is f( -4.10201287896889) = 213.71097514696675
 2: estimate to a root is f( -6.34710329529507) =  76.65637351948691
 3: estimate to a root is f( -6.95941163108092) =   5.31984100233008
 4: estimate to a root is f( -7.00059085626200) =  -0.07800105634618
 5: estimate to a root is f( -6.99999988135762) =   0.00001566079365
 6: estimate to a root is f( -6.99999999999998) =   0.00000000000281