-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNFI 1st & 2nd Derivative (Not Equal).cpp
95 lines (84 loc) · 1.94 KB
/
NFI 1st & 2nd Derivative (Not Equal).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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
using namespace std;
int x[100],y[10][100];
double Umain;
double mult[]={0,0,1,1,1.0/12.0,1.0/12.0};
double fact(int x){
double mul=1;
for(int i=x;i>0;i--)
mul*=i;
return mul;
}
double val_Of_U1(int i){
if(i<=1)
return 1;
else if(i==2){
return 2*Umain-1;
} else if(i==3){
return 3*Umain*Umain-6*Umain+2;
} else if(i==4){
return 4*Umain*Umain*Umain-18*Umain*Umain+22*Umain-6;
} else if(i==5){
double val=0;
val+=5*Umain*Umain*Umain*Umain;
val-=40*Umain*Umain*Umain;
val+=105*Umain*Umain;
val-=100*Umain;
val+=24;
return val;
}
}
double val_Of_U2(int i){
if(i<=1){
return 0;
} else if(i==2){
return 1;
} else if(i==3){
return Umain-1;
} else if(i==4){
return 6*Umain*Umain-9*Umain+11;
} else if(i==5){
double val=0;
val+=2*Umain*Umain*Umain;
val-=12*Umain*Umain;
val+=21*Umain;
val-=10;
return val;
}
}
int main(){
cout<<"Enter the point: \n";
double point;
cin>>point;
cout<<"Enter the value of X and Y: \n";
for(int i=0;i<5;i++){
cin>>x[i]>>y[0][i];
}
for(int i=1;i<5;i++){
for(int j=0;j<5-i;j++){
y[i][j]=y[i-1][j+1]-y[i-1][j];
}
}
for(int i=0;i<5;i++) cout<<x[i]<<" ";
cout<<endl;
for(int i=0;i<5;i++){
for(int j=1;j<=i;j++) cout<<" ";
for(int j=0;j<5-i;j++){
cout<<y[i][j]<<" ";
}
cout<<endl;
}
double sum=0.0;
int h=x[1]-x[0], sign=1;
Umain=(point-x[0])/h;
for(int i=1;i<=4;i++){
sum+=val_Of_U1(i)*y[i][0]*(1.0/fact(i));
}
cout<<"Ans of 1st Derivative: "<<(1.0/h)*sum<<endl;
sum=0.0;
for(int i=2;i<=4;i++){
sum+=val_Of_U2(i)*y[i][0]*mult[i];
}
cout<<"Ans of 2nd Derivative: "<<(1.0/(h*h))*sum<<endl;
return 0;
}