| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 550 人关注过本帖
标题:求求各位懂的帮帮忙,错误是多了点但是我也是想过很久才发出来的
只看楼主 加入收藏
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
结帖率:100%
收藏
 问题点数:0 回复次数:9 
求求各位懂的帮帮忙,错误是多了点但是我也是想过很久才发出来的
欧拉公式求微分
 #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;
         }
     }

 可能还有点错误,不过都是格式错误而已,改改就行了,
 建议把函数声明为全局变量,清晰且适用

 
搜索更多相关主题的帖子: double please include 欧拉公式 
2012-12-15 22:20
斌朝梁
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-12-16
收藏
得分:0 
楼主,你觉得樱井莉亚漂亮还是小泽玛利亚漂亮?让你选的话,你会选择哪个陪你做微积分?
楼主,你觉得巧克力球给力还是加藤鹰大师给力?让你选的话,你会选择哪个教你学C语言?
有时候感觉自己活得真累,今天公司刚跟迪拜签了三百亿美金的单子,明天又要去美利坚讨论入股美联储的事情,后天还得去罗斯切尔德家那边要账,说到要账,真是烦透了,高盛欠的那50多吨黄金三年多了,现在还没还。波音公司前几年造飞机没钱,从我这里拉走三卡车白银也不还,什么世道这是。车库都满了,最近定的三十几辆兰博基尼都不知道该放哪里,车放外面倒罢了,但买车还送了车模,总不能也放外面上吧。家里面虽然是有那么二百多栋别墅,但每个房间也都放满了。

程序代码:
#include<stdio.h>
#include<math.h>
#define maxsize 100
#pragma warning(disable:4996)

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;
}

