求求各位懂的帮帮忙,错误是多了点但是我也是想过很久才发出来的
欧拉公式求微分#include<stdio.h>
#include<math.h>
#define maxsize 100
int main(void)
{
double a,b,step,x[maxsize],y[maxsize];
double f1(double x,double y);
double f2(double x);
int i,n;
printf("please input step=\n");
scanf("%lf",&step);
printf("please input a= and b=\n");
scanf("%lf %lf",&a,&b);
n=(int)((b-a)/step);
for(i=0;i<=n;i++)
x[i]=a+i*step;
y[0]=0;
for(i=0;i<=n-1;i++)
{
y[i+1]=y[i]+step*f1(x[i+1],y[i]);
}
for(i=0;i<=n;i++)
{
printf("x[%d]=%lf,y[%d]=%lf,f2=%lf",i,x[i],i,y[i],f2(x[i]));
printf("\n");
}
return 0;
}
double f1(double x,double y)
{
double z;
z=622*sin(314*x)-20*y;
return z;
}
double f2(double x)
{
double z;
z=48827*(exp(-20*x)+10*sin(314*x)/157-cos(314*x))/24749;
return z;
}
改进欧拉公式求微分#include<stdio.h>
#include<math.h>
#define maxsize 100
int main(void)
{
double a,b,step,x[maxsize],y[maxsize],k1,k2;
double f1(double x,double y);
double f2(double x);
int i,n;
printf("please input step=\n");
scanf("%lf",&step);
printf("please input a= and b=\n");
scanf("%lf %lf",&a,&b);
n=(int)((b-a)/step);
for(i=0;i<=n;i++)
x[i]=a+i*step;
y[0]=0;
for(i=0;i<=n-1;i++)
{
k1=step*f1(x[i],y[i]);
k2=step*f1(x[i]+step,y[i]+k1);
y[i+1]=y[i]+(k1+k2)/2;
}
for(i=0;i<=n;i++)
{
printf("x[%d]=%lf,y[%d]=%lf,f2=%lf",i,x[i],i,y[i],f2(x[i]));
printf("\n");
}
return 0;
}
double f1(double x,double y)
{
double z;
z=622*sin(314*x)-20*y;
return z;
}
double f2(double x)
{
double z;
z=48827*(exp(-20*x)+10*sin(314*x)/157-cos(314*x))/24749;
return z;
}
龙格-库塔公式求微分
#include<stdio.h>
#include<math.h>
#define maxsize 100
int main(void)
{
double a,b,step,x[maxsize],y[maxsize],k1,k2,k3,k4;
double f1(double x,double y);
double f2(double x);
int i,n;
printf("please input step=\n");
scanf("%lf",&step);
printf("please input a= and b=\n");
scanf("%lf %lf",&a,&b);
n=(int)((b-a)/step);
for(i=0;i<=n;i++)
x[i]=a+i*step;
y[0]=0;
for(i=0;i<=n-1;i++)
{
k1=step*f1(x[i],y[i]);
k2=step*f1(x[i]+step/2,y[i]+k1/2);
k3=step*f1(x[i]+step/2,y[i]+k2/2);
k4=step*f1(x[i]+step,y[i]+k3);
y[i+1]=y[i]+(k1+2*k2+2*k3+k4)/6;
}
for(i=0;i<=n;i++)
{
printf("x[%d]=%lf,y[%d]=%lf,f2=%lf",i,x[i],i,y[i],f2(x[i]));
printf("\n");
}
return 0;
}
double f1(double x,double y)
{
double z;
z=622*sin(314*x)-20*y;
return z;
}
double f2(double x)
{
double z;
z=48827*(exp(-20*x)+10*sin(314*x)/157-cos(314*x))/24749;
return z;
}
基尔公式求微分
#include<stdio.h>
#include<math.h>
#define maxsize 100
int main(void)
{
double a,b,step,x[maxsize],y[maxsize],k1,k2,k3,k4;
double f1(double x,double y);
double f2(double x);
int i,n;
printf("please input step=\n");
scanf("%lf",&step);
printf("please input a= and b=\n");
scanf("%lf %lf",&a,&b);
n=(int)((b-a)/step);
for(i=0;i<=n;i++)
x[i]=a+i*step;
y[0]=0;
for(i=0;i<=n-1;i++)
{
k1=f1(x[i],y[i]);
k2=f1(x[i]+step/2,y[i]+k1*step/2);
k3=f1(x[i]+step/2,y[i]+(sqrt(2)-1)*step*k1/2+(1-sqrt(2)/2)*step*k2);
k4=f1(x[i]+step,y[i]-sqrt(2)*step*k2/2+(1+sqrt(2)/2)*step*k3);
y[i+1]=y[i]+(k1+(2-sqrt(2))*k2+(2+sqrt(2))*k3+k4)*step/6;
}
for(i=0;i<=n;i++)
{
printf("x[%d]=%lf,y[%d]=%lf,f2=%lf",i,x[i],i,y[i],f2(x[i]));
printf("\n");
}
return 0;
}
double f1(double x,double y)
{
double z;
z=622*sin(314*x)-20*y;
return z;
}
double f2(double x)
{
double z;
z=48827*(exp(-20*x)+10*sin(314*x)/157-cos(314*x))/24749;
return z;
}
我想把这几种方法和在一起,就是这个意思,以上单独求微分的方法都完全正确,我现在需要做的,就是想合起来
#include<stdio.h>
#include<math.h>
#define maxsize 100
void eulerian_method(double x[maxsize],double y[maxsize])
{
double a,b,step;
double f1(double x,double y);
double f2(double x);
int i,n;
printf("please input step=\n");
scanf("%lf",&step);
printf("please input a= and b=\n");
scanf("%lf %lf",&a,&b);
n=(int)((b-a)/step);
for(i=0;i<=n;i++)
x[i]=a+i*step;
y[0]=0;
for(i=0;i<=n-1;i++)
{
y[i+1]=y[i]+step*f1(x[i+1],y[i]);
}
for(i=0;i<=n;i++)
{
printf("x[%d]=%lf,y[%d]=%lf,f2=%lf",i,x[i],i,y[i],f2(x[i]));
printf("\n");
}
}
void modified_eulerian_method(double x[maxsize],double y[maxsize])
{
double a,b,step,k1,k2;
double f1(double x,double y);
double f2(double x);
int i,n;
printf("please input step=\n");
scanf("%lf",&step);
printf("please input a= and b=\n");
scanf("%lf %lf",&a,&b);
n=(int)((b-a)/step);
for(i=0;i<=n;i++)
x[i]=a+i*step;
y[0]=0;
for(i=0;i<=n-1;i++)
{
k1=step*f1(x[i],y[i]);
k2=step*f1(x[i]+step,y[i]+k1);
y[i+1]=y[i]+(k1+k2)/2;
}
for(i=0;i<=n;i++)
{
printf("x[%d]=%lf,y[%d]=%lf,f2=%lf",i,x[i],i,y[i],f2(x[i]));
printf("\n");
}
}
void runge_kutta(double x[maxsize],double y[maxsize])
{
double a,b,step,k1,k2,k3,k4;
double f1(double x,double y);
double f2(double x);
int i,n;
printf("please input step=\n");
scanf("%lf",&step);
printf("please input a= and b=\n");
scanf("%lf %lf",&a,&b);
n=(int)((b-a)/step);
for(i=0;i<=n;i++)
x[i]=a+i*step;
y[0]=0;
for(i=0;i<=n-1;i++)
{
k1=step*f1(x[i],y[i]);
k2=step*f1(x[i]+step/2,y[i]+k1/2);
k3=step*f1(x[i]+step/2,y[i]+k2/2);
k4=step*f1(x[i]+step,y[i]+k3);
y[i+1]=y[i]+(k1+2*k2+2*k3+k4)/6;
}
for(i=0;i<=n;i++)
{
printf("x[%d]=%lf,y[%d]=%lf,f2=%lf",i,x[i],i,y[i],f2(x[i]));
printf("\n");
}
}
void gill(double x[maxsize],double y[maxsize])
{
double a,b,step,k1,k2,k3,k4;
double f1(double x,double y);
double f2(double x);
int i,n;
printf("please input step=\n");
scanf("%lf",&step);
printf("please input a= and b=\n");
scanf("%lf %lf",&a,&b);
n=(int)((b-a)/step);
for(i=0;i<=n;i++)
x[i]=a+i*step;
y[0]=0;
for(i=0;i<=n-1;i++)
{
k1=f1(x[i],y[i]);
k2=f1(x[i]+step/2,y[i]+k1*step/2);
k3=f1(x[i]+step/2,y[i]+(sqrt(2)-1)*step*k1/2+(1-sqrt(2)/2)*step*k2);
k4=f1(x[i]+step,y[i]-sqrt(2)*step*k2/2+(1+sqrt(2)/2)*step*k3);
y[i+1]=y[i]+(k1+(2-sqrt(2))*k2+(2+sqrt(2))*k3+k4)*step/6;
}
for(i=0;i<=n;i++)
{
printf("x[%d]=%lf,y[%d]=%lf,f2=%lf",i,x[i],i,y[i],f2(x[i]));
printf("\n");
}
}
void menu()
{
printf("********************************\n");
printf("请选择命令号!*******************\n");
printf("1欧拉折线法求解微分! *\n");
printf("2欧拉改进法求解微分! *\n");
printf("3龙格-库塔公式求解微分! *\n");
printf("4-基尔公式求解微分! *\n");
printf("0退出! *\n");
printf("********************************\n");
}
int main(void)
{
int i,sel;
double (double x[i],double y[i]);
while(1)
{
menu();
printf("请输入命令号:\n");
scanf("%d",&sel);
switch(sel)
{
case 1:eulerian_method(x[i],y[i]);
case 2:modified_eulerian-method(x[i],y[i]);
case 3: runge_kutta(x[i],y[i]);
case 4:gill(x[i],y[i]);
case 0:exit(1);break;
default:
printf("输入的命令号错误!请重新输入:\n");
break;
}
}
return 0;
}
double f1(double x,double y)
{
double z;
z=622*sin(314*x)-20*y;
return z;
}
double f2(double x)
{
double z;
z=48827*(exp(-20*x)+10*sin(314*x)/157-cos(314*x))/24749;
return z;
}
下面的是一位楼主给我的帮助,但是我还是不太看得懂,我希望懂的可以直接帮我改改,一部分也行,好让我从中有所启发
double (double x[i],double y[i]);
这里传递不能为i,因为i是变量,这里规定的是传入的数组的大小,因为是一维数组,所以可以写作double x[],double y[])不用指定大小,但二维数组不行
程序代码:
int i,sel;
double (double x[i],double y[i]); //应规定函数名
while(1)
{
menu();
printf("请输入命令号:\n");
scanf("%d",&sel);
switch(sel)
{
case 1:eulerian_method(x[i],y[i]); //没有关于x和y的声明,i的值也未初始化,无法编译
程序代码:
case 2:modified_eulerian-method(x[i],y[i]); //在main函数中找不到关于这个的声明,无法编译
case 3: runge_kutta(x[i],y[i]); //同上
case 4:gill(x[i],y[i]); //同上
case 0:exit(1);break; //我记得应在每个case的最后都跟一个break;不然default会输出
default:
printf("输入的命令号错误!请重新输入:\n");
break;
}
}
可能还有点错误,不过都是格式错误而已,改改就行了,
建议把函数声明为全局变量,清晰且适用