ok here is my code for initialising a matrix to be solved.
Code://All Header Files Used #include <iostream> #include <string> #include <cmath> //initialising matrix to the size of 100 double A[100][100]; //declaring functions for use in the program void Finite_Difference(int n,double a,double b,double c,double dx,double A[ ][100]); using namespace std; //Function Definition to Calculate the Finite Difference Matrix void Finite_Difference(int n,double a,double b,double c,double dx, double A[ ][100]) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if (i == j) { A[i][j]=((2*a) - c*(pow(dx,2))); } else if( j == (i+1)) { A[i][j] = (-a + (b/2)*dx); } else if (j == (i-1)) { A[i][j] = (-a - (b/2)*dx); } else A[i][j] = 0; } } } int main() { cout<<" Welcome to the Boundary Value Problem Solver"<<endl; cout<<" This Solver Will be Using the Finite Difference Method"<<endl; cout<<" The Differential Equation to be Solved is in the Form of:"<<endl; cout<<endl; cout<<" ay'' + b' + cy + x = 0"<<endl; cout<<" With Boundary Conditions y(0) = d & y(20) = e"<<endl; double const1,const2,const3,bound1,bound2,dx; bool v1 = false; while(!v1) { cout<<"Please Enter the Constant 'a':\n"; cin>>const1; //Determined whether the input is valid or invalid if(cin.fail()) { cin.clear(); cin.ignore(INT_MAX, '\n'); cout << "The input was invalid. Please try again" << endl; } else { v1 = true; } } bool v2 = false; while(!v2) { cout<<"Please Enter the Constant 'b':\n"; cin>>const2; //Determined whether the input is valid or invalid if(cin.fail()) { cin.clear(); cin.ignore(INT_MAX, '\n'); cout << "The input was invalid. Please try again" << endl; } else { v2 = true; } } bool v3 = false; while(!v3) { cout<<"Please Enter the Constant 'c':\n"; cin>>const3; //Determined whether the input is valid or invalid if(cin.fail()) { cin.clear(); cin.ignore(INT_MAX, '\n'); cout << "The input was invalid. Please try again" << endl; } else { v3 = true; } } bool v4 = false; while(!v4) { cout<<"Please Enter the Boundary Condtion at y(0):\n"; cin>>bound1; //Determined whether the input is valid or invalid if(cin.fail()) { cin.clear(); cin.ignore(INT_MAX, '\n'); cout << "The input was invalid. Please try again" << endl; } else { v4 = true; } } bool v5 = false; while(!v5) { cout<<"Please Enter the Boundary Condition at y(20):\n"; cin>>bound2; //Determined whether the input is valid or invalid if(cin.fail()) { cin.clear(); cin.ignore(INT_MAX, '\n'); cout << "The input was invalid. Please try again" << endl; } else { v5 = true; } } bool v6 = false; while(!v6) { cout<<"Please Enter the Interval Size:\n"; cin>>dx; //Determined whether the input is valid or invalid if(cin.fail()) { cin.clear(); cin.ignore(INT_MAX, '\n'); cout << "The input was invalid. Please try again" << endl; } else if(dx<=0) { cout<< "The Interval Must be Greater than 0"<<endl; cout<< "Please Try Again."<<endl; } else { v6 = true; } } double dim = ((bound2-bound1)/dx) - 1; int size = (int)dim; //Initialises the A matrix to 0 everywhere for(int i=0;i<size-1;i++) { for(int j=0;j<size-1;j++) { A[i][j]=0; } } //creates the Finite_Difference matrix Finite_Difference(size,const1,const2,const3,dx,A); return 0;
Anyways, with values of: a= 8, b=-2, c=-1, y(0) = 5, y(20)= 8 & dx =2
the matrix should then be:
assuming x = 2a -c(dx^2)
y = a + (b/2)dx
z = -a - (b/2)*dx
then the matrix should look like this:
x z 0 0 0 0 0 0 0
y x z 0 0 0 0 0 0
0 y x z 0 0 0 0 0
0 0 y x z 0 0 0 0
0 0 0 y x z 0 0 0
0 0 0 0 y x z 0 0
0 0 0 0 0 y x z 0
0 0 0 0 0 0 y x z
0 0 0 0 0 0 0 y x
But this is not what i'm getting for my output. Can anyone see what i'm not seeing.
Thanks for your time.



LinkBack URL
About LinkBacks


