How to call imaginary number "i" in an equation in C++?

107 Views Asked by At

I want to solve the differential equation,

idy/dx=x 

using Euler method in C++, but i am not able to put i(iota) in the front of the equation. My code is:

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <complex>

using namespace std;
using namespace std::complex_literals;
int main() {
    std::complex<double> z = 1i;
    std::complex<double> y[100];

    int j;
    float h = 0.1, x[100];
    y[0]    = 0.0;
    x[0]    = 0.0;

    for (j = 0; j <= 10; j++) {
        y[j + 1] = y[j] + h * imag(z) * x[j];
        x[j + 1] = x[j] + h;
        cout << x[j] << "\t" << y[j] << endl;
    }
}

My output is:

0   (0,0)
0.1 (0,0)
0.2 (0.01,0)
0.3 (0.03,0)
0.4 (0.06,0)
0.5 (0.1,0)
0.6 (0.15,0)
0.7 (0.21,0)
0.8 (0.28,0)
0.9 (0.36,0)
1   (0.45,0)

When i run this code, the solution comes out to be real numbers only, however, the actual solution to the above differential equation is imaginary. I want to ask, is there any way I can write this i(iota) in my equation in the code.

0

There are 0 best solutions below