Matrices

This is a discussion on Matrices within the C++ Programming forums, part of the General Programming Boards category; ok here is my code for initialising a matrix to be solved. Code: //All Header Files Used #include <iostream> #include ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    Matrices

    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.
    Last edited by scottmanc; 11-18-2003 at 05:16 PM.
    C++ can hurt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C simple Matrices
    By Cyberman86 in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 05:20 PM
  2. Help getting started..matrices
    By BobDole11 in forum C Programming
    Replies: 7
    Last Post: 11-15-2008, 08:51 PM
  3. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  4. Comparing Matrices (D3D9)
    By MicroFiend in forum Game Programming
    Replies: 2
    Last Post: 10-12-2005, 08:36 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21