-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1-ii.cpp
80 lines (62 loc) · 1.81 KB
/
P1-ii.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
#define EIGEN_STACK_ALLOCATION_LIMIT 0
#include<bits/stdc++.h>
#include<math.h>
#include<Eigen/Eigen>
using namespace std;
using namespace Eigen;
//使用和P1-i_ver.2同樣的結構 CoeMatrix*NumSol-(B.C.)=SecondODE
void Numerical_Solution_Abt(){
const int deltaX=300; //deltaX的大小請於此處更改
Matrix<double,deltaX+1,1> NumSol;
Matrix<long double,deltaX+1,1> SolErr;
Matrix<double,deltaX-1,1> SecondODE,sNumSol,BC;
Matrix<double,deltaX-1,deltaX-1> CoeMatrix;
CoeMatrix.setZero();
for(int i=0;i<deltaX-1;i++){
if(i==0){
CoeMatrix(i,i)=-2*(deltaX*deltaX);
CoeMatrix(i,i+1)=1*(deltaX*deltaX);
}
else if(i==deltaX-2){
CoeMatrix(i,i)=-2*(deltaX*deltaX);
CoeMatrix(i,i-1)=1*(deltaX*deltaX);
}
else{
CoeMatrix(i,i)=-2*(deltaX*deltaX);
CoeMatrix(i,i-1)=1*(deltaX*deltaX);
CoeMatrix(i,i+1)=1*(deltaX*deltaX);
}
}
SecondODE.setZero();
BC.setZero();
BC(0,0)=1*(deltaX*deltaX);
BC(deltaX-2,0)=2*(deltaX*deltaX);
SecondODE=SecondODE-BC;
sNumSol=CoeMatrix.colPivHouseholderQr().solve(SecondODE);
for(int i=0;i<=deltaX;i++){
if(i==0){
NumSol(i,0)=1;
}
else if(i==deltaX){
NumSol(i,0)=2;
}
else{
NumSol(i,0)=sNumSol(i-1,0);
}
}
for(int i=0;i<=deltaX;i++){
double XPosi=(double)i/deltaX;
SolErr(i,0)=NumSol(i,0)-(XPosi+1);
}
long double norm_SolErr=0;
for(int i=0;i<=deltaX;i++){
norm_SolErr+=SolErr(i,0)*SolErr(i,0);
}
norm_SolErr=sqrt(norm_SolErr);
cout<<NumSol<<endl;
cout<<"||||||||||||"<<endl;
cout<<norm_SolErr<<endl;
}
int main(){
Numerical_Solution_Abt();
}