c语言 编程 牛顿迭代,C语言编写牛顿迭代法的跟踪
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
#include?
#include
float?f(float?x)?/*定義f函數(shù),以實現(xiàn)f(x)=x*x*x+x*x-3*x-3*/
{
float?y;
y=((x+1)*x-3.0)*x-3.0;
return?y;
}
float?g(float?x)?/*定義g函數(shù),為f函數(shù)的導函數(shù)*/
{
float?y;
y=(3.0*x+2.0)*x-3.0;
return?y;
}
float?h(float?x)?/*定義h函數(shù),以實現(xiàn)x*x*x=-x*x+3*x+3*/
{
float?y;
y=(3.0-x)*x+3.0;
return?y;
}
rootA()?/*定義rootA函數(shù),以實現(xiàn)用二分法求近似根*/
{
int?i=0;
float?x,y,x1=0,x2=0,y1=0,y2=0;
do
{
printf("Input?x1=");
scanf("%f",&x1);
printf("Input?x2=");
scanf("%f",&x2);
y1=f(x1);
y2=f(x2);
}
while?(y1*y2>=0);?/*定義變量x1,x2為區(qū)間的兩端點,同時保證兩個端點的函數(shù)值相異*/
do
{
x=(x1+x2)/2;
y=f(x);
if(fabs(y)<1e-6)
break;
if(y*y1>0)
{
x1=x;
y1=y;
}
else
x2=x;
i=i+1;?/*定義變量i以計算迭代次數(shù)*/
}
while?(fabs(x2-x1)>=1e-6);?/*設定精確度為1e-6*/
printf("The?running?number?is?%d\n",i);
printf("A?root?of?equation?is?%7f\n",x);
}
rootB()?/*定義rootB函數(shù),以實現(xiàn)用基本迭代法求近似根*/
{
float?x0=0,x1=0,j;
int?k=0,N;
printf("Input?the?divisor:");?/*選定初值*/
scanf("%f",&x0);
printf("Input?the?number:");?/*輸入最大迭代次數(shù)*/
scanf("%d",&N);
printf("Input?the?Precision:");?/*輸入精度*/
scanf("%f",&j);
do
{
x1=pow(h(x0),(1.0/3.0));?/*建立迭代格式*/
if(fabs(x1-x0)
{
printf("A?root?of?equation?is?%7f\n",x1);
break;
}
x0=x1;
k=k+1;
}while?(k<=N);
if(k>N)
printf("It?is?Failure!\n");
if(k<=N)
printf("The?running?number?is?%d\n",k);
}
rootC()?/*定義rootC函數(shù),以實現(xiàn)用牛頓切線法求近似根*/
{
float?x0,x1,j;
int?N,k=0;
printf("Input?the?divisor:");
scanf("%f",&x0);
printf("Input?the?number:");
scanf("%d",&N);
printf("Input?the?Precision:");
scanf("%f",&j);
for(k=0;k<=N;k=k+1)
{
if(fabs(g(x0))<=1e-6)
{
printf("It?is?Singular!\n");
break;
}
else
{
x1=x0-(f(x0)/g(x0));?/*迭代函數(shù)*/
if(fabs(x1-x0)
{
printf("A?root?of?equation?is?%7f\n",x1);
break;
}
x0=x1;
}
}
if(k>N)
printf("It?is?Failure!\n");
else
printf("The?running?number?is?%d\n",k);
}
void?main()?/*主函數(shù)*/
{
int?choose;
char?yes_no;
printf("Which?method?do?you?want?to?choose?(1,2,3)\n");
printf("1---------ErFenFa\n");
printf("2---------JiBenDieDaiFa\n");
printf("3---------NiuDunQieXianFa\n");
do
{
printf("You?choose?is?");
scanf("%d",&choose);
switch(choose)
{
case?1:?rootA();break;
case?2:?rootB();break;
case?3:?rootC();break;
case?0:?break;
default:?printf("It?is?error!\n");
}
if(choose==0)
break;
printf("Are?you?want?to?contain?(Y/N)");
do
{
scanf("%c",&yes_no);
}
while?(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}
while?(yes_no=='Y'||yes_no=='y');
}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的c语言 编程 牛顿迭代,C语言编写牛顿迭代法的跟踪的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 00后都开始立遗嘱了:微信号、QQ号、游
- 下一篇: 京东物流已有超过 1 万名专业送装师傅,