G
G
Gleb2020-09-15 15:21:56
Mathematics
Gleb, 2020-09-15 15:21:56

Where is the error in the numerical solution of the equation?

Given the Cauchy problem: y'' + 4 y = 4 / sin(2x) , y (pi / 4) = 2, y' (pi / 4) = pi.
There is also a formula for solving the difference equation, I tried to write it and I feel I made a mistake in initializing the variables, mb who will tell you where and how?
Obviously, the U_n values ​​are displayed much less than the true ones.
5f60b16df37cd115620508.png

int main() {
    int i = 0;
    int n;

    double U[n], A[n] , B[n], C[n];

    U[0] = 2;
    U[1] = M_PI;

    A[n] = 1, B[n] = 4, C[n] = 0;

    double N = 50;
    double h = 0.1;
    double x = N * h;
    n = 1 / N;
    
    double fu[n];
    fu[0] = 4 / sin(2*x);

    while (i < n) {
        U[n] = 1 / (A[n] + (B[n] * h) / 2) * ( (h*h) * fu[n] - U[n-1] * ( C[n] *
                                (h*h) - 2*A[n])- U[n-2] * (A[n] - U[n-2] *
                                        (A[n] - (B[n]*h)/ 2)));
        i++;
    }

    std::cout << U[3] << std::endl ;
    std::cout << x ;

    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2020-09-15
@shevzoom

n forgot to initialize

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question