改寫EX2-1.c 為Python 寫法
from math import *
#define f(x) = (pow(x,3)-4.0*x+2.0)
#====================================
def f_cal(x) :
temp=x*x*x-4.0*x+2.0
#print(temp)
return temp
a=0.0
b=2.0
x=a
i=1
print(" i x f(x)\n")
while x<=b:
s = "{%5d} {%8.2f} {%2.5f}" %(i,x,f_cal(x))
print(s)
i+=1
x=x+0.1
x=x+0.1
===== RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-1.py =====
i x f(x)
{ 1} { 0.00} {2.00000}
{ 2} { 0.10} {1.60100}
{ 3} { 0.20} {1.20800}
{ 4} { 0.30} {0.82700}
{ 5} { 0.40} {0.46400}
{ 6} { 0.50} {0.12500}
{ 7} { 0.60} {-0.18400} ---有根存在
{ 8} { 0.70} {-0.45700}
{ 9} { 0.80} {-0.68800}
{ 10} { 0.90} {-0.87100}
{ 11} { 1.00} {-1.00000}
{ 12} { 1.10} {-1.06900}
{ 13} { 1.20} {-1.07200}
{ 14} { 1.30} {-1.00300}
{ 15} { 1.40} {-0.85600}
{ 16} { 1.50} {-0.62500}
{ 17} { 1.60} {-0.30400}
{ 18} { 1.70} {0.11300}---有根存在
{ 19} { 1.80} {0.63200}
{ 20} { 1.90} {1.25900}
{ 21} { 2.00} {2.00000}
沒有留言:
張貼留言