C++, Laplace Equation in a 20*20 array

Click For Summary

Discussion Overview

The thread discusses a C++ implementation of solving the Laplace equation using an iterative technique on a 20x20 array. Participants explore issues related to array indexing, variable declarations, and code functionality, as well as formatting output for better readability.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach to solving the Laplace equation, detailing the use of three arrays for values, new values, and differences.
  • Another participant questions the choice of compiler and operating system, suggesting the problem lies within the code.
  • Some participants point out that the code resembles C more than C++, highlighting the importance of variable declaration and array indexing conventions.
  • Concerns are raised about the loop indices starting from 1 instead of 0, with some arguing that this could lead to confusion.
  • A participant mentions successfully resolving the initial code issues by redefining arrays to the correct size and adjusting loop counts.
  • There is a suggestion to use CSV format for output files to improve readability in spreadsheet applications.
  • Some participants discuss the appropriateness of using floats versus doubles in the code, with differing opinions on the need for container classes versus arrays.
  • One participant proposes encapsulating the arrays in a class for better code reusability.

Areas of Agreement / Disagreement

Participants express differing views on coding practices, such as array indexing and variable types. There is no consensus on the best approach to structuring the code or the necessity of using C++ features over C-style arrays.

Contextual Notes

Some participants note that the original code's structure may lead to confusion due to the unconventional indexing and variable declarations. The discussion also highlights the potential for misunderstanding regarding array indexing in different programming languages.

Who May Find This Useful

Readers interested in C++ programming, numerical methods for solving differential equations, and best practices in coding may find this discussion relevant.

SolStis
Messages
10
Reaction score
0
The code should solve laplace equation through an iterative technique until values change less than the specified tollerence, in this case maxdiff. I've used 3 arrays. one to store all values including initial and boundary conditions, and 2 more to store the new values and differences between the old values. The code then sums all values in the differences array, diff[j], and the calculates the average to confirm that allvalues in the array have changed by more or less than the tollerence. The code compiles ok but then performs an illegal operation, can anyone help?

Code:

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main() {

//Declaration of variables
float V[20][20], V0[18][18], diff[18][18];
int i, j;


for (i=0;i<=19;i++) {
for (j=0;j<=19;j++) {
V[0][j] = 0.0;
V[19][j] = 1.0;
V[0] = (i)/19.0;
V[19] = (i)/19.0;
}
}
for (i=1; i<=18; i++) {
for (j=1; j<=18; j++) {
V[j]=1.0;
}
}

float maxdiff, sum, diffsum;
int count=0;

for(i=1;i<=18;i++) {
for(j=1; j<=18; j++) {
diff[j]=1.0;
}
}


maxdiff=0.0001;
diffsum=1.0;

while (diffsum >= maxdiff) {
sum = 0.0;
for (i=1; i<=18; i++) {
for (j=1; j<=18; j++) {
V0[j] = V[j];
V[j] = ((V[i+1][j] + V[i-1][j] + V[j+1] + V[j-1])/4.0);
diff[j] = (V0[j] - V[j]);
sum = sum + diff[j];
}
}
diffsum=sum/(18 * 18);
count++;
}
count << count;

}
 
Technology news on Phys.org
What compiler, what operating system, are you sure problem is within your code. It compiles and runs here (Visual C++, Windows).
 
C++? Looks more like C with an element of C++
 
There's a reason why in C++ (unlike C) you are allowed & recommended to declare variables just before you need to use them, and not right at the start miles away from where your code is. There's also a reason why in loops, it is normal to count from [0, n) where your array is size n. It's easy to see you not gone over that way.

Now to the code:
Code:
    float V[20][20], V0[18][18], diff[18][18];

//...

    for(i=1;i<=18;i++) { 
        for(j=1; j<=18; j++) {
            diff[i][j]=1.0; // <--- What's wrong here?
        }
    }

//...

        for (i=1; i<=18; i++) {
            for (j=1; j<=18; j++) {
                V0[i][j] = V[i][j]; // <--- here?
                //...
                diff[i][j] = (V0[i][j] - V[i][j]); // <--- here?
                sum = sum + diff[i][j]; // <--- and here?
 
Hi, Thanks for the replies.

OS: XP SP2, Used qunicy 2005 to compile.

I managed to sort the problem out, redefiend the Arrays V0 and diff to 20 20.

Also in reply to the question about the loop counts, I counted from i=1 to 18 in calculating the V0 and diff arrays so the values were directly comparable to the original array V.

Code now works fine.

Another question though, is there a tagging format i can use to format an output file as an xls doc so i can get rows and and columns formated correctly and not just as spaces and endls within a txt doc?

Thanks again

Sol
 
Why don't you use CSV (comma separated values)?

The sooner you learn to start your indices at 0, the better for you.
 
Have managed to figure that 1 aswell, got some nice surface plots out.
Thanks for the help
 
As far as I know, all arrays in any given programming language are always assumed to start from 0. It might seem confusing at first, but it'll sort out a lot of confusion as well.
 
computerex said:
As far as I know, all arrays in any given programming language are always assumed to start from 0. It might seem confusing at first, but it'll sort out a lot of confusion as well.

No. In Pascal you may have

Code:
realtable : array[10..100] of real;

There is no such element as realtable[0].
 
  • #10
His loops run from 1 to 18 because his 20x20 array also contains the boundary conditions (those occupy all elements a[0][j], a[19][j], a[0], and a[19]). Notice that inside his loop, he accesses elements i-1 and i+1.

What the loop COULD use, is a comment explaining what it's doing and why it doesn't go from 0 to 19 like you would expect.
 
  • #11
It's also quite strange to see floats being defined. I can't recall the last time I saw C++ code using them.

By the way, since you've now seemingly sorted out what was causing you problems, it's of little harm to mention that there's nothing in the code that couldn't be accomplished just as easily with C. If you wanted to write the code properly in C++ you'd be much better off using an appropriate container, say, std::vector<std::vector<double >>, and working with the containers using typedef'd iterators. It won't add much in the way of speed, but the code will at least be reusable. As it stands, the code you're using is a bloody mess and you won't have a clue what you've done if you ever need to revisit it in the future.
 
Last edited:
  • #12
Borek said:
computerex said:
As far as I know, all arrays in any given programming language are always assumed to start from 0. It might seem confusing at first, but it'll sort out a lot of confusion as well.

No. In Pascal you may have
Or associative arrays in languages like PHP etc.

Ben Niehoff said:
His loops run from 1 to 18 because his 20x20 array also contains the boundary conditions (those occupy all elements a[0][j], a[19][j], a[0], and a[19]). Notice that inside his loop, he accesses elements i-1 and i+1.

What the loop COULD use, is a comment explaining what it's doing and why it doesn't go from 0 to 19 like you would expect.

You missed the part of his declaration where V0 & diff was both 18x18 and all the lines I highlighted where the counter went [1, 18].
 
  • #13
If he doesn't need double precision, then why declare doubles? That's just a waste of resources.

I don't see why he needs container classes, either. In fact, arrays seem more appropriate here. Semantically, he is NOT working with a vector of vectors, but with a field defined over 2-dimensional space. The best way to make this code reusable (in my opinion) would be to encapsulate the arrays in a ScalarField class.
 
  • #14
KTC said:
You missed the part of his declaration where V0 & diff was both 18x18 and all the lines I highlighted where the counter went [1, 18].

Yeah, I didn't see that he had declared his other arrays as 18x18. Whoops. :P
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K