Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Aug 4, 2008 #1
    The code should solve laplace equation through an iterative technique untill values change less than the specified tollerence, in this case maxdiff. Ive 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 any one 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++;
    }
    cout << count;

    }
     
  2. jcsd
  3. Aug 4, 2008 #2

    Borek

    User Avatar

    Staff: Mentor

    What compiler, what operating system, are you sure problem is within your code. It compiles and runs here (Visual C++, Windows).
     
  4. Aug 4, 2008 #3
    C++? Looks more like C with an element of C++
     
  5. Aug 4, 2008 #4

    KTC

    User Avatar

    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 (Text):
        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?
     
     
  6. Aug 4, 2008 #5
    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
     
  7. Aug 4, 2008 #6

    Borek

    User Avatar

    Staff: Mentor

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

    The sooner you learn to start your indices at 0, the better for you.
     
  8. Aug 4, 2008 #7
    Have managed to figure that 1 aswell, got some nice surface plots out.
    Thanks for the help
     
  9. Aug 4, 2008 #8
    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.
     
  10. Aug 4, 2008 #9

    Borek

    User Avatar

    Staff: Mentor

    No. In Pascal you may have

    Code (Text):
    realtable : array[10..100] of real;
    There is no such element as realtable[0].
     
  11. Aug 4, 2008 #10

    Ben Niehoff

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  12. Aug 4, 2008 #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: Aug 4, 2008
  13. Aug 4, 2008 #12

    KTC

    User Avatar

    Or associative arrays in languages like PHP etc.


    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].
     
  14. Aug 4, 2008 #13

    Ben Niehoff

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  15. Aug 4, 2008 #14

    Ben Niehoff

    User Avatar
    Science Advisor
    Gold Member

    Yeah, I didn't see that he had declared his other arrays as 18x18. Whoops. :P
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++, Laplace Equation in a 20*20 array
Loading...