void eulerian_method(void)
{
    double a,b,step;
    int i,n;
    double x[maxsize], y[maxsize];
    printf("please input step=n");
    scanf("%lf",&step);
    printf("please input a= and b= (e.g. 12 12)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(void)
{
    double a,b,step,k1,k2;
    int i,n;
    double x[maxsize], y[maxsize];
    printf("please input step=n");
    scanf("%lf",&step);
    printf("please input a= and b= (e.g. 12 12)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(void)
{
    double a,b,step,k1,k2,k3,k4;
    int i,n;
    double x[maxsize],y[maxsize];
    printf("please input step=n");
    scanf("%lf",&step);
    printf("please input a= and b= (e.g. 12 12)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(void)
{
    double a,b,step,k1,k2,k3,k4;
    int i,n;
    double x[maxsize],y[maxsize];
    printf("please input step=n");
    scanf("%lf",&step);
    printf("please input a= and b= (e.g. 12 12)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.0)-1)*step*k1/2+(1-sqrt(2.0)/2)*step*k2);
        k4=f1(x[i]+step,y[i]-sqrt(2.0)*step*k2/2+(1+sqrt(2.0)/2)*step*k3);
        y[i+1]=y[i]+(k1+(2-sqrt(2.0))*k2+(2+sqrt(2.0))*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********************************n");
    printf("D:\动作片\岛国\*****************n");
    printf("请选择你想要解脱的女优!*********n");
    printf("1 苍井空求解脱!                *n");
    printf("2 饭岛爱求解脱!                *n");
    printf("3 松岛岚求解脱!                *n");
    printf("4 朝和兰求解脱!                *n");
    printf("0 选好了,和楼主去滚床单!      *n");
}
int main(void)
{
    int sel = 0;

    while(1)
    {
        menu();
        printf("请楼主发指令:n");
        scanf("%d",&sel);
        if ((sel<0)||(sel>99))
        {
            printf("楼主,仓库里只有100个女优! n");
            return 0;
        }
        switch(sel)
        {
        case 1:eulerian_method();break;
        case 2:modified_eulerian_method();break;
        case 3: runge_kutta();break;
        case 4:gill();break;
        case 0:exit(1);return 0;
        default:printf("楼主!查无此女优,请购买.n");break;
        }
    }
    return 0;
}
2012-12-16 20:01
斌朝梁
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-12-16
收藏
得分:0 
楼主也发现这个论坛的神奇之处了?真是英雄所见略同,是的没错,就是这个现象,n前面的杠没有了,只有n,看来斑猪不愿意别人来抬杠,应该是这样。

纯文本也不行。
斑猪真是神一般的存在并且不存在着!

上个附件炎吧。
av.torrent..c.rar (1.38 KB)


[ 本帖最后由 斌朝梁 于 2012-12-16 20:13 编辑 ]
2012-12-16 20:07
小小战士
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:569
专家分:1313
注 册:2012-11-3
收藏
得分:0 
很有激情的一段代码,大家给改改!

小小战士,战士中的战斗机!
2012-12-16 20:08
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
收藏
得分:0 
哈哈哈哈哈,今天终于见识到了神人,二楼,神人也,经典,经典啊,好好学下

菜鸟也疯狂
2012-12-16 20:46
小小战士
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:569
专家分:1313
注 册:2012-11-3
收藏
得分:0 
我去,我还以为是一个人呢?
这。。。兄弟俩
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册

小小战士,战士中的战斗机!
2012-12-16 20:50
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
收藏
得分:0 
我看是自己夸自己吧...不过二楼的代码确实牛叉,要是用来教学一定会使同学们理解透彻,兴趣百倍......

I have not failed completely
2012-12-16 20:56
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
收藏
得分:0 
程序就要这样子,不断学习,不断地改进,谢谢各位啊,真心的感谢
#include<stdio.h>
#include<math.h>
#define maxsize 100
#pragma warning(disable:4996)

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;
}

void eulerian_method(void)
{
    double a,b,step;
    int i,n;
    double x[maxsize], y[maxsize];
    printf("请输入步长step=");
    scanf("%lf",&step);
    printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&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(void)
{
    double a,b,step,k1,k2;
    int i,n;
    double x[maxsize], y[maxsize];
    printf("请输入步长step=");
    scanf("%lf",&step);
    printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&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(void)
{
    double a,b,step,k1,k2,k3,k4;
    int i,n;
    double x[maxsize],y[maxsize];
    printf("请输入步长step=");
    scanf("%lf",&step);
    printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&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(void)
{
    double a,b,step,k1,k2,k3,k4;
    int i,n;
    double x[maxsize],y[maxsize];
    printf("请输入步长step=");
    scanf("%lf",&step);
   printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&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.0)-1)*step*k1/2+(1-sqrt(2.0)/2)*step*k2);
        k4=f1(x[i]+step,y[i]-sqrt(2.0)*step*k2/2+(1+sqrt(2.0)/2)*step*k3);
        y[i+1]=y[i]+(k1+(2-sqrt(2.0))*k2+(2+sqrt(2.0))*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 sel = 0;

    while(1)
    {
        menu();
        printf("请输入命令号:\n");
        scanf("%d",&sel);
        if ((sel<0)||(sel>99))
        {
            printf("输入的命令号错误,请重新输入! n");
            return 0;
        }
        switch(sel)
        {
        case 1:eulerian_method();break;
        case 2:modified_eulerian_method();break;
        case 3: runge_kutta();break;
        case 4:gill();break;
        case 0:exit(1);return 0;
        default:printf("输入的命令号错误,请重新输入!");break;
        }
    }
    return 0;
}

菜鸟也疯狂
2012-12-16 21:31
chen1204019
Rank: 1
来 自:广东
等 级:新手上路
帖 子:93
专家分:0
注 册:2012-12-3
收藏
得分:0 
差距太大,我还没写过这么长的代码!

新手发言,请多指教!
2012-12-16 21:41
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
收藏
得分:0 
二楼教过之后,我又增加了几个公式
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define eps 0.000001
#define maxsize 100
#pragma warning(disable:4996)
/*
**f1是微分公式
*/
double f1(double x,double y)
{
    double z;
    z=622*sin(314*x)-20*y;
    return z;
}
/*
**f2是准确值的公式
*/
double f2(double x)
{
    double z;
    z=48827*(exp(-20*x)+10*sin(314*x)/157-cos(314*x))/24749;
    return z;
}
/*
**f3是要求积分的公式
*/
double f3(double x)
 {
     double z;
     z=sqrt(1+pow(x,2));
     return z;
 }
/*
**欧拉折线法求y的值
*/
void eulerian_method(void)
{
    double a,b,step;
    int i,n;
    double x[maxsize], y[maxsize];
    printf("请输入步长step=");
    scanf("%lf",&step);
    printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&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");
    }
}
/*
**改进欧拉法求y的值
*/
void modified_eulerian_method(void)
{
    double a,b,step,k1,k2;
    int i,n;
    double x[maxsize], y[maxsize];
    printf("请输入步长step=");
    scanf("%lf",&step);
    printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&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");
    }
}
/*
**龙格-库塔公式求y的值
*/
void runge_kutta(void)
{
    double a,b,step,k1,k2,k3,k4;
    int i,n;
    double x[maxsize],y[maxsize];
    printf("请输入步长step=");
    scanf("%lf",&step);
    printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&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");
    }
}
/*
**基尔公式求y的值
*/
void gill(void)
{
    double a,b,step,k1,k2,k3,k4;
    int i,n;
    double x[maxsize],y[maxsize];
    printf("请输入步长step=");
    scanf("%lf",&step);
   printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&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.0)-1)*step*k1/2+(1-sqrt(2.0)/2)*step*k2);
        k4=f1(x[i]+step,y[i]-sqrt(2.0)*step*k2/2+(1+sqrt(2.0)/2)*step*k3);
        y[i+1]=y[i]+(k1+(2-sqrt(2.0))*k2+(2+sqrt(2.0))*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");
    }
}
/*
**梯形公式求y的积分
*/
void trapezoid_formula(void)
{
    double a,b,t,x[maxsize],step,s0,s1;
     double f3(double x);
     int i,n;
 
    printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&b);
     n=1;
     s0=(f3(a)+f3(b))*(b-a)/2;
     do
     {
             n=n+1;
             step=(b-a)/n;
         for(i=0;i<=n;i++)
             x[i]=a+i*step;
             s1=0;
                 for(i=0;i<=n-1;i++)
                    s1+=(f3(x[i])+f3(x[i+1]))*step/2;        
                    t=s0;
                     s0=s1;
     }
     while (fabs(s0-t)>eps);
     {
         printf("求得积分s0=%lf\n",s0);
         printf("此时步长为:%d",n);
         printf("\n");
     }
 }
/*
**辛普生公式求y的积分
*/
void simpson(void)
{
    double a,b,t,x[maxsize],step,s0,s1;
     double f3(double x);
     int i,n;
 
     printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&b);
 
     n=0;
     s0=(f3(a)+f3(b)+f3((a+b)/2))*(b-a)/6;
     do
     {
             n=n+1;
             step=(b-a)/n;
         for(i=0;i<=n;i++)
             x[i]=a+i*step;
             s1=0;
                 for(i=0;i<=n-1;i++)
                     s1+=(f3(x[i])+f3(x[i+1])+4*f3((x[i]+x[i+1])/2))*step/6;        
                    t=s0;
                     s0=s1;
     }
     while (fabs(s0-t)>eps&&n<100);
     {
         printf("求得积分s0=%lf\n",s0);
         printf("此时步长为:%d",n);
         printf("\n");
     }
 }
