Variable not being asigned well

Hi, I am kind of new in c++, all the problems I encountere I tried to solve them by reading webpages and posts in this forum, but I haven't been able to solve this problem. In "void solveBetaY" where there are a lot of couts we can see that the first cout shows the valors of a,b,c and d. The next cout should show the same valor as d, as it is a copy and paste, but strangely it doesn't show the same valor.

The valor of d should be the same for each j, but it isnt, it seems that the valor is not being assigned well. How can I solve this?

Thank you for your help

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  #include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;


const double mu = 0.00002094;         //kind of nice
const double Wo = 0.032;            //kind of nice
const double Wf = 0.1;                //kind of nice
const double Wp = 109.0 / 3750.0;             //kind of nice
const double Wn = 0.028;                //kind of nice
const double Q = 4.82*pow(10, 7);        //kind of nice
const double L = 318000.0;               //kind of nice
const double cpf = 1;
const double cpo = 1;
const double cpp = 1;
const double cpn = 1;
const double Tref = 600.0;
const double Ts = 371.5;
const int nodesx = 1;
const int nodesy = 10;
const int timesteps = 2;
const int buclesmax = 2;
const double deltax = 0.00001;
const double deltay = 0.00001;
const double deltat = 0.0000001;
double ro[nodesx + 2][nodesy + 2][timesteps + 1];
double u[nodesx + 2][nodesy + 2][timesteps + 1];
double v[nodesx + 2][nodesy + 2][timesteps + 1];
double P[nodesx + 2][nodesy + 2][timesteps + 1];
double Yf[nodesx + 2][nodesy + 2][timesteps + 1];
double Yn[nodesx + 2][nodesy + 2][timesteps + 1];
double alpha[nodesx + 2][nodesy + 2][timesteps + 1];
double beta[nodesx + 2][nodesy + 2][timesteps + 1];
double a[nodesx + 2];
double b[nodesx + 2];
double c[nodesx + 2];
double d[nodesx + 2];

void TDMAy(double(&phi)[nodesx + 2][nodesy + 2][timesteps + 1], double a[nodesy], double b[nodesy], double c[nodesy], double d[nodesy], int x, int t){

	double e[nodesy + 1];
	double f[nodesy + 2];

	e[0] = c[0] / b[0];
	f[0] = d[0] / b[0];

	for (int i = 1; i<nodesy + 1; i++){
		e[i] = c[i] / (b[i] - e[i - 1] * a[i]);
		f[i] = (d[i] - f[i - 1] * a[i]) / (b[i] - e[i - 1] * a[i]);
	}

	f[nodesy + 1] = (d[nodesy + 1] - f[nodesy] * a[nodesy + 1]) / (b[nodesy + 1] - e[nodesy] * a[nodesy + 1]);

	phi[x][nodesy + 1][t] = f[nodesy + 1];
	//cout << phi[x][nodesy + 1][t] << endl;
	/*if (phi == v){
	cout << phi[x][nodesy + 1][t] << endl;
	cout << "a = " << a[nodesy + 1] << " b = " << b[nodesy + 1] << " c = " << c[nodesy + 1] << " d = " << d[nodesy + 1] << endl << endl;
	cout << "flag " << flag[nodesy+1] << endl;
	}*/

	for (int i = nodesy; i >= 0; i--){
		phi[x][i][t] = f[i] - e[i] * phi[x][i + 1][t];
		//cout << phi[x][i][t] << endl;
		/*if (phi == v){
		cout << "[" << x << "][" << i << "][" << t << "] " << phi[x][i][t] << endl;
		cout << "a = " << a[i] << " b = " << b[i] << " c = " << c[i] << " d = " << d[i] << endl ;
		cout << "flag " << flag[i] << endl<<endl;
		}*/
	}
	//cout << endl << endl << "estoy en el TDMAy" << endl << endl;
	//if (phi == v){ system("pause"); }
	return;
}

