| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 645 人关注过本帖
标题:最小二乘法编程有错,求助!
只看楼主 加入收藏
秦秦秦
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-4-12
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
最小二乘法编程有错,求助!
这个程序错在哪里了?求助!感激不尽!
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define N 9//N个点
#define T 2//T次拟合
#define W 1//权函数
#define PRECISION 0.00001
float pow_n(float a,int n)
{
    int i;
    if(n==0)
        return (1);
    float res=a;
    for(i=1;i<n;i++)
    {
        res*=a;
    }
    return(res);
}
void mutiple(float a[][N],float b[][T+1],float c[][T+1])
{
    float res=0;
    int i,j,k;
    for(i=0;i<T+1;i++)
        for(j=0;j<T+1;j++)
        {
            res=0;
            for(k=0;k<N;k++)
            {
                res+=a[i][k]*b[k][j];
                c[i][j]=res;
            }
        }
}
void matrix_trans(float a[][T+1],float b[][N])
{
    int i,j;
    for(i=0;i<N;i++)
    {
        for(j=0;j<T+1;j++)
        {
            b[j][i]=a[i][j];
        }
    }
}
void init(float x_y[][2],int n)
{
    int i;
    printf("请输入%d个已知点:\n",N);
    for(i=0;i<n;i++)
    {
        printf("(x%d y%d):",i,i);
        scanf("%f   %f",&x_y[i][0],&x_y[i][1]);
    }
}
void get_A(float matrix_A[][T+1],float x_y[][2],int n)
{
    int i,j;
    for(i=0;i<N;i++)
    {
        for(j=0;j<T+1;j++)
        {
            matrix_A[i][j]=W*pow_n(x_y[i][0],j);
        }
    }
}
void print_array(float array[][T+1],int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<T+1;j++)
        {
            printf("%-g      ",array[i][j]);
        }
        printf("\n");
    }
}
void convert(float argu[][T+2],int n)
{
    int i,j,k,p,t;
    float rate,temp;
    for(i=1;i<n;i++)
    {
        for(j=i;j<n;j++)
        {
            if(argu[i-1][i-1]==0)
            {
                for(p=i;p<n;p++)
                {if(argu[p][i-1]!=0)
                break;}
                if(p==n)
                {
                    printf("方程组无解!\n");exit(0);
                }
                for(t=0;t<n+1;t++)
                {
                    temp=argu[i-1][t];
                    argu[i-1][t]=argu[p][t];
                    argu[p][t]=temp;
                }
            }
            rate=argu[j][i-1]/argu[i-1][i-1];
            for(k=i-1;k<n+1;k++)
            {
                argu[j][k]-=argu[i-1][k]*rate;
                if(fabs(argu[j][k])<=PRECISION)
                    argu[j][k]=0;
            }
        }
    }
}
void compute(float argu[][T+2],int n,float root[])
{
    int i,j;
    float temp;
    for(i=n-1;i>=0;i--)
    {
        temp=argu[i][n];
        for(j=n-1;j>i;j--)
        {
            temp-=argu[i][j]*root[j];
        }
        root[i]=temp/argu[i][i];
    }
}
void get_y(float trans_A[][N],float x_y[][2],float y[],int n)
{
    int i,j;
    float temp;
    for(i=0;i<n;i++)
    {
        temp=0;
        for(j=0;j<N;j++)
        {
            temp+=trans_A[i][j]*x_y[j][1];} y[i]=temp;
    }
}
void cons_formula(float coef_A[][T+1],float y[],float coef_form[][T+2])
{
    int i,j;
    for(i=0;i<T+1;i++)
    {
        for(j=0;j<T+2;j++)
        {
            if(j==T+1)
                coef_form[i][j]=y[i];
            else
                coef_form[i][j]=coef_A[i][j];
        }
    }
}
void print_root(float a[],int n)
{
    int i,j;
    printf("%d个点的%d次拟合的多项式系数为:\n",N,T);
    for(i=0;i<n;i++)
    {
        printf("a[%d]=%g,",i+1,a[i]);
    }
    printf("\n");
    printf("拟合曲线方程为:\ny(x)=%g",a[0]);
    for(i=1;i<n;i++)
    {
        printf(" + %g",a[i]);
        for(j=0;j<i;j++)
        {
            printf("*X");
        }
    }
    printf("\n");
}
void process()
{
    float
        x_y[N][2],matrix_A[N][T+1],trans_A[T+1][N],coef_A[T+1][T+1],coef_formu[T+1][T+2],y[T+1],a[T+1];
    init(x_y,N);
    get_A(matrix_A,x_y,N);
    printf("矩阵A为:\n");
    print_array(matrix_A,N);
    matrix_trans(matrix_A,trans_A);
    mutiple(trans_A,matrix_A,coef_A);
    printf("正定矩阵为:\n");
    print_array(coef_A,T+1);
    get_y(trans_A,x_y,y,T+1);
    cons_formula(coef_A,y,coef_formu);
    convert(coef_formu,T+1);
    compute(coef_formu,T+1,a);
    print_root(a,T+1);
}
void main()
{
    process();
}
搜索更多相关主题的帖子: return include 
2014-05-19 16:57
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:7 
报的是什么错误

我不是砖家,要努力成为砖家。
2014-05-19 17:59
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
收藏
得分:7 
试编译楼主的程序,有一个小问题, exit 函数未声明,加入
#include <stdlib.h>
编译通过。
试运行结果:

请输入9个已知点:
(x0 y0):1 2
(x1 y1):3 4
(x2 y2):5 6
(x3 y3):7 8
(x4 y4):9 10
(x5 y5):11 12
(x6 y6):13 14
(x7 y7):15 16
(x8 y8):17 18
矩阵A为:
1      1      1
1      3      9
1      5      25
1      7      49
1      9      81
1      11      121
1      13      169
1      15      225
1      17      289
正定矩阵为:
9      81      969
81      969      13041
969      13041      187017
9个点的2次拟合的多项式系数为:
a[1]=1,a[2]=1,a[3]=0,
拟合曲线方程为:
y(x)=1 + 1*X + 0*X*X

这是个数学问题,我不甚了解。不知此结果是否有错,有错的话又错在哪里?

在提问时,最好对程序的功能、涉及的知识、出错的情况作具体描述。
2014-05-19 23:17
Arons
Rank: 1
等 级:新手上路
帖 子:5
专家分:9
注 册:2014-5-22
收藏
得分:7 
看不懂
2014-05-23 14:33
秦秦秦
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-4-12
收藏
得分:0 
回复 2 楼 砖家的谎言
第10题.c(14) : error C2143: syntax error : missing ';' before 'type'
第10题.c(17) : error C2065: 'res' : undeclared identifier
第10题.c(17) : warning C4244: '*=' : conversion from 'float ' to 'int ', possible loss of data
第10题.c(19) : warning C4244: 'return' : conversion from 'int ' to 'float ', possible loss of data
执行 cl.exe 时出错.
2014-05-28 22:06
秦秦秦
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-4-12
收藏
得分:0 
回复 3 楼 top398
首先谢谢您的解答
但是按您说的改过之后还是错的,并且与改之前错误一样,报错如下
第10题.c(14) : error C2143: syntax error : missing ';' before 'type'
第10题.c(17) : error C2065: 'res' : undeclared identifier
第10题.c(17) : warning C4244: '*=' : conversion from 'float ' to 'int ', possible loss of data
第10题.c(19) : warning C4244: 'return' : conversion from 'int ' to 'float ', possible loss of data
执行 cl.exe 时出错.
2014-05-28 22:08
快速回复:最小二乘法编程有错,求助!
数据加载中...
 
   



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

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