资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

拉格朗日函数图像C语言,拉格朗日函数构造

用C语言实现拉格朗日插值、牛顿插值、等距结点插值算法

#includestdio.h

站在用户的角度思考问题,与客户深入沟通,找到临西网站设计与临西网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站制作、做网站、企业官网、英文网站、手机端网站、网站推广、域名申请雅安服务器托管、企业邮箱。业务覆盖临西地区。

#includestdlib.h

#includeiostream.h

typedef struct data

{

float x;

float y;

}Data;//变量x和函数值y的结构

Data d[20];//最多二十组数据

float f(int s,int t)//牛顿插值法,用以返回插商

{

if(t==s+1)

return (d[t].y-d[s].y)/(d[t].x-d[s].x);

else

return (f(s+1,t)-f(s,t-1))/(d[t].x-d[s].x);

}

float Newton(float x,int count)

{

int n;

while(1)

{

cout"请输入n值(即n次插值):";//获得插值次数

cinn;

if(n=count-1)// 插值次数不得大于count-1次

break;

else

system("cls");

}

//初始化t,y,yt。

float t=1.0;

float y=d[0].y;

float yt=0.0;

//计算y值

for(int j=1;j=n;j++)

{

t=(x-d[j-1].x)*t;

yt=f(0,j)*t;

//coutf(0,j)endl;

y=y+yt;

}

return y;

}

float lagrange(float x,int count)

{

float y=0.0;

for(int k=0;kcount;k++)//这儿默认为count-1次插值

{

float p=1.0;//初始化p

for(int j=0;jcount;j++)

{//计算p的值

if(k==j)continue;//判断是否为同一个数

p=p*(x-d[j].x)/(d[k].x-d[j].x);

}

y=y+p*d[k].y;//求和

}

return y;//返回y的值

}

void main()

{

float x,y;

int count;

while(1)

{

cout"请输入x[i],y[i]的组数,不得超过20组:";//要求用户输入数据组数

cincount;

if(count=20)

break;//检查输入的是否合法

system("cls");

}

//获得各组数据

for(int i=0;icount;i++)

{

cout"请输入第"i+1"组x的值:";

cind[i].x;

cout"请输入第"i+1"组y的值:";

cind[i].y;

system("cls");

}

cout"请输入x的值:";//获得变量x的值

cinx;

while(1)

{

int choice=3;

cout"请您选择使用哪种插值法计算:"endl;

cout" (0):退出"endl;

cout" (1):Lagrange"endl;

cout" (2):Newton"endl;

cout"输入你的选择:";

cinchoice;//取得用户的选择项

if(choice==2)

{

cout"你选择了牛顿插值计算方法,其结果为:";

y=Newton(x,count);break;//调用相应的处理函数

}

if(choice==1)

{

cout"你选择了拉格朗日插值计算方法,其结果为:";

y=lagrange(x,count);break;//调用相应的处理函数

}

if(choice==0)

break;

system("cls");

cout"输入错误!!!!"endl;

}

coutx" , "yendl;//输出最终结果

}

拉格朗日插值用c语言怎么编程?各位高手帮帮忙啦

#includestdio.h

#includestring.h

#define N 100

typedef struct tag{

double x;

double y;

}POINT;

void main()

{

int i,j,n;

double x,temp,Ln=0;

POINT pt[N];

printf("请输入你要输入点的个数,,1=n=%d:\n",N);

printf("n=");

scanf("%d",n);

printf("\n");

printf("\n请输入对应的点数\n");

for(i=0;in;i++)

scanf("%lf,%lf",pt[i].x,pt[i].y);

printf("\n");

printf("输入插值点x的值:\n");

scanf("%lf",x);

printf("\n");

for(i=0;in;i++)

{

for(j=0,temp=1;jn;j++)

{

if(j!=i)

temp=temp*(x-pt[j].x)/(pt[i].x-pt[j].x);

}

Ln=Ln+temp*pt[i].y;

}

printf("输出:\nLn(%lf)=%lf\n",x,Ln);

}

拉格朗日插值公式 C语言实现 runge现象

拉格朗日插值多项式 ,用于离散数据的拟合 C/C++ code

#include stdio.h

#include conio.h

#include alloc.h

float lagrange(float *x,float *y,float xx,int n) /*拉格朗日插值算法*/

{ int i,j;

float *a,yy=0.0; /*a作为临时变量,记录拉格朗日插值多项式*/

a=(float *)malloc(n*sizeof(float));

for(i=0;i=n-1;i++)

{ a[i]=y[i];

for(j=0;j=n-1;j++)

if(j!=i) a[i]*=(xx-x[j])/(x[i]-x[j]);

yy+=a[i];

}

free(a);

return yy;

}

main()

{ int i,n;

float x[20],y[20],xx,yy;

printf("Input n:");

scanf("%d",n);

if(n=20) {printf("Error!The value of n must in (0,20)."); getch();return 1;}

if(n=0) {printf("Error! The value of n must in (0,20)."); getch(); return 1;}

for(i=0;i=n-1;i++)

{ printf("x[%d]:",i);

scanf("%f",x[i]);

}

printf("\n");

for(i=0;i=n-1;i++)

{ printf("y[%d]:",i);scanf("%f",y[i]);}

printf("\n");

printf("Input xx:");

scanf("%f",xx);

yy=lagrange(x,y,xx,n);

printf("x=%f,y=%f\n",xx,yy);

getch();

}

拉格朗日差值多项式的c语言

function =lagrange(x1,y1,xx)

%本程序为Lagrange1插值,其中x1,y1

%为插值节点和节点上的函数值,输出为插值点xx的函数值,

%xx可以是向量。

syms x

n=length(x1);

for i=1:n

t=x1;t(i)=[];L(i)=prod((x-t)./(x1(i)-t));% L向量用来存放插值基函数

end

u=sum(L.*y1);

p=simplify(u) % p是简化后的Lagrange插值函数(字符串)

=subs(p,x,xx);

clf

plot(x1,y1,'ro',xx,,'*')


网页名称:拉格朗日函数图像C语言,拉格朗日函数构造
文章路径:http://cdkjz.cn/article/dsspsgc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220