数值计算方法作业.doc
文本预览下载声明
数值计算方法作业
姓名:李琦 学号:062410124
求 在x=1.5附近的一个根。
一.牛顿下山法:
#include stdio.h
#include math.h
float f(float x) /* 定义函数f(x) */
{ return x*x*x-x-1; }
void main()
{ float x0,x1=1.5;
x0=1;
for(;;)
{
printf ( x0=%f,x0);
printf ( x1=%f\n,x1);
x0=x1;
x1=x0-((x0*x0*x0-x0-1)/(3*x0*x0-1));
if(x0==x1)
break;
}
printf( x=%f\n,x1);
}
二.加权法
#include stdio.h
#include math.h
float f(float x) /* 定义函数f(x) */
{ return x*x*x-1; }
float f1(float x) /* 定义函数f(x)的导数 */
{ return 3*x*x; }
void main()
{ float x0,x1=1.5,c;
c=f1(x1);x0=1;
printf(c=%f\n,c);
for(;;)
{
printf ( x0=%f,x0);
printf ( x1=%f\n,x1);
x0=x1;
x1=(f(x0)-c*x0)/(1-c);
if(x0==x1)
break;
}
printf(x=%f\n,x1);
}
三.单点弦法:
#include stdio.h
#include math.h
float f(float x) /* 定义函数f(x) */
{ return x*x*x-x-1; }
void main()
{ float x1,x0=1.5,a;
a=f(x0);
x1=1;
for(;;)
{
printf ( x0=%f,x0);
printf ( x1=%f\n,x1);
x0=x1;
x1=x0-(f(x0)*(x0-1.5)/(f(x0)-a));
if(x0==x1)
break;
}
printf( x=%f\n,x1);
}
四.双点弦法:
#include stdio.h
#include math.h
float f(float x) /* 定义函数f(x) */
{
return x*x*x-x-1;
}
void main()
{
float x1=1,x0=1.5,x2;
for(;;)
{
printf ( x0=%f,x0);
printf ( x1=%f\n,x1);
x2=x1-(f(x1)*(x1-x0))/(f(x1)-f(x0));
if(x1==x2)
break;
else
{
x0=x1;
x1=x2;
}
}
printf( x=%f\n,x2);
}
显示全部