利用lagrange插值法计算函数值
2004-09-30 15:45
260 查看
x: 10 15 20
y: 1 1.1761 1.3010
求f(13);
答案:
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
int n;
float *x = NULL;
float *y = NULL;
float xFound;
float yGet = 0.0;
float yGetTemp = 1.0;
printf("请输入你要输入的n的个数:");
scanf("%d", &n);
printf("请输入已知x的值(如:10 29 23 ……): ");
x = (float *)malloc(sizeof(float) * n);
for (int client = 0; client < n; client++)
scanf("%f", (x + client));
printf("/n");
printf("请输入已知Y的值(如:23 23 23 ……): ");
y = (float *)malloc(sizeof(float) * n);
for (client = 0; client < n; client++)
scanf("%f", (y + client));
printf("/n请输入要求的x的值: ");
scanf("%f", &xFound);
int temp;
for (client = 0; client < n; client++)
{
for (temp = 0; temp < n; temp++)
{
if (temp == client)
continue;
else
yGetTemp *= ( (xFound - *(x + temp)) / ( *(x + client) - *(x + temp)));
}
yGet += (yGetTemp * (*(y + client)));
yGetTemp = 1.0;
}
printf("/nThe result is:%f/n", yGet);
}
y: 1 1.1761 1.3010
求f(13);
答案:
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
int n;
float *x = NULL;
float *y = NULL;
float xFound;
float yGet = 0.0;
float yGetTemp = 1.0;
printf("请输入你要输入的n的个数:");
scanf("%d", &n);
printf("请输入已知x的值(如:10 29 23 ……): ");
x = (float *)malloc(sizeof(float) * n);
for (int client = 0; client < n; client++)
scanf("%f", (x + client));
printf("/n");
printf("请输入已知Y的值(如:23 23 23 ……): ");
y = (float *)malloc(sizeof(float) * n);
for (client = 0; client < n; client++)
scanf("%f", (y + client));
printf("/n请输入要求的x的值: ");
scanf("%f", &xFound);
int temp;
for (client = 0; client < n; client++)
{
for (temp = 0; temp < n; temp++)
{
if (temp == client)
continue;
else
yGetTemp *= ( (xFound - *(x + temp)) / ( *(x + client) - *(x + temp)));
}
yGet += (yGetTemp * (*(y + client)));
yGetTemp = 1.0;
}
printf("/nThe result is:%f/n", yGet);
}
相关文章推荐
- 利用API 实现字符串表达式的计算
- 利用Gearman,搭建异步分布式计算平台
- C语言 利用gettimeofday()时间差计算
- 利用时间戳来准确计算某个时间点具现在的时间差(转)
- 一个抽象类Shape 里边有两个抽象方法 计算面积 和 周长; Shape由三角形和圆形继承,利用多态的特性设计完成不同子类的功能
- 利用积分图像法快速计算Haar特征
- 数值计算方法与算法:C语言实现利用Gauss消元法解方程组
- APP Inventor中如何利用GPS经纬度计算GPS距离、速度、方向
- Java中,利用语言与平台特性改进RMI分布计算框架
- 利用time的函数计算N+_天的时间
- 案例:利用累加器计算前N个学生的总成绩和平均成绩
- 大数据日志分析系统-python脚本利用es聚合计算
- 利用并行的方法计算自由能
- 通过程序设计几何图形、矩形、圆形、正方形、几种类型,能过利用接口和多态性计算几何图形的面积和周长并显示出来
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型, 能够利用接口和多态性计算几何图形的面积和周长并显示。
- 第十周项目3-利用二叉树遍历思想解决问题(1)计算二叉树节点个数
- 利用反距离权重插值法实现等值线
- * 1.创建一个Rectangle类,添加width和height两个成员变量。 * 2.在Rectangle中添加两种方法分别计算矩形的周长和面积 * 3.编程利用Rectangle输出一个矩形
- 利用无名管道实现简单并行计算
- 机房重构时利用状态模式实现消费时间的计算