void solveBetaY(int i, int ts){
	b[0] = 1;
	c[0] = 0;
	d[0] = 1;
	b[nodesx + 1] = 1;
	a[nodesx + 1] = 0;
	d[nodesx + 1] = 1;
	for (int j = 1; j < nodesy + 1; j++){

		if (u[i][j][ts] >= 0 && v[i][j][ts] >= 0){

			a[j] = -(ro[i][j - 1][ts] * v[i][j - 1][ts] / deltay) - (mu) / (deltay*deltay);


			b[j] = (ro[i][j][ts] / deltat) + (ro[i][j][ts] * v[i][j][ts] / deltay) + (2 * mu) / (deltay*deltay) + (ro[i][j][ts] * u[i][j][ts] / deltax) + (2 * mu) / (deltax*deltax);


			c[j] = -(mu) / (deltay*deltay);


			d[i] = (ro[i][j][ts - 1] * beta[i][j][ts - 1] / deltat) + (ro[i - 1][j][ts] * u[i - 1][j][ts] * beta[i - 1][j][ts] / deltax) + mu*(beta[i + 1][j][ts] + beta[i - 1][j][ts]) / (deltax*deltax) + (P[i][j][ts] - P[i][j][ts - 1]) / (deltat) +(mu / 3)* ((u[i + 1][j][ts] * u[i + 1][j][ts] + u[i - 1][j][ts] * u[i - 1][j][ts] - 2 * u[i][j][ts] * u[i][j][ts]) / (deltax*deltax) + (v[i][j + 1][ts] * v[i][j + 1][ts] + v[i][j - 1][ts] * v[i][j - 1][ts] - 2 * v[i][j][ts] * v[i][j][ts]) / (deltay*deltay) + (u[i + 1][j][ts] * (v[i + 1][j + 1][ts] - v[i + 1][j - 1][ts]) + v[i + 1][j][ts] * (u[i + 1][j + 1][ts] - u[i + 1][j - 1][ts]) - u[i - 1][j][ts] * (v[i - 1][j + 1][ts] - v[i - 1][j - 1][ts]) - v[i - 1][j][ts] * (u[i - 1][j + 1][ts] - u[i - 1][j - 1][ts])) / (4 * deltax*deltay)) + (5 * mu / 3)*((u[i][j + 1][ts] - u[i][j - 1][ts])*(v[i + 1][j][ts] - v[1 - 1][j][ts]) / (4 * deltax*deltay) - (u[i + 1][j][ts] - u[i - 1][j][ts])*(v[i][j + 1][ts] - v[i][j - 1][ts]) / (4 * deltax*deltay));
			cout << "a [" << j << "= " << a[j] << endl << "b [" << j << "= " << b[j] << endl << "c [" << j << "= " << c[j] << endl << "d [" << j << "= " << d[j] << endl;
			cout << (ro[i][j][ts - 1] * beta[i][j][ts - 1] / deltat) + (ro[i - 1][j][ts] * u[i - 1][j][ts] * beta[i - 1][j][ts] / deltax) + mu*(beta[i + 1][j][ts] + beta[i - 1][j][ts]) / (deltax*deltax) + (P[i][j][ts] - P[i][j][ts - 1]) / (deltat)+(mu / 3)* ((u[i + 1][j][ts] * u[i + 1][j][ts] + u[i - 1][j][ts] * u[i - 1][j][ts] - 2 * u[i][j][ts] * u[i][j][ts]) / (deltax*deltax) + (v[i][j + 1][ts] * v[i][j + 1][ts] + v[i][j - 1][ts] * v[i][j - 1][ts] - 2 * v[i][j][ts] * v[i][j][ts]) / (deltay*deltay) + (u[i + 1][j][ts] * (v[i + 1][j + 1][ts] - v[i + 1][j - 1][ts]) + v[i + 1][j][ts] * (u[i + 1][j + 1][ts] - u[i + 1][j - 1][ts]) - u[i - 1][j][ts] * (v[i - 1][j + 1][ts] - v[i - 1][j - 1][ts]) - v[i - 1][j][ts] * (u[i - 1][j + 1][ts] - u[i - 1][j - 1][ts])) / (4 * deltax*deltay)) + (5 * mu / 3)*((u[i][j + 1][ts] - u[i][j - 1][ts])*(v[i + 1][j][ts] - v[1 - 1][j][ts]) / (4 * deltax*deltay) - (u[i + 1][j][ts] - u[i - 1][j][ts])*(v[i][j + 1][ts] - v[i][j - 1][ts]) / (4 * deltax*deltay)) << endl;
			cout << endl << (ro[i][j][ts - 1] * beta[i][j][ts - 1] / deltat) << endl;
			cout << endl << (ro[i - 1][j][ts] * u[i - 1][j][ts] * beta[i - 1][j][ts] / deltax) << endl;
			cout << endl << mu*(beta[i + 1][j][ts] + beta[i - 1][j][ts]) / (deltax*deltax) << endl;
			cout << endl << (P[i][j][ts] - P[i][j][ts - 1]) / (deltat) << endl;
			cout << endl << (mu / 3)* ((u[i + 1][j][ts] * u[i + 1][j][ts] + u[i - 1][j][ts] * u[i - 1][j][ts] - 2 * u[i][j][ts] * u[i][j][ts]) / (deltax*deltax) + (v[i][j + 1][ts] * v[i][j + 1][ts] + v[i][j - 1][ts] * v[i][j - 1][ts] - 2 * v[i][j][ts] * v[i][j][ts]) / (deltay*deltay) + (u[i + 1][j][ts] * (v[i + 1][j + 1][ts] - v[i + 1][j - 1][ts]) + v[i + 1][j][ts] * (u[i + 1][j + 1][ts] - u[i + 1][j - 1][ts]) - u[i - 1][j][ts] * (v[i - 1][j + 1][ts] - v[i - 1][j - 1][ts]) - v[i - 1][j][ts] * (u[i - 1][j + 1][ts] - u[i - 1][j - 1][ts])) / (4 * deltax*deltay)) + (5 * mu / 3)*((u[i][j + 1][ts] - u[i][j - 1][ts])*(v[i + 1][j][ts] - v[1 - 1][j][ts]) / (4 * deltax*deltay) - (u[i + 1][j][ts] - u[i - 1][j][ts])*(v[i][j + 1][ts] - v[i][j - 1][ts]) / (4 * deltax*deltay)) << endl;
			system("pause");
		}
		
		else {

			a[j] = 0;


			b[j] = 0;


			c[j] = 0;


			d[i] = 0;
		}

	}

	TDMAy(beta, a, b, c, d, i, ts);
	return;
}

int _tmain(int argc, _TCHAR* argv[])
{
	for (int i = 0; i < nodesx + 2; i++){    //condicions inicials
		for (int y = 0; y < nodesy + 2; y++){
			for (int t = 0; t < timesteps + 1; t++){
				u[i][y][t] = 1;
				v[i][y][t] = 1;
				P[i][y][t] = 100000;

				Yf[i][y][t] = 0;   //1
				Yn[i][y][t] = 329.0 / 429.0;
				alpha[i][y][t] = -vr * (100.0 / 429.0);


				beta[i][y][t] = 1; 

				ro[i][y][t] = 1;
				
			}			
		}
	}
	solveBetaY(1, 1);
	for (int i = 0; i < nodesy + 2; i++){
		cout << beta[1][i][1] << endl;
	}
	system("pause");
}
The valor of d should be the same for each j,

You don't assign anything to d[j]. On the other hand, you do assign to d[i] on lines 97 and 119.

(By the way, it is value, not valor.)
Thanks a lot for your help, I can't believe the error was s stupid, hours of checking for this hahahaha.

Thank you very much again! I apreciate it a lot :D
Topic archived. No new replies allowed.