龙格-库塔
#include
#include
float function (float x,float y)
{
return (0-(y*y));//f(x,y)µÄ±í´ïʽ }
int main()
{
float x0,x1,y0,y1,k1,k2,k3,k4,a,b,c,n,h;int i;
scanf (\"%f %f %f %f\",&a,&b,&c,&n);x0=a;
y0=c;
h=(b-a)/n;
for (i=1; i
{
k1=h*function(x0,y0);
k2=h*function(x0+h/2,y0+k1/2);k3=h*function(x0+h/2,y0+k2/2);k4=h*function(x0+h,y0+k3);x1=x0+h;
y1=y0+(k1+2*k2+2*k3+k4)/6;
printf (\"x%d=%f,y%d=%f\\n\",i,x1,i,y1);x0=x1;
y0=y1;
}
return 0;
}
拉格朗日
#include
#include
#include
#define maxlength 500
#define pi 3.1415926535384
floata[maxlength],f[maxlength];
float n;
floatlagrange (floata[],floatf[],float x,int n)
{
int k=0,j;
floaty=0.0,l;
while (k
{
l=1.0;
// printf (\"%lf\\n\",y);
for (j=0; j
{
if (j!=k)
{
l=l*(x-a[j])/(a[k]-a[j]); // printf (\"%lf\\n\",l);
}
}
//printf (\"%lf\\n\",f[k]);
// printf (\"%lf\\n\",l);
y=y+l*f[k];
k++;
}
printf (\"x=%f,y=%f\\n\",x,y);
return y;
}
float fx1(floatx)
{
return (1/(1+x*x));
}
floatfx2(floatx)
{
return exp(x);
}
floatfx3(float x)
{
return sqrt(x);
}
void math1 (float c,float n)
{
int i=0;
float h;
h=2*c/n;
while(i
{
a[i]=i*h-5;
i++;
}
}
void math2()
{
int i=0;
while (i
{
a[i]=cos((2*i+1)*pi/2/(n+1));
i++;
}
}
int main()
{
n=20;
int i;
math1(5,n);
for (i=0; i
{
f[i]=fx1(a[i])
}
lagrange(a,f,0.75,n);
return 0;
}
龙贝格
#include
#include
#include
double t[1000],r[1000];
double f(double x)
{
return (x*x*exp(x));
}
int main()
{
double h,a,b,e;
int i,N,m;
scanf (\"%lf,%lf,%d,%lf\",&a,&b,&N,&e);
h=b-a;
m=0;
t[1]=0.5*h*(f(a)+f(b));
printf (\"%lf\",t[1]);
r[0]=t[0];
while(1)
{
printf (\"\\n\");
for (i=0; i
t[i]=r[i];
m++;
h=h/2;
r[0]=t[0]/2;
for (i=1; i
r[0]+=h*f(a+(i-0.5)*h)/2;
printf (\"%lf\",r[0]);
r[1]=(4*r[0]-t[0])/3;
printf (\" %lf\",r[1]);
if (m==1)
continue;
r[2]=(16*r[1]-t[1])/15;
printf (\" %lf\",r[2]);
if (m==2)
continue;
r[3]=(64*r[2]-t[2])/63;
printf (\" %lf\",r[3]);
if (m==3)
continue;
if ((fabs(r[3]-t[3])
break;
}
printf (\"\\nthe current time is :%d\\n\",m);
return 0;
}
牛顿
#include
#include
#include
double function(double point)
{
return (point*point-2*point*exp(-point)+exp(-2*point));
}
double d_function(double point)
{
return (2*point+2*point*exp(-point)-2*exp(-point)-2*exp(-2*point)); }
int main()
{
double beginner,error1,error2;
int max_times=0;
beginner=error1=error2=0;
scanf (\"%lf %lf %lf %d\",&beginner,&error1,&error2,&max_times);int current_times=0;
while(current_times
{
if(fabs(function(beginner))
{
printf (\"%lf\\n\",beginner);
return 0;
}
if(fabs(d_function(beginner))
{
printf (\"failure\\n\");
return 0;
}
beginner=beginner-function(beginner)/d_function(beginner);if(fabs(function(beginner)/d_function(beginner))
printf (\"%lf\\n\",beginner);
return 0;
}
current_times++;
}
printf(\"failure\\n\");
return 0;
}