c语言sumxy函数 c语言sum函数的使用方法

想用C语言编写多项式拟合的程序【c语言sumxy函数 c语言sum函数的使用方法】#include stdio.h
#include conio.h
#include stdlib.h
#include math.h
main()
{
int i,j,m,n=7,poly_n=2;
double x[7]={1,2,3,4,6,7,8},y[7]={2,3,6,7,5,3,2};
double a[3];
void polyfit(int n,double *x,double *y,int poly_n,double a[]);
system("cls");
polyfit(n,x,y,poly_n,a);
for (i=0;ipoly_n 1;i)/*这里是升序排列c语言sumxy函数 , Matlab是降序排列*/
printf("a[%d]=%g\n",i,a[i]);
getch();
}
/*==================polyfit(n,x,y,poly_n,a)===================*/
/*=======拟合y=a0 a1*x a2*x^2 …… apoly_n*x^poly_n========*/
/*=====n是数据个数 xy是数据值 poly_n是多项式c语言sumxy函数的项数======*/
/*===返回a0,a1,a2,……a[poly_n]c语言sumxy函数,系数比项数多一(常数项)=====*/
void polyfit(int n,double x[],double y[],int poly_n,double a[])
{
int i,j;
double *tempx,*tempy,*sumxx,*sumxy,*ata;
void gauss_solve(int n,double A[],double x[],double b[]);
tempx=calloc(n,sizeof(double));
sumxx=calloc(poly_n*2 1,sizeof(double));
tempy=calloc(n,sizeof(double));
sumxy=calloc(poly_n 1,sizeof(double));
ata=calloc((poly_n 1)*(poly_n 1),sizeof(double));
for (i=0;in;i)
{
tempx[i]=1;
tempy[i]=y[i];
}
for (i=0;i2*poly_n 1;i)
for (sumxx[i]=0,j=0;jn;j)
{
sumxx[i] =tempx[j];
tempx[j]*=x[j];
}
for (i=0;ipoly_n 1;i)
for (sumxy[i]=0,j=0;jn;j)
{
sumxy[i] =tempy[j];
tempy[j]*=x[j];
}
for (i=0;ipoly_n 1;i)
for (j=0;jpoly_n 1;j)
ata[i*(poly_n 1) j]=sumxx[i j];
gauss_solve(poly_n 1,ata,a,sumxy);
free(tempx);
free(sumxx);
free(tempy);
free(sumxy);
free(ata);
}
void gauss_solve(int n,double A[],double x[],double b[])
{
int i,j,k,r;
double max;
for (k=0;kn-1;k)
{
max=fabs(A[k*n k]); /*find maxmum*/
r=k;
for (i=k 1;in-1;i)
if (maxfabs(A[i*n i]))
{
max=fabs(A[i*n i]);
r=i;
}
if (r!=k)
for (i=0;in;i)/*change array:A[k]A[r] */
{
max=A[k*n i];
A[k*n i]=A[r*n i];
A[r*n i]=max;
}
max=b[k];/*change array:b[k]b[r]*/
b[k]=b[r];
b[r]=max;
for (i=k 1;in;i)
{
for (j=k 1;jn;j)
A[i*n j]-=A[i*n k]*A[k*n j]/A[k*n k];
b[i]-=A[i*n k]*b[k]/A[k*n k];
}
}
菜鸟求助,c语言问题,一下sum函数以求a,b两数的和,下面程序的主函数中给任给两书x,y调用sum函数求和 。#includestdio.h
#includemath.h
float sum(float a,float b)
{
float s;
s=a b;
return s;
}
void main()
{
float x,y,m;
printf("请输入求和的两个数:");
scanf("%f %f",x,y);//初始化
m=sum(x,y);
printf("%f\n",m);
}
关于最小二乘法的c语言程序#include "stdafx.h"
#include stdio.h
#include conio.h
#include stdlib.h
#include cmath
#include iostream
using namespace std;
void polyfit(int n, double x[], double y[], int poly_n, double a[])
{
int i, j;
double *tempx, *tempy, *sumxx, *sumxy, *ata;
void gauss_solve(int n, double A[], double x[], double b[]);
tempx = new double[n];
sumxx = new double[poly_n * 21];
tempy = new double[n];
sumxy = new double[poly_n1];
ata = new double[(poly_n1)*(poly_n1)];
for (i = 0; in; i)
{
tempx[i] = 1;
tempy[i] = y[i];
}
for (i = 0; i2 * poly_n1; i)
for (sumxx[i] = 0, j = 0; jn; j)
{
sumxx[i]= tempx[j];
tempx[j] *= x[j];
}
for (i = 0; ipoly_n1; i)
for (sumxy[i] = 0, j = 0; jn; j)
{
sumxy[i]= tempy[j];
tempy[j] *= x[j];
}
for (i = 0; ipoly_n1; i)
for (j = 0; jpoly_n1; j)
ata[i*(poly_n1)j] = sumxx[ij];
gauss_solve(poly_n1, ata, a, sumxy);
delete [] tempx;
tempx = NULL;
delete [] sumxx;
sumxx = NULL;
delete [] tempy;
tempy = NULL;
delete [] sumxy;
sumxy = NULL;
delete [] ata;
ata = NULL;
}
void gauss_solve(int n, double A[], double x[], double b[])
{
int i, j, k, r;
double max;
for (k = 0; kn - 1; k)
{
max = fabs(A[k*nk]); /*find maxmum*/
r = k;
for (i = k1; in - 1; i)
if (maxfabs(A[i*ni]))
{
max = fabs(A[i*ni]);
r = i;
}
if (r != k)
for (i = 0; in; i)/*change array:A[k]A[r] */
{
max = A[k*ni];
A[k*ni] = A[r*ni];
A[r*ni] = max;
}
max = b[k];/*change array:b[k]b[r]*/
b[k] = b[r];
b[r] = max;
for (i = k1; in; i)
{
for (j = k1; jn; j)
A[i*nj] -= A[i*nk] * A[k*nj] / A[k*nk];
b[i] -= A[i*nk] * b[k] / A[k*nk];
}
}
for (i = n - 1; i = 0; x[i] /= A[i*ni], i--)
for (j = i1, x[i] = b[i]; jn; j)
x[i] -= A[i*nj] * x[j];
}
/*==================polyfit(n,x,y,poly_n,a)===================*/
/*=======拟合y=a0 a1*x a2*x^2 …… apoly_n*x^poly_n========*/
/*=====n是数据个数 x y是数据值 poly_n是多项式c语言sumxy函数的项数======*/
/*===返回a0,a1,a2,……a[poly_n]c语言sumxy函数,系数比项数多一(常数项)=====*/
void main()
{
intn = 9, poly_n = 2;
//double x[20] = { 1 ,2,3,4,7 ,8 ,9,11,12, 13,15,15,17 ,17, 19,18 ,20,19,20, 20 },
//y[20] = { 1,3,6 ,10 ,17,25,34,45,57,70,85,100,117,134,153,171,191,210 ,230 ,250 };
double x[9]={1,3,4,5,6,7,8,9,10},
y[9]={10,5,4,2,1,1,2,3,4};
double a[50];
polyfit(n, x, y, poly_n, a);
for (int i = 0; ipoly_n1; i)/*这里是升序排列,Matlab是降序排列*/
cout"a["i"]="a[i]endl;
}
运行结果,c语言sumxy函数我是拟合的2次的,你可以拟合多次 。
方程式c语言sumxy函数:
0.267571*x*x-3.60531*x 13.4597=0
这个2次多项式的最低点还用我说吗,在那个区间上 , 自己代入c语言sumxy函数;
sum在c语言中的用法在C语言中没有sum这个保留字,换句话它在C语言中只能算是一个标识符,没有特殊的语法功能 。
一般来说C语言的标识符,有两个基本的使用原则 。
1、要符合语法要求,C语言中规定,标识符有数字、字母、下划线(_)组成,而且第1符号只能为字母或者下划线 。
2、标识符的命名,尽量便于阅读 。如问题中的sum用于表示两数之和,就容易理解 。
求两数之和,用C语言怎么写?谢谢直接求两数之和include
stdio.hint
main
(){int
a,b,sum;
//申明a,b,sum为整型
a=1;b=1;
//给a,b赋值,这里a,b自己定
sum=a b;
//求和
printf{"the
sum
is
%d",sum};
//输出sumc语言sumxy函数的值
return
(0);}
求输入的两数之和#include
stdio.hint
main
(){int
a,b,sum;
printf{"Please
enter
x,y:"};
//提示输入x,y
scanf{"%d,%d",a,b};
//将输入的值赋给a,b
sum=a b;
//求和
printf{"the
sum
is
%d",sum};
//输出sum的值
return
(0);
}
使用函数求和c语言sumxy函数:
#include
stdio.hint
sum(int
x,int
y)
//定义求和函数{int
sum;
//申明sum为整数型sum=x y;
//将输入函数的x,y求和return(sum);
//向函数返回sum的值}
int
main
()
//以下为主函数{int
a,b,sum;
sum=sum(a,b);
//调用求和函数
printf{"the
sum
is
%d",sum};
//输出sum的值
return
(0);
}
原创噢~
急急急 C语言 输入两个整数x,y,分别调用sum函数 求他们的和,调用sub函数 求他们的差,int sum(int x,int y);
int sub(int x, int y);
void main()
{
int x,y,sum,sub;
scanf("%d,%d",x,y);
sum = sum(int x,int y);
printf("x y = %d",sum);
sub = sub(int x ,int y);
printf("x-y = %d",sum);
}
int sum(int x,int y)
{
return (x y);
}
int sub(int x, int y)
{
return(x-y);
}
关于c语言sumxy函数和c语言sum函数的使用方法的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读