欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python牛頓法求函數(shù) 牛頓法 python

想用python來(lái)求解牛頓插值問(wèn)題,編了一段程序,其中有些錯(cuò)誤看不出來(lái),懇請(qǐng)大佬指出錯(cuò)誤,代碼如下

import?matplotlib.pyplot?as?plt

公司主營(yíng)業(yè)務(wù):做網(wǎng)站、成都網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出凌河免費(fèi)做網(wǎng)站回饋大家。

from?pylab?import?mpl

import?math

"""

牛頓插值法

插值的函數(shù)表為

xi??????-28.9,?-12.2,?4.4,?21.1,??37.8

f(xi)???2.2,????3.9,?????6.6,?10.3,?15.4

"""

x=[-28.9,-12.2,4.4,21.1,37.8]

y=[2.2,3.9,6.6,10.3,15.4]

"""計(jì)算4次差商的值"""

def?Four_time_difference_quotient(x,?y):

i?=?0????????????#?i記錄計(jì)算差商的次數(shù)

quotient?=?[0,?0,?0,?0,?0,]

while?i??4:

j?=?4

while?j??i:

if?i?==?0:

quotient[j]=((y[j]-y[j-1])/(x[j]-x[j-1]))

else:

quotient[j]?=?(quotient[j]-quotient[j-1])/(x[j]-x[j-1-i])

j?-=?1

i?+=?1

return?quotient;

def?function(data):

return?x[0]+parameters[1]*(data-0.4)+parameters[2]*(data-0.4)*(data-0.55)+\

parameters[3]*(data-0.4)*(data-0.55)*(data-0.65)\

+parameters[4]*(data-0.4)*(data-0.55)*(data-0.80)

"""計(jì)算插值多項(xiàng)式的值和相應(yīng)的誤差"""

def?calculate_data(x,parameters):

returnData=[];

for?data?in?x:

returnData.append(function(data))

return?returnData

"""畫函數(shù)的圖像

newData為曲線擬合后的曲線

"""

def?draw(newData):

plt.scatter(x,y,label="離散數(shù)據(jù)",color="red")

plt.plot(x,newData,label="牛頓插值擬合曲線",color="black")

plt.scatter(0.596,function(0.596),label="預(yù)測(cè)函數(shù)點(diǎn)",color="blue")

plt.title("牛頓插值法")

mpl.rcParams['font.sans-serif']?=?['SimHei']

mpl.rcParams['axes.unicode_minus']?=?False

plt.legend(loc="upper?left")

plt.show()

parameters=Four_time_difference_quotient(x,?y)

yuanzu=calculate_data(x,parameters)

draw(yuanzu)

python牛頓法求多項(xiàng)式的根

#includeiostream.h

#includemath.h

#includeconio.h

const int N=200;

//帶入原函數(shù)后所得的值

double f(float x)

{

return (x*x*x-1.8*x*x+0.15*x+0.65);

}

//帶入一階導(dǎo)函數(shù)后所得的值

double f1(double x)

{

return (3*x*x-3.6*x+0.15);

}

//牛頓迭代函數(shù)

double F(double x)

{

double x1;

x1=x-1.0*f(x)/f1(x);

return (x1);

}

void main()

{

double x0,D_value,x1,y[4];

int k=0,count=0;

for(;;)

{

if(count==3)break;

cout"輸入初始值:";

cinx0;

do

{

k++;

x1=F(x0);

D_value=fabs(x1-x0);

x0=x1;

}

while((D_value0.000005)(k=N));

for(int j=0,flag=0;jcount;j++)

{

if(fabs(y[j]-x1)0.000005)

{ flag=1;

cout"該數(shù)值附近的根已經(jīng)求出,請(qǐng)重新?lián)Q近似值"endl;

break;

}

}

if(flag==1)

continue;

else

{

cout"方程的一個(gè)根:"x1","" 迭代次數(shù)為:"kendl;

y[count]=x1;

count++;

}

//else

//cout"計(jì)算失敗!"endl;

}

}

//你的程序其實(shí)沒(méi)問(wèn)題,牛頓迭代法本身循環(huán)一次只能找到一個(gè)答案,只要再建一個(gè)循環(huán)控制使

//用迭代法的次數(shù)和判斷根的個(gè)數(shù)就行。我又加了一個(gè)判斷是否有重復(fù)的根的循環(huán)。

//希望能對(duì)你有所幫助。

求解這道題目

令f(x)=2sin(x+π/3)-x,牛頓法求解f(x)=0,過(guò)程為:

f'(x)=2cos(x+π/3)-1,任取初值x?,令x?=x?-f(x?)/f'(x?)

然后迭代執(zhí)行:x?=x?,x?=x?-f(x?)/f'(x?),直到|x?-x?|=10??,x?即為所求

C語(yǔ)言代碼如下:

#includestdio.h

#includemath.h

#define PI 3.141592653589793

int main() {

double x0 = PI; // 初值任取

double x1 = x0-(2*sin(x0+PI/3)-x0)/(2*cos(x0+PI/3)-1);

while (fabs(x1 - x0) 1e-8) {

? x0 = x1;

? x1 = x0-(2*sin(x0+PI/3)-x0)/(2*cos(x0+PI/3)-1);

}

printf("%.15f\n", x1);

return 0;

}

運(yùn)行結(jié)果如圖:

python代碼如下:

import math

x0 = math.pi # 初值任取

x1 = x0-(2*math.sin(x0+math.pi/3)-x0)/(2*math.cos(x0+math.pi/3)-1)

while abs(x1-x0) 1e-8:

x0 = x1

x1 = x0-(2*math.sin(x0+math.pi/3)-x0)/(2*math.cos(x0+math.pi/3)-1)

print(x1)

運(yùn)行結(jié)果為:

與C語(yǔ)言結(jié)果一致~

牛頓迭代法python程序求平方根和立方根

import math

def sqrt(x):

y = x

while abs(y * y - x) 1e-6:

y = (y + x / y) / 2

return y

print(sqrt(5))

print(math.sqrt(5))

當(dāng)前名稱:python牛頓法求函數(shù) 牛頓法 python
本文URL:http://chinadenli.net/article6/highog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站外貿(mào)網(wǎng)站建設(shè)網(wǎng)站營(yíng)銷微信公眾號(hào)App開發(fā)小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)