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

You are accessing an element of your array that doesn't exist by using "<=" in multiple places.

an array of size 128,

starting at zero (0)

is from 0 to 127

for(int i = 0; i < 128; i++)

Debugging with a decent compiler would tell you the stack around "v" was corrupted, meaning you are accessing an element of your array that doesn't exist.

// 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]);//<--
//is v[i+1][0] exist when i < xMeshPoints ??
  }

Hi pseudorandom21, thanks for answering my question.
I increased the size of my array by 1, and things are working as expected.

You say that debugging with a decent compiler would help me catch these mistakes. I'm compiling using gcc for windows. That is, I compile by using g++ in the command window. Why doesn't this compiler catch these things?

Again, thanks!

There is probably a debug mode for GCC, you'll have to google the right argument for that.

Debug modes will normally run your application under the control of the debugger.

Alternatively visual studio 2010 express is free, then you just hit "F5".

http://www.microsoft.com/express/Downloads/

Thanks, will look into it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.