Hi,
this is my first post. I'll try to keep it short.
Basically, I'm trying to solve a one-way wave equation using finite differences. I'm trying to put the values in an multidim. array, where a column represents a time step.
The first column is the initial condition. I calculate the second column using entries from the first. For some reason, this changes the first column. I do not understand this.
Hope someone has the time to help. Thanks!
I first initialize the variables needed:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int xMeshPoints = 20; // number of points in x-direction.
int tMeshPoints = 1; // number of points in t-direction.
double dt = 0.001;
double dx = double(2)/(xMeshPoints); // function defined on [0,2].
double a = 1;
double v [xMeshPoints][tMeshPoints];
Putting entries in first column, and calculating second column using entries from first:
// Initial condition (t=0):
for (int i=0; i<=xMeshPoints; i++)
{
if (dx*i >= 0.5 && dx*i <= 1)
v[i][0] = 2;
else
v[i][0] = 1;
}
// Print first column:
cout << "First column:" << endl;
for (int i=0; i<=xMeshPoints; i++)
{
cout << v[i][0] << endl;
}
// Calculate second column:
for (int i=0; i<xMeshPoints; i++)
{
v[i][1] = v[i][0] - (a*dt/dx)*(v[i+1][0]-v[i][0]);
}
// Print first column (again):
cout << "First column (again):" << endl;
for (int i=0; i<=xMeshPoints; i++)
{
cout << v[i][0] << endl;
}
return 0;
}
Output:
First column:
1
1
1
1
1
2
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
First column (again):
1
1
1
1
1
0.99
0.9799
0.969699
0.959396
0.94899
0.94848
0.947965
0.947444
0.946919
0.946388
0.945852
0.94531
0.944763
0.944211
0.943653
0.94309