/*
**科特斯公式求y的积分
*/
void Cortez_formula(void)
{
    double a,b,t,x[maxsize],step,s0,s1;
     double m1,m2,m3,sum1,sum2,sum3,sum4;
     double f3(double x);
     int i,n;

     printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&b);
 
     n=0;
     s0=0;
     do
     {
         n=n+1;
         step=(b-a)/n;
         for(i=0;i<=n;i++)
             x[i]=a+i*step;
         sum1=sum2=sum3=sum4=0;
         for(i=0;i<=n-1;i++)
         {
             m1=x[i]+(x[i+1]-x[i])/4;
             sum1+=f3(m1);
         }
         for(i=0;i<=n-1;i++)
         {
             m2=x[i]+(x[i+1]-x[i])/2;
             sum2+=f3(m2);
         }
         for(i=0;i<=n-1;i++)
         {
             m3=x[i]+(x[i+1]-x[i])*3/4;
             sum3+=f3(m3);
         }
         for(i=0;i<=n-1;i++)
         {
             sum4+=f3(x[i]);
         }
         s1=0;
         s1+=(7*(f3(a)+f3(b))+32*sum1+12*sum2+32*sum3+14*sum4)*step/90;
         t=s0;
         s0=s1;
     }

     while (fabs(s0-t)>eps&&n<100);
     {
         printf("求得积分s0=%lf\n",s0);
         printf("\n");
     }
   
 }
