| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 602 人关注过本帖, 1 人收藏
标题:本人对函数编程不是很熟悉,这个第一个尝试做的,希望会的朋友给我指正-我给 ...
取消只看楼主 加入收藏
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
结帖率:100%
收藏(1)
已结贴  问题点数:50 回复次数:4 
本人对函数编程不是很熟悉,这个第一个尝试做的,希望会的朋友给我指正-我给多点分
#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 please include 
2012-12-15 16:16
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
收藏
得分:0 
欧拉公式求微分
#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;
}


菜鸟也疯狂
2012-12-15 20:04
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
收藏
得分:0 
二楼,麻烦你说说,我就是不太懂才放上来问的,单独的公式求解我已经写得完全正确啦

菜鸟也疯狂
2012-12-15 20:05
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
收藏
得分:0 
楼主大哥,你标注的我不是很明白,你能不能帮我直接改改你标注的错误啊,其实我还是不懂的怎么修改,因为没做过多少次这样的

菜鸟也疯狂
2012-12-15 21:14
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
收藏
得分:0 
谢谢,谢谢,太感谢,我一定会好好看看,我把分全给你

菜鸟也疯狂
2012-12-15 23:45
快速回复:本人对函数编程不是很熟悉,这个第一个尝试做的,希望会的朋友给我指正 ...
数据加载中...
 
   



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

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