-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApproximation.cpp
130 lines (124 loc) · 2.23 KB
/
Approximation.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include "Approximation.h"
#include "LinearEquations.h"
using namespace std;
void Pade(int m, int n,double *a,double(*function)(double)){
int N = m + n;
double q0 = 1;
double p0 = a[0];
double **b = new double*[N + 1];
for (int i = 0; i < N+1; i++){
b[i] = new double[N+1];
}
for (int i = 0; i < N; i++){
for (int j = 0; j <=i - 1; j++){
if (j <= n){
b[i][j] = 0;
}
}
if (i <= n){
b[i][i] = 1;
}
for (int j = i + 1; j < N; j++){
b[i][j] = 0;
}
for (int j = 0; j <=i; j++){
if (j < m){
b[i][n + j] = -1 * a[i - j];
}
}
for (int j = n + i+1; j < N; j++){
b[i][j] = 0;
}
b[i][N] = a[i+1];
}
/*
使用部分选主元法求解线性方程组
*/
double *x = GaussianPivotElimination(b, N);
if (x == NULL){
cout << "没有求得系数!" << endl;
}
else{
cout << "有理逼近系数:" << endl;
for (int i = 0; i < N; i++){
cout << "x[" << i + 1 << "]:" << x[i] << " ";
}
cout << endl;
}
}
double padeFunction(double x){
return exp(-1 * x);
}
void testPade(){
double *a = new double[3];
a[0] = 1;
a[1] = -1;
a[2] = 0.5;
a[3] = -1.0/6;
a[4] = 1.0/24;
a[5] = -1.0/120;
Pade(2, 3, a, padeFunction);
}
void Chebyshev(int m, int n, double *a, double(*function)(double)){
int N = m + n;
double q0 = 1;
double **b = new double*[N + 1];
for (int i = 0; i < N + 1; i++){
b[i] = new double[N + 1];
}
/*
构建矩阵B
*/
for (int i = 0; i < N; i++){
for (int j = 0; j < i; j++){
if (j <= n){
b[i][j] = 0;
}
}
if (i <= n){
b[i][i] = 0;
}
for (int j = i + 1; j < n; j++){
b[i][j] = 0;
}
for (int j = n; j < N; j++){
if (i != 0){
b[i][j] = -1 * (a[i + j - n] + a[abs(i - j + n)])/2;
}
else{
b[i][j] = -1 * a[j-n]/2;
}
}
if (i != 0){
b[i][N] = a[i];
}
else{
b[i][N] = a[i] / 2;
}
}
/*
利用选主元法求解线性方程组
*/
double *x=GaussianPivotElimination(b, N);
if (x == NULL){
}
else{
cout << "有理逼近系数:" << endl;
for (int i = 0; i < N; i++){
cout << "x[" << i + 1 << "]:" << x[i] << " ";
}
cout << endl;
}
}
void testChebyshev(){
cout << "test" << endl;
double *a = new double[6];
a[0] = 1.266066;
a[1]=-1.130318;
a[2]=0.271495;
a[3]=-0.044337;
a[4]=0.005474;
a[5]=-0.000543;
Chebyshev(2, 3, a, padeFunction);
}