How to find the difference between each iteration level

Hi,

I'm writing a code for a type of numerical method solution. I got stuck at a point of implementing the check expression while the iteration is carried out in the program. The objective of the present problem is that I have to stop iterations from moving ahead and should display result. Help me in with the desirable syntax and structure of the loop that I need to follow in achieving my result.
The problem has to be fixed at the end by fixing it with necessary syntax and structure of loop.

Please help me and your help is needful.


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
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;

int main()
{
/*Grid Point declarations*/
    int x,y;
    double dx,dy;
    double left,right,top,bottom;
    double a[100][100];
    double phi[100][100],uphi[100][100];
    cout<<"Enter no. of grid points"<<endl<<"x:";
    cin>>x;
    cout<<"y:";
    cin>>y;
    cout<<"Enter Grid Size"<<endl<<"dx:";
    cin>>dx;
    cout<<"dy:";
    cin>>dy;

    
/*Read & Define Boundary Conditions*/  
    cout<<endl<<endl<<"Enter value for left:";
    cin>>left;
    for (int i=0;i<=x-1;i++)
    {
        phi[0][i]=left;
    }
    
    cout<<endl<<"Enter value for right:";
    cin>>right;
    for (int i=0;i<=x-1;i++)
    {
        phi[x-1][i]=right;
    }
    
    cout<<endl<<"Enter value for top:";
    cin>>top;
    for (int i=0;i<=x-1;i++)
    {
        phi[i][y-1]=top;
    }
    
    cout<<endl<<"Enter value for right:";
    cin>>bottom;
    for (int i=0;i<=x-1;i++)
    {
        phi[i][0]=bottom;
    }    

cout<<'\n';
cout<<"First Iteration values"<<endl<<'\n';   
    
/*First Guess it=1*/   

for (int i=1;i<=x-2;i++)
{
    for(int j=1;j<=y-2;j++)
    {
            phi[i][j]=(left+right+top+bottom)/4;
            cout<<i<<'\t'<<j<<'\t'<<phi[i][j]<<endl;
    }
} 
cout<<'\n';

/*    for (int i=0;i<=x-1;i++)
    {
        for (int j=0;j<=y-1;j++)
        {
            cout<<i<<'\t'<<j<<'\t'<<phi[i][j]<<endl;
        }
    }*/


/*Iteration from it=2*/   
int nit;
cout<<"Enter required iterations: ";
cin>>nit;
cout<<'\n';

                            
       for (int it=2;it<=nit;it++)
       {
           for (int i=1;i<=x-2;i++)
               {
                    for(int j=1;j<=y-2;j++)
                    {
                           phi[i][j]=(phi[i+1][j]+phi[i-1][j]+phi[i][j+1]+phi[i][j-1])/4;
                           cout<<it<<'\t'<<phi[i][j]<<endl;
                    }
               }  
       cout<<'\n';                       
       }
            
    getch();
}
On what line is the loop you're having trouble with and what specifically do you want it to do? I think you may just want to use a break or continue statement but your question is too vague (and I have no idea what your code is supposed to do and am not keen to spend 20 minutes figuring it out).
If the difference value of n+1 iteration and n th iteration is equal to 10^-3. then the program should stop and print the last iteration value.

I think I'm clear now in defining my problem.
Topic archived. No new replies allowed.