2018年12月28日 星期五

使用Python 二分法程式 求 (0.0000115*pow(x,2)+0.000014*pow(x,1.5)-0.01962)

使用Python 二分法程式 求 (0.0000115*pow(x,2)+0.000014*pow(x,1.5)-0.01962)


 i    a(i)        b(i)       p(i          f(p(i))          h(i)

{ 1} {37.000000} {38.000000} {37.500000}  {-0.000233} { 0.500000}

Bisection Method failed after {2{ iterations!!!

{ 2} {37.500000} {38.000000} {37.750000}  { 0.000015} { 0.250000}

Bisection Method failed after {3{ iterations!!!

{ 3} {37.500000} {37.750000} {37.625000}  {-0.000109} { 0.125000}

Bisection Method failed after {4{ iterations!!!

{ 4} {37.625000} {37.750000} {37.687500}  {-0.000047} { 0.062500}

Bisection Method failed after {5{ iterations!!!

{ 5} {37.687500} {37.750000} {37.718750}  {-0.000016} { 0.031250}

Bisection Method failed after {6{ iterations!!!

{ 6} {37.718750} {37.750000} {37.734375}  {-0.000000} { 0.015625}

Bisection Method failed after {7{ iterations!!!

{ 7} {37.734375} {37.750000} {37.742188}  { 0.000008} { 0.007812}

Bisection Method failed after {8{ iterations!!!

{ 8} {37.734375} {37.742188} {37.738281}  { 0.000004} { 0.003906}

Bisection Method failed after {9{ iterations!!!

{ 9} {37.734375} {37.738281} {37.736328}  { 0.000002} { 0.001953}

Bisection Method failed after {10{ iterations!!!

{10} {37.734375} {37.736328} {37.735352}  { 0.000001} { 0.000977}

{10} iterations!!!

The Root= {37.735352} f({37.735352})={ 0.000001}

from math import *

MAX=50    #/* maximum iterations */
TOL=0.001 #/* tolerance */

#=================================================================
#define  f(x)  (0.0000115*pow(x,2)+0.000014*pow(x,1.5)-0.01962)
#=================================================================
def f_cal(x) :
    temp=   (0.0000115*pow(x,2)+0.000014*pow(x,1.5)-0.01962)
    return temp

i=1;
a=37;
b=38;
print(" i    a(i)        b(i)       p(i          f(p(i))          h(i)\n");
while(i<=MAX):
    p=(a+b)/2.0;
    h=fabs((b-a)/2.0);
    s = "{%2d} {%9.6f} {%9.6f} {%9.6f}  {%9.6f} {%9.6f}\n" %( i,a,b,p,f_cal(p),h)
    print(s)
    if(fabs(f_cal(p))==0 or h<TOL):
        print("{%2d} iterations!!!\n" %(i))
        print("The Root= {%9.6f} f({%9.6f})={%9.6f}\n" %(p,p,f_cal(p)))
        break
    else:
            if (f_cal(a)*f_cal(p) >0):
                a=p
            elif (f_cal(b)*f_cal(p) >0):
                b=p
            i=i+1
    print("Bisection Method failed after {%d{ iterations!!!\n"%(i))
   


======= RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-2-2.py ==========
 i    a(i)        b(i)       p(i          f(p(i))          h(i)

{ 1} {37.000000} {38.000000} {37.500000}  {-0.000233} { 0.500000}

Bisection Method failed after {2{ iterations!!!

{ 2} {37.500000} {38.000000} {37.750000}  { 0.000015} { 0.250000}

Bisection Method failed after {3{ iterations!!!

{ 3} {37.500000} {37.750000} {37.625000}  {-0.000109} { 0.125000}

Bisection Method failed after {4{ iterations!!!

{ 4} {37.625000} {37.750000} {37.687500}  {-0.000047} { 0.062500}

Bisection Method failed after {5{ iterations!!!

{ 5} {37.687500} {37.750000} {37.718750}  {-0.000016} { 0.031250}

Bisection Method failed after {6{ iterations!!!

{ 6} {37.718750} {37.750000} {37.734375}  {-0.000000} { 0.015625}

Bisection Method failed after {7{ iterations!!!

{ 7} {37.734375} {37.750000} {37.742188}  { 0.000008} { 0.007812}

Bisection Method failed after {8{ iterations!!!

{ 8} {37.734375} {37.742188} {37.738281}  { 0.000004} { 0.003906}

Bisection Method failed after {9{ iterations!!!

{ 9} {37.734375} {37.738281} {37.736328}  { 0.000002} { 0.001953}

Bisection Method failed after {10{ iterations!!!

{10} {37.734375} {37.736328} {37.735352}  { 0.000001} { 0.000977}

{10} iterations!!!

The Root= {37.735352} f({37.735352})={ 0.000001}

>>>

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...