-2

I'm trying to solve a system of complex differential equation. I would like to fill the complex vector xi[n] with a gaussian valued function. But, when I check the output file, it gives me just a lot of zeros. I gave the input values using the cin function and it worked...what's the problem in this code????

using namespace std;       

int main()
{
  int  n;
  int tmax;
  int tt = 5000;                   // number of first-order equations 
  double ti, tf, dt, sigma,mu,z,q,N ;
  complex<double> xi[n], xf[n], eta[tt];
  double  j;
  int i, y,d;
  int m=0;

  ofstream file;
  file.open ("provavet11(om100(2g))).dat"); 

  printf("Number of equations\n");
  scanf("%d", &n);
  printf("Particles in central cav.\n");
  scanf("%d", &N);
  printf("Sigma\n");
  scanf("%d", &q);
  /* initial information */

  ti = 0.0;            
  // initial value for variable t
  for(y=0; y<=n-1; y++)
  {
    //scanf("%f\n", xi[y]);
    //cin >> xi[2*y]
    //   }
    xi[y]=  N*exp(-pow((y-(n-1)/2.),2)/(2*q*q));        
  }
Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
Gbp
  • 21
  • 3
  • 3
    `complex xi[n], xf[n], eta[tt];` You really expected this to work, yet alone compile? `n` is not initialized, and even if it is initialized, it is not legal C++ to declare arrays using a variable as the number of items. – PaulMcKenzie Apr 18 '15 at 17:30
  • 1
    It might work with gcc, but obviously it won't work at runtime since `n` has garbage. However, even if it did work, it would put the memory on the stack, which would not be a good thing for this sort of problem. Definitely should be `std::vector>`. – sfjac Apr 18 '15 at 17:38

1 Answers1

0

There are multiple things that are at issue with your code.

First, you're using n before it is initialized. It could be 0, 10, 323432, -234, who knows.

int  n;   
complex<double> xi[n], xf[n], eta[tt];  // <-- n is not initialized

Second, even if n were initialized, it is not legal C++ to declare arrays using a variable as the number of entries. In C++, a "varying array" is accomplished by using std::vector.

#include <vector>

int main()
{
    int  n;   
    int tmax;
    int tt = 5000;                   // number of first-order equations 
    double ti, tf, dt, sigma,mu,z,q,N ;
    std::vector<complex<double>> xi, xf, eta(tt);
    //...
    // Once `n` is read in and initialized:
    xi.resize(n);
    xf.resize(n);
    //...
}

The third issue is that you commented out code that definitely would not have worked correctly, regardless of whether you used std::vector or not:

  for(y=0; y<=n-1; y++)
  {
    //scanf("%f\n", xi[y]);
    //cin >> xi[2*y]  // way out of bounds!

Assuming that you wanted to use the code you commented out, the value of 2*y when y exceeds n/2 well exceeds the bounds of your xi array (or vector). This is a memory overwrite. You need to ensure that xi is big enough to hold all the entries.

Edit:

Given the code you posted here: http://ideone.com/Eckq3Z

You should do two things:

1) Change the calls in the vector from reserve() to resize().

2) In the dnx function, pass the vector's by reference, not by value:

complex<double> dnx(double t, vector<complex<double> >& x, vector<complex<double> >& dx, int n)

For the second item, you are passing vector by value, meaning that the parameter is a temporary and will disappear as soon as the function returns.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Ok thanks(I'm new in C++, sorry). I have done all the correction you indicated, but still I'm not finding a way to fill my complex vector with the function xi[y]= N*exp(-pow((y-(n-1)/2.),2)/(2*q*q)); – Gbp Apr 18 '15 at 19:20
  • You need to state what you mean by "fill my complex vector". Does the code compile? If so, what are the values of each entry of x[i]? W hy not break up that long formula into several steps, so you can see what each step is doing. – PaulMcKenzie Apr 18 '15 at 21:28
  • Here is your code, with some variables renamed for clarity, as well as hardcoded values. http://ideone.com/572qrf So what part of that code is not working? If that isn't the code you're running, you need to explain in greater detail exactly what the input is, and what you expected as output. – PaulMcKenzie Apr 18 '15 at 21:40
  • http://ideone.com/Eckq3Z The code compile, but it does not return 0, so basically the computation fails. I wonder if I'm defining the functions correctly. – Gbp Apr 19 '15 at 18:32
  • Any ideas about that? – Gbp Apr 20 '15 at 15:33
  • @GiuseppeBuonaiuto `Compilation error#stdincompilation error#stdout 0s 0KB` That link you posted shows that there is no such include file as ``, so it isn't a "computation" error -- it is a compiler error. Fix the code so that it compiles cleanly. Also, there is no input data -- do as what I did -- hardcode values into the program. – PaulMcKenzie Apr 20 '15 at 15:39
  • http://ideone.com/s6bKgS if you look now, there is an actual computational error. It look like the runge kutta subroutine fails to readout the vector. I really don't get it – Gbp Apr 20 '15 at 16:25
  • It should be `resize()`, not `reserve()`. The code at the link I had should have been `resize()`, so you should change your code to do this. In any event, maybe you should look at how is used. http://en.cppreference.com/w/cpp/numeric/complex – PaulMcKenzie Apr 20 '15 at 16:31
  • Your updated code: http://ideone.com/5E3OiP I think you need to start debugging your code, along with the use of a calculator to see where *exactly* in the program the results differ than what you would if you used a calculator. – PaulMcKenzie Apr 20 '15 at 16:45
  • I did it, surely I expect to find non zero values for every time steps. Do you think the fuctions are correctly defined? – Gbp Apr 20 '15 at 17:14
  • @GiuseppeBuonaiuto - You didn't state exacty at what line, loop, etc. where the calculations start to differ between your computer program and a hand calculation of the values. If you can't calculate it by hand, you won't get far writing/debugging/testing a computer program that's supposed to do the same thing. – PaulMcKenzie Apr 20 '15 at 18:35
  • Since the second line on the generated file.Which means that the function dnx fails to be read in the runge algorhitm. I have done a debug but nothing came up – Gbp Apr 20 '15 at 22:20
  • Change those `reserve` calls to `resize` in the `dnx` function. – PaulMcKenzie Apr 20 '15 at 22:26
  • I updated the answer -- the issue with your `dnx` function is that you're passing vectors by value. They should be reference parameters, not value parameters. – PaulMcKenzie Apr 20 '15 at 22:32