-
Notifications
You must be signed in to change notification settings - Fork 0
/
interLag.cpp
75 lines (58 loc) · 1.19 KB
/
interLag.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
double Lagrange(int n,double x,double *Preim,double *Im){
double s = 0;
for(int k=0;k<n;k++){
double p = 1, xk = *(Preim+k);
for(int i=0;i<n;i++){
if(i==k)
continue;
double xi = *(Preim+i);
p *= (x-xi)/(xk-xi);
};
s += *(Im+k)*p;
};
return s;
}
double factorial(int k){
if (k==0){
return 1;
}else{
return k*factorial(k-1);
};
}
double df(double x,int ord){
if (ord==0){
return log(x);
}else{
return pow(-1,ord+1)/pow(x,ord)*factorial(ord-1);
}
}
double Taylor(int grad,double c,double x){
double s = 0;
for (int k=0;k<=grad;k++){
//
s += df(c,k)*pow(x-c,k)/factorial(k);
}
return s;
}
int main()
{
//
int n = 10;
double *x,*y;
x = new double [n];
y = new double [n];
double val[n] = {0.1,0.9,1.1,2.7,2.1,1.4,0.5,0.3,3.0,2.4};
for (int i=0;i<n;i++)
*(x+i) = val[i];
for (int j=0;j<n;j++)
*(y+j) = log(*(x+j));
double x0 = 1.8;
cout << "Original: " << log(x0) << endl;
cout << "Lagrange: " << Lagrange(n,x0,x,y) << endl;
cout << "Taylor: " << Taylor(4,1,x0) << endl;
return 0;
}