C++ vs MATLAB syntax: Incorrect results

This is a pretty simple question, I am not sure what I am missing.

MATLAB code:
1
2
3
4
5
  Nx = 10;  
  Ny = 10; 
  Lx = 2*pi; 
  ygl = -cos(pi*(0:Ny)/Ny)';
  VGL = cos(acos(ygl(:))*(0:Ny)); 


It appears the problem in my C++ code is I am not indexing correctly, so instead I get the value cos(acos(ygl(:))) repeating instead of a matrix.

C++ code:
1
2
3
4
5
6
7
8
for(int i = 0; i< nx+1; i++){
		for(int j = 0; j< ny+1; j++){
			ygl[j] = -1. * cos(((j) * M_PI )/ny);
			VGL[j + ny*i] = cos( acos(ygl[j]) ); 
			cout << VGL[j + ny*i] << endl ; //test	
			
		}		
	}
Last edited on
Your definition of VGL[j+ny*i] has no dependence on i on the RHS.

BTW, your MATLAB code would be wrong if Nx wasn’t equal to Ny.
@lastchance wow, you're right I have no idea how I couldn't see it. Thanks!!
Topic archived. No new replies allowed.