Calculation error

Hi,

I'm trying to calculate values of array b and d.

I have checked that all the values which are assigned to arrays a and b are positive values.

But when I try to calculate using this loop the values of o is alternatively changing from positive to negative.

there is some kind of error within this part that I'm unaware.

Please guys help me to fix this error.

1
2
3
4
5
6
7
   for(int i=3;i<j;i++)
  {  
          o=a[i]/b[i-1];
          b[i]=b[i]-(o*c[i-1]);
          d[i]=d[i]-(o*d[i-1]);
  }
Last edited on
You show the code, which is your proposed solution. But you didn't state what the original problem was, so it's not possible to know whether this is correct or not.
Hi Chervil,

I have posted you entire code.

I tried to fix this bug. it works well till line 52 .

The problem is with array b[i-1]. when i try to calculate and print b[i-1].

It prints alternative negative numbers. But I didn't assign any negative values to any of arrays.

I don't understand how does it prints a negative value for b[i-1].

But if I'm taking b[i] instead of b[i-1]. It shows perfect value which has to be same for b[i-1] also.

please kindly go through the code. Let me know where is the error.

Thanks in Advance


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
#include <iostream>
#include <conio.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <sstream>

using namespace std;

int main()
{
    double dt;
    int j=41,n=45,k;
    double uy[45],y[45],a[n],b[n],c[n],d[n];
    double dy=0.001,mu=0.000217,f;
    float o;
    cout<<"Enter time step interval of your choice : ";
    cin>>dt;
    
         for(int i=1;i<=j;i++)
            if (i==1)
                {   
                    y[i]=40;
                }
                else
                { y[i]=0;
                }

f=mu*dt/(dy*dy);
  
  for(int i=2;i<j;i++)
  {
          if (i==2)
          {
             a[i]=f;
             b[i]=(-2*f)+1;
             c[i]=f;
             d[i]=-1-(a[i]*y[i-1]); 
          }else if (i==j-1)
          {
             a[i]=f;
             b[i]=(-2*f)+1;
             c[i]=f;
             d[i]=-1-(a[i]*y[i+1]);    
          }else
          {     
             a[i]=f;
             b[i]=(-2*f)+1;
             c[i]=f;
             d[i]=-1;
          } 
  }
         
  for(int i=3;i<j;i++)
  {  
          o=a[i]/b[i-1];
          b[i]=b[i]-(o*c[i-1]);
          d[i]=d[i]-(o*d[i-1]);         
  }
                      
y[j-1]=d[j-1]/b[j-1];


for (int i=j-2;i>1;i--)
{
  y[i]=(d[i]-(c[i]*y[i+1]))/b[i];

}
     
cout<<"CALCULATION COMPLETE !!!!";        
getch();
}



@Vigii,

I noticed these things:

On line 29 dt is not initialised, producing garbage values for f, and for all the values in the a array.

On lines 36, 42, 48 b[i] is always negative by assignment, but could be positive given the garbage value for f.

Btw, I think you should always use doubles, not floats - mixing the two can cause problems, and there is no need to use floats (presumably not processing billions of them).

Hope all goes well.
Hi,

You can see dt is read as user input on line 18.

and also the value is non-negative yielding 0.132 as a value for b[i].

Please kindly check it.
Sorry, my bad.

I still think you should use doubles exclusively.

The only other suggestion I have is to use the debugger to deduce where things go wrong. using the debugger can save one heaps of time tracking down run time errors.

Hope all goes well.
Topic archived. No new replies allowed.