Anda di halaman 1dari 4

#include<iostream>

#include<iomanip>
#include<math.h>
#include<cstdio>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int n,i,k,j,p;
float a[50][50],l[50][50]={0},u[50][50]={0},q[50],r[50],sum,sum1,b[50]
[50],z[50][1000]={0},x[50][1000]={0};
float le , np , alpha,dt,dx,gama,t_final;
float pi = 3.14;
std::cout<<"Enter the value of alpha ! ";
std::cin>>alpha;
std::cout<<"Enter the length of rod ! ";
std::cin>>le;
std::cout<<"Enter the Time ! ";
std::cin>>t_final;
std::cout<<"Enter the step time ! ";
std::cin>>dt;
std::cout<<"Enter the node points ! ";
std::cin>>n;

clock_t t_cal;

int y = n-1;

dx = le/(n-1);

gama = (alpha*dt)/(dx*dx);

float t_new = t_final/dt;

for(i=0;i<=y;i++)
{
if (i==0)
{
for (int j = 0; j <= y; j++ )
{
if (j==i)
{
a[i][j] = 1;
}
else if (j==i+1)
{
a[i][j] = 0;
}
else
{
a[i][j] = 0 ;
}
std::cout<<a[i][j]<<" ";
std::cout<<"\n";
}
}

else if (i==y)
{
for (int j = 0; j <= y; j++ )
{
if (j==y)
{
a[i][j] = 1;
}
else if (j==y-1)
{
a[i][j] = 0;
}
else
{
a[i][j] = 0 ;
}
std::cout<<a[i][j]<<" ";
std::cout<<"\n";
}
}
else
{
for(j=0;j<=y;j++)
{
if (j==i-1||j==i+1)
{
a[i][j] = -gama;
}
else if (j==i)
{
a[i][j] = 1+2*gama;
}
else
{
a[i][j] = 0;
}
std::cout<<a[i][j]<<" ";
std::cout<<"\n";
}
}
}
std::cout<<"Enter elements of b matrix"<<"\n";
for (int t = 0; t<=t_new;t++)
{
for(i=0;i<=y;i++)
{
if (i==0)
{
if (t==0)
{
b[i][t] = 1;
}
else if (i==y)
{
b[i][t] = 0;
}
else

{
b[i][t] = 0;
}
}
std::cout<<b[i][j]<<" ";
std::cout<<"\n";
}
}
//********** LU decomposition *****//
for (int k = 0; k<=y; k++)
{
l[k][k] =1;
for (int j = k; j<=y;j++)
{
sum = 0;
for (int s = 0; s<=y-1;s++)
sum+=l[k][s]*u[s][j];
u[k][j] = a[k][j] - sum;
}
for(i=k+1;i<=y;i++)
{
sum = 0;
for (int s = 0; s<=y-1;s++)
sum+=l[k][s]*u[s][j];
l[i][k] = (a[i][k] - sum)/u[k][k];
}
}
//******** Displaying LU matrix**********//
std::cout<<endl<<endl<<"LU matrix is "<<endl;
for(i=0;i<=y;i++)
{
for(j=0;j<=y;j++)
std::cout<<l[i][j]<<" ";
std::cout<<endl;
}
std::cout<<endl;
for(i=0;i<=y;i++)
{
for(j=0;j<=y;j++)
std::cout<<u[i][j]<<" ";
std::cout<<endl;
}

//***** FINDING Z; LZ=b*********//

for (int t = 0; t<=t_new;t++)


{
for(i=0;i<=y;i++)
{
//forward subtitution method
sum=0;
for(p=0;p<i;p++)
sum+=l[i][p]*z[p][t];
z[i][t]=(b[i][t]-sum)/l[i][i];
}

//********** FINDING X; UX=Z***********//


for(i=y;i>=0;i--)
{
sum=0;
for(p=y;p>i;p--)
sum+=u[i][p]*x[p][t];
x[i][t]=(z[i][t]-sum)/u[i][i];

b[i][t+1] = x[i][t];

}
}

//*********** DISPLAYING SOLUTION**************//


std::cout<<endl<<"Set of solution is"<<endl;
for (int t = 0; t<=t_new;t++)
{
for(i=0;i<=y;i++)
{
std::cout<<endl<<x[i][t];
}
}
t_cal = clock() - t_cal;
std::cout<<"It took me seconds" <<((float)t_cal)/CLOCKS_PER_SEC<<"seconds";

return 0;
}

Anda mungkin juga menyukai