zoukankan      html  css  js  c++  java
  • 分段抛物线插值

    先输入数据 两个两个的输,中间用空格隔开,比如
    please input data1: 11 11.08然后回车
    依次输入完五组数据
    完了你想查某个温度的溶解度,他要求你输入温度你输入11.5
    它就输出对应的溶解度,然后他提示你是否继续查溶解度,是就输入y,想结束程序
    就输入n.

    #include<stdio.h>
    #include<stdlib.h>
    #define NUMBER 5
    typedef struct
    {
    double x;
    double y;
    }Point;
    double * parabola(Point*, Point*, Point*);
    double calculate(double* , double );
    int main()
    {
    double x = 0, y = 0;
    double* f = NULL;
    int i = 0, n = 0;//n为要插的中间那个点的位置
    char c = 'y';
    Point p[NUMBER];
    for(i = 0; i < NUMBER; i++)
    {
    printf("Please input the point%d:", i+1);
    scanf("%lf%lf", &p[i].x, &p[i].y);
    while(10 != getchar())
    {
    continue;
    }
    }
    while('n' != c)
    {
    printf("Please input the temperature:");
    scanf("%lf", &x);
    while(10 != getchar())
    {
    continue;
    }
    //计算插值点的位子
    if(2 * x <= (p[1].x + p[2].x)) n = 1;
    else if(2 * x >= (p[NUMBER-2].x + p[NUMBER-1].x)) n = NUMBER - 2;
    else n = (int)NUMBER / 2;
    printf("%d\n", n);
    f = parabola(&p[n-1],&p[n],&p[n+1]);//计算抛物线方程
    y = calculate(f,x);//计算溶解度
    printf("The solubility at this temperature is %lf\n", y);
    printf("Continue?(y/n) ");
    scanf("%c", &c);
    while(10 != getchar())
    {
    continue;
    }
    //释放内存
    free(f);
    }

    return 0;
    }

    //下面为计算抛物线方程的函数
    //设抛物线函数为y = a * x^2 + b * x + c
    //以下f[0]为a,f[1]为b,f[2]为c
    double * parabola(Point* p1, Point* p2, Point* p3)
    {
    double temp1 = 0, temp2 = 0;
    double * f = NULL;
    f = (double*)calloc(3,sizeof(double));
    if(NULL == f)
    {
    printf("Calloc failed!\n");
    return NULL;
    }
    temp1 = (p2->y - p1->y)/(p2->x - p1->x);
    temp2 = (p3->y - p2->y)/(p3->x - p2->x);
    f[0] = (temp2 - temp1)/(p3->x - p1->x);
    f[1] = temp1 - f[0] * (p1->x + p2->x);
    f[2] = p1->y - p1->x * (p1->x * f[0] + f[1]);
    return f;
    }

    //根据温度计算溶解度
    double calculate(double* f, double x)
    {

    double y;
    y = x * (f[0] * x + f[1]) + f[2];
    return y;
    }






    //这里是第二个程序
    #include<stdio.h>
    #include<stdlib.h>
    #define NUMBER 5
    typedef struct
    {
    double x;
    double y;
    }Point;
    double parabola(Point*, Point*, Point*, double);
    int main()
    {
    double x = 0, y = 0;
    int i = 0, n = 0;//n为要插的中间那个点的位置
    char c = 'y';
    Point p[NUMBER];
    for(i = 0; i < NUMBER; i++)
    {
    printf("Please input the data%d:", i+1);
    scanf("%lf%lf", &p[i].x, &p[i].y);
    while(10 != getchar())
    {
    continue;
    }
    }
    while('n' != c)
    {
    printf("Please input the temperature:");
    scanf("%lf", &x);
    while(10 != getchar())
    {
    continue;
    }
    //计算插值点的位子
    if(2 * x < (p[1].x + p[2].x)) n = 1;
    else if(2 * x > (p[NUMBER-2].x + p[NUMBER-1].x)) n = NUMBER - 2;
    else n = (int)NUMBER / 2;
    //printf("%d\n", n);
    y = parabola(&p[n-1],&p[n],&p[n+1],x);
    printf("The solubility at this temperature is %lf\n", y);
    printf("Continue?(y/n) ");
    scanf("%c", &c);
    while(10 != getchar())
    {
    continue;
    }
    }

    return 0;
    }


    //直接套用公式计算溶解度
    double parabola(Point* p1, Point* p2, Point* p3, double x)
    {
    double temp1 = 0, temp2 = 0, temp3 = 0, y = 0;
    double a = 0, b = 0, c = 0;
    temp1 = (p2->y - p1->y) * (x - p1->x);
    temp2 = (p3->y - p1->y) * (p2->x - p1->x) - (p2->y - p1->y) * (p3->x - p1->x);
    temp3 = (p3->x - p1->x) * (p3->x - p1->x) * (p2->x - p1->x);
    y = p1->y + temp1 / (p2->x - p1->x) + temp2 * (x - p1->x) * (x - p2->x) / temp3;
    return y;
    }

  • 相关阅读:
    bzoj 2818 Gcd(欧拉函数 | 莫比乌斯反演)
    bzoj 2186 [Sdoi2008]沙拉公主的困惑(欧拉函数,逆元)
    bzoj 2393 Cirno的完美算数教室(容斥原理+搜索)
    c3p0 连接池配置
    Hibernate连接池断开自动重连
    Oracle ASM注意事项
    JAVA如何获得数据库的字段及字段类型
    在引入的css或者js文件后面加参数的作用
    JAVA注解
    Linux软连接和硬链接
  • 原文地址:https://www.cnblogs.com/wqj1212/p/1510588.html
Copyright © 2011-2022 走看看