求最小二乘曲线的拟合的C语言程序

来源:百度知道 编辑:UC知道 时间:2024/06/03 14:44:14

曲线拟合的最小二乘法
如有实验数据如下
求x=55处的近似值 并画出图形
x 0 10 20 30 40 50 60 70 80 90
y 68 67.1 66.4 65.6 64.6 61.8 61.0 60.8 60.4 60
#include "stdio.h"
#include "graphics.h"
double s(double x[],double y[],double x1)
{
double a,b,tmp1=0,tmp2=0,tmp3=0,tmp4=0,result;
int i=0;
for(;i<10;i++)
{
tmp1+=x[i]*x[i];
tmp2+=y[i];
tmp3+=x[i];
tmp4+=x[i]*y[i];
}
b=(tmp1*tmp2-tmp3*tmp4)/(10*tmp1-tmp3*tmp3);
a=(10*tmp4-tmp2*tmp3)/(10*tmp1-tmp3*tmp3);
result=a*x1+b;
return result;
}
void hzbz(int x,int y)
{
int i,j;
line(0,y,1024,y);
moveto(x-200,y);
j=x-200;
for(i=0;i<100;i++)
{
if(i%5!=0)
lineto(j,y-5);
else
lineto(j,y-8);
j=j+10;
moveto(j,y);
}
line(x,768,x,0);
moveto(x,y+200);
j=y+640;
for(i=0;i<300;i++)
{
if(i%5!=0)
lin