#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 10
#include <math.h>
void niudun() {
int NUM;//需要已知点个数-1=插值次数
// double X[MAX] = {0.4, 0.55, 0.65, 0.80, 0.90};
// double Y[MAX] = {0.410705, 0.57815, 0.69675, 0.88811, 1.02652};
double X[MAX];
double Y[MAX];
double X_location, Result;
int i;
printf("N次插值:\n");
printf("N=");
scanf("%d", &NUM);
printf("X坐标:\n");
for (i = 0; i <= NUM; i++)
scanf("%lf", &X[i]);
printf("Y坐标:\n");
for (i = 0; i <= NUM; i++)
scanf("%lf", &Y[i]);
printf("X:\n");
scanf("%lf", &X_location);
int j;
double s = Y[0];
double p = 1;
printf("差商表:\n");
for (i = 1; i <= NUM; i++) {
printf("%d阶差商表:\t", i);
for (j = 1; j <= NUM - i + 1; j++) {
Y[j - 1] = (Y[j] - Y[j - 1]) / (X[j + i - 1] - X[j - 1]);
printf("%lf\t", Y[j - 1]);
}
printf("\n");
p = p * (X_location - X[i - 1]);
s = s + Y[0] * p;
}
printf("Result:%lf\n", s);
}
float lagelangri(float x[], float y[], float xx, int n) {
int i, j;
float *a, yy = 0;
a = new float[n];
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];
}
delete a;
return yy;
}
void lage() {
float x[5] = {-3, -1, 1, 2, 3};
float y[5] = {1, 1.5, 2, 2, 1};
float xx1 = -2, xx2 = 0, xx3 = 2.75, yy1, yy2, yy3;
yy1 = lagelangri(x, y, xx1, 5);
yy2 = lagelangri(x, y, xx2, 5);
yy3 = lagelangri(x, y, xx3, 5);
printf("x1=%-20f,y1=%f\n", xx1, yy1);
printf("x2=%-20f,y2=%f\n", xx2, yy2);
printf("x3=%-20f,y3=%f\n", xx3, yy3);
}
double f(double x) {
return (x * x * x - 0.2 * x * x - 0.2 * x - 1.2);
}
void erfen() {
double x1, x2, xx;
printf("请输入区间的左端与右端,以空格隔开");
scanf("%lf %lf", &x1, &x2);
if (f(x1)*f(x2) >= 0) {
printf("不能使用二分法");
}
while (f(x1)*f(x2) < 0) {
xx = (x1 + x2) / 2;
if (f(x1)*f(xx) > 0) {
x1 = xx;
printf("%f", xx);
} else(f(x2)*f(xx) > 0);
{
x2 = xx;
}
if (abs(xx) < 1 / (2.71 * 2.71 * 2.71 * 2.71 * 2.71)) {
break;
}
}
printf("%f", xx);
}
double x;
double m(int n) {
double result = 1;
for (int i = 0; i < n; i++) {
result *= x;
}
return result;
}
void qinjiushao() {
long int y;
int n;
printf("请输入n与x");
scanf("%d %lf", &n, &x);
for (int i = 0; i < n; i++) {
y += i * m(i);
}
printf("%ld", y);
}
int main() {
int i, l = 0;
while (1) {
printf("---------------------------------------------------\n");
printf(" 请选择计算功能 ");
printf("---------------------------------------------------\n");
printf(" 欢迎使用数值计算系统 \n");
printf("---------------------------------------------------\n");
printf(" 0 退出系统 \n");
printf(" 1 牛顿插值法 \n");
printf(" 2 拉格朗日插值法 \n");
printf(" 3 二分法 \n");
printf(" 4 秦九韶算法 \n");
printf("---------------------------------------------------\n");
printf("---------------------------------------------------\n");
printf("请选择(0-4):>");
scanf("%d", &i);
switch (i) {
case 1:
niudun();
break;
case 2:
lage();
break;
case 3:
erfen();
break;
case 4:
qinjiushao();
break;
}
scanf("%d", &i);
if (i == 0)
break;
}
}