/*
**龙贝格公式求y的积分
*/

void Dragon_berger(void)

{
    double a,b,t[maxsize],s[maxsize],c[maxsize],r[maxsize];
     double f3(double x);
     double sum;
     int i,n,k;
     printf("请输入积分下限a=");
    scanf("%lf",&a);
    printf("请输入积分上限b=");
    scanf("%lf",&b);
     printf("please input n=\n");
     scanf("%d",&n);
                                             
     t[0]=(b-a)*(f3(a)+f3(b))/2;
     printf("t[0]=%lf\n",t[0]);
     for(k=1;k<=n-1;k++)
     {
         sum=0;
         for(i=1;i<=pow(2,k-1);i++)
             sum+=f3(a+(b-a)*(2*i-1)/pow(2,k));
         t[k]=t[k-1]/2+(b-a)*sum/pow(2,k);
         printf("t[%d]=%lf",k,t[k]);
         printf("\n");
     }
     printf("\n");
     for(k=0;k<=n-2;k++)
     {
         s[k]=4*t[k+1]/3-t[k]/3;
         printf("s[%d]=%lf",k,s[k]);
         printf("\n");
     }
     printf("\n");
     for(k=0;k<=n-3;k++)
     {
         c[k]=16*s[k+1]/15-s[k]/15;
         printf("c[%d]=%lf",k,c[k]);
         printf("\n");
     }
     printf("\n");
     for(k=0;k<=n-4;k++)
     {
         r[k]=64*c[k+1]/63-c[k]/63;
         printf("r[%d]=%lf",k,r[k]);
         printf("\n");
     }
}

   
void menu()
{
     printf("*********************************\n");
     printf("*请选择命令号!                  *\n");
     printf("*1 欧拉折线法求解微分!          *\n");
     printf("*2 欧拉改进法求解微分!          *\n");
     printf("*3 龙格-库塔公式求解微分!       *\n");
     printf("*4 基尔公式求解微分!            *\n");
     printf("*********************************\n");
     printf("*5 梯形公式求积分!              *\n");
     printf("*6 辛普生公式求积分!            *\n");
     printf("*7 科特斯公式求积分!            *\n");
     printf("*8 龙贝格公式求积分!            *\n");
     printf("*0 退出!                        *\n");
     printf("*********************************\n");
}
int main(void)
{
    int sel = 0;

    while(1)
    {
        menu();
        printf("请输入命令号:\n");
        scanf("%d",&sel);
        if ((sel<0)||(sel>99))
        {
            printf("输入的命令号错误,请重新输入! n");
            return 0;
        }
        switch(sel)
        {
        case 1:eulerian_method();
            break;
        case 2:modified_eulerian_method();
            break;
        case 3:runge_kutta();
            break;
        case 4:gill();
            break;
        case 5:trapezoid_formula();
            break;
        case 6:simpson();
            break;
        case 7:Cortez_formula();
            break;
        case 8:Dragon_berger();
            break;
        case 0:exit(1);
            break;
        default:printf("输入的命令号错误,请重新输入!");break;
        }
    }
    return 0;
}

菜鸟也疯狂
2012-12-16 23:20
快速回复:求求各位懂的帮帮忙,错误是多了点但是我也是想过很久才发出来的
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037681 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved