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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

//Including necessary files
#include<iostream>
#include<iomanip>
#include<fstream>
#include<math.h>
#define nu 0.00001
#define h 0.1
#define U 100
using namespace std;
ofstream mout("A8_output.doc"); //mout prints to word file
ofstream xlsout("A8_comparison.xls"); //xlsout prints to excel file
void ftcs(double t,double y);
void implicit(double t,double y);
void crank_nicholson(double t,double y);
main()
{
//Declare necessary variables
int i,j,prog=1;
double t,y;
char change,wish;
//Naming the program and displaying the partial differential equation
cout<<"Programme to solve partial differential equation :";
cout<<"\n\ndu/dt = nu * d^2(u)/dy^2";
mout<<"Programme to solve partial differential equation :";
mout<<"\n\ndu/dt = nu * d^2(u)/dy^2";
while (prog==1)
{
t=0.1;
for (i=0;i<3;i++)
{
y=0.01;
for (j=0;j<2;j++)
{
ftcs(t,y);
implicit(t,y);
crank_nicholson(t,y);
y=y/10;
}
t=t/10;
}
cout<<"\n\nDo you want to try other delta_t and delta_y values?";
cout<<"\nIf yes then press 'y' and then press enter to continue";
cout<<"\nElse press any other key and then press enter to continue.\n\n";
cin>>change;
mout<<"\n\nDo you want to try other delta_t and delta_y values?";
mout<<"\nIf yes then press 'y' and then press enter to continue";
mout<<"\nElse press any other key and then press enter to continue.\n\n";
while (change=='y')
{
cout<<"\n\nEnter the value of delta_t : ";
//Loop to get a positive number
do
{
//Verifying whether entered value is a number
while (!(cin>>t))
{
cout<<"\n\nThe value entered is not a number, please try again : ";
cin.clear();//Clearing error flag
cin.ignore(10000,'\n');//Ignoring previously entered nonnumber value
}
//Checking for negative value
if (t<=0)
cout<<"\n\nPlease enter a positive number : ";
}
while (t<=0);
mout<<"\n\nEnter the value of delta_t : ";
mout<<t;
cout<<"\n\nEnter the value of delta_y : ";
//Loop to get a positive number
do
{
//Verifying whether entered value is a number
while (!(cin>>y))
{
cout<<"\n\nThe value entered is not a number, please try again : ";
cin.clear();//Clearing error flag
cin.ignore(10000,'\n');//Ignoring previously entered nonnumber value
}
//Checking for negative value
if (y<=0)
cout<<"\n\nPlease enter a positive number : ";
}
while (y<=0);
mout<<"\n\nEnter the value of delta_y : ";
mout<<y;
ftcs(t,y);
implicit(t,y);
crank_nicholson(t,y);
cout<<"\n\nDo you want to try other delta_t and delta_y values?";
cout<<"\nIf yes then press 'y' and then press enter to continue";
cout<<"\nElse press any other key and then press enter to continue.\n\n";
cin>>change;
mout<<"\n\nDo you want to try other delta_t and delta_y values?";
mout<<"\nIf yes then press 'y' and then press enter to continue";
mout<<"\nElse press any other key and then press enter to continue.\n\n";
}
//Asking for rerunning the program
cout<<"\n\nDo you want to rerun the programme?";
cout<<"\nIf yes press 'y' and then press enter";
cout<<"\nElse press anyother key and then press enter to exit.\n\n";
//Store user's wish
cin>>wish;
mout<<"\n\nDo you want to rerun the programme?";
mout<<"\nIf yes press 'y' and then press enter";
mout<<"\nElse press anyother key and then press enter to exit.\n\n";
mout<<wish;
if (wish=='y')
prog=1;
else
prog=0;
}
}
void ftcs(double f_t,double f_y)
{
//cout<<"\n\nt = "<<f_t<<"\ty = "<<f_y;
int i,j,t,y;
double n_t,n_y,constant;
constant=nu*f_t/(f_y*f_y);
n_t=1/f_t;
n_y=h/f_y;
t=int(n_t+0.5);
y=int(n_y+0.5);
double u[t][y];
for (i=0;i<=t;i++)
{
cout<<"\ni="<<i<<" j="<<y;
u[i][y]=U;
cout<<"\t"<<u[i][y];
}
for (i=0;i<y;i++)
u[0][i]=0;
}
void implicit(double i_t,double i_y)
{
//cout<<"\n\n"<<i_t<<"\t"<<i_y;
}
void crank_nicholson(double cs_t,double cs_y)
{
//cout<<"\n\n"<<cs_t<<"\t"<<cs_y;
}
 