-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifferentiation.cpp
More file actions
107 lines (94 loc) · 1.92 KB
/
Differentiation.cpp
File metadata and controls
107 lines (94 loc) · 1.92 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <iomanip>
using namespace std;
double f(double x)
{
return x * exp(x);
}
double df(double x)
{
return exp(x) + x * exp(x);
}
double ddf(double x)
{
return exp(x) + exp(x) + x * exp(x);
}
double dFD(double x, double h)
{
return 1 / h * (f(x + h) - f(x));
}
double dCD(double x, double h)
{
return 1 / (2 * h) * (f(x + h) - f(x - h));
}
double ddFD(double x, double h)
{
return 1 / pow(h, 2) * (f(x) - 2 * f(x + h) + f(x + 2 * h));
}
double ddCD(double x, double h)
{
return 1 / pow(h, 2) * (f(x + h) - 2 * f(x) + f(x - h));
}
double xyi(double* x, double* y, int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += x[i] * y[i];
}
return sum;
}
double xyij(double* x, double* y, int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
sum += x[i] * y[j];
}
}
return sum;
}
double xs(double* x, int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += x[i];
}
return sum;
}
double xsq(double* x, int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += pow(x[i], 2);
}
return sum;
}
double lSquares(int n, double xyi, double xyij, double xsq, double x)
{
return (n * xyi - xyij) / (n * xsq - pow(x, 2));
}
int main()
{
double error[10], x[10], y[10], h;
for (int i = 1; i < 11; i++)
{
h = i * 0.05;
error[i - 1] = abs(df(2) - dFD(2, i * 0.05));
x[i - 1] = log(h);
y[i - 1] = log(error[i - 1]);
}
cout << lSquares(10, xyi(x, y, 10), xyij(x, y, 10), xsq(x, 10), xs(x, 10)) << endl;
for (int i = 1; i < 11; i++)
{
h = i * 0.05;
error[i - 1] = abs(df(2) - dCD(2, i * 0.05));
x[i - 1] = log(h);
y[i - 1] = log(error[i - 1]);
}
cout << lSquares(10, xyi(x, y, 10), xyij(x, y, 10), xsq(x, 10), xs(x, 10)) << endl;
}