-
Notifications
You must be signed in to change notification settings - Fork 0
/
test01.cc
62 lines (51 loc) · 1.15 KB
/
test01.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
using namespace std;
const int N = 1000;
float Lagrange(float arrX [],float arrY[],int n,float x)
{
float yResult =0.0;
float LValue[N];
int k,m;
float temp1,temp2;
for ( k = 0; k < n; k++)
{
temp1 = 0;
temp2 = 0;
for (m = 0;m<n ;m++)
{
if (m == k)
{
continue;
}
temp1 *= x - arrX[m];
temp2 *= arrX[k] - arrX[m];
}
LValue[k] = temp1/temp2;
}
for (int i = 0; i < n; i++)
{
yResult += arrY[i]*LValue[i];
}
return yResult;
}
int main()
{
float arrX[N],arrY[N];
int num;
cout<<"输入插值节点的个数(小于"<<N<<"个): ";
cin>>num;
cout<<"\n--接下来输入这些插值节点(先输入X 再输入对应的Y)--\n";
for(int i=0;i<num;i++)
{
cout<<"第"<<i+1<<"个节点的X值: ";
cin>>arrX[i];
cout<<"第"<<i+1<<"个节点的Y值: ";
cin>>arrY[i];
}
float X;
cout<<"\n--请输入待求解的插值节点的X值--\n";
cin>>X;
float Res = Lagrange(arrX,arrY,num,X);
cout<<"\n--插值结果为: "<<Res<<endl;
return 0;
}