Tell me more ×
Physics Stack Exchange is a question and answer site for active researchers, academics and students of physics. It's 100% free, no registration required.

I solved the following "coupled" ODE numerically, whit forward Euler scheme: $$ \frac{\text{d}N_a}{\text{d}t} = -\frac{N_a}{\tau_a} $$ $$ \frac{\text{d}N_b}{\text{d}t} = \frac{N_a}{\tau_a}-\frac{N_b}{\tau_b} $$

This is the result of my program with $\tau_a = \tau_b = 1$ and $N_a(0) = N_b(0) = 1000$ (image): http://s11.postimage.org/ci5r60g1d/decay.jpg

It is the correct behaviour (my simulation is correct)?

C++ code:

/*  R. M.
        21.08.2012
    Exercice 1.4 of Computational Physics, N. Giordano and H. Nakanishi
        Euler method to solve: dN_a/dt = -N_a/tau_a and dN_b/dt = N_a/tau_a - N_b/tau_b
*/

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

void calculate(
    std::vector<double>& time, 
    std::vector<double>& nuclei_a, 
    std::vector<double>& nuclei_b,
    const double t_max, 
    const double dt, 
    const double tau_a, 
    const double tau_b)
{
    const int iterations(t_max / dt);

    for(int i(1); i < iterations; i++)
    {
        time.push_back( time[i-1] + dt );
        nuclei_a.push_back( nuclei_a[i-1] - nuclei_a[i-1] * dt / tau_a);
        nuclei_b.push_back( nuclei_b[i-1] + nuclei_a[i-1] * dt / tau_a - nuclei_b[i-1] * dt / tau_b);
    }
}

void save(
    const std::vector<double>& time,
    const std::vector<double>& nuclei_a,
    const std::vector<double>& nuclei_b,
    const std::string& filename_a,
    const std::string& filename_b)
{
    std::ofstream file_out_a(filename_a);
    std::ofstream file_out_b(filename_b);

    for(int i(0); i < time.size(); i++)
    {
        file_out_a << time[i] << ' ' << nuclei_a[i] << std::endl;
        file_out_b << time[i] << ' ' << nuclei_b[i] << std::endl;
    }

    file_out_a.close();
    file_out_b.close();
}

int main()
{
    // Constants
    constexpr double t_max(10.);    // Time to end simulation
    constexpr double dt(0.01);      // Time step
    constexpr double tau_a(11.);        // Decay time constant (A nuclei)
    constexpr double tau_b(1.);     // Decay time constant (B nuclei)

    std::vector<double> time({0.});
    std::vector<double> nuclei_a({1000.});
    std::vector<double> nuclei_b({1000.});

    calculate(time,nuclei_a,nuclei_b,t_max,dt,tau_a,tau_b);
    save(time,nuclei_a,nuclei_b,"nuclei_a.dat","nuclei_b.dat");

    return 0;
}
share|improve this question
1  
Hi R.M., and welcome to Physics Stack Exchange! Questions like this where you simply ask if your result is correct aren't appropriate for this site. Besides, this is really a numerical computation question, not a physics question. If you have some reason to believe your solution is incorrect, then perhaps you could turn it into a question which would be appropriate on Computational Science or Stack Overflow. – David Zaslavsky Aug 21 '12 at 0:29
The exact solution to your system is found by solving the first equation: $N_a= N_a(0)e^{-t/\tau_a}$, and then using the ansatz $N_b = f(t)e^{-t/\tau_b}$ plugging in the solution for $N_a$. The result is a sum of exponentials, and is very simple, and it is contained in the general theory of solving first order linear differnetial equations which I am sure you can find on Wikipedia or elsewhere. – Ron Maimon Aug 21 '12 at 6:21
@RonMaimon I found the exercise in a computational physics book, so I thought that the ODE couldn't be solved analytically. Sorry. I will found the analytical solution and check if it correspond with my numerical simulation. – R. M. Aug 21 '12 at 7:13
@DavidZaslavsky I thought that asking if the graph seems right for the physical phenomena was a question to ask here. Sorry. – R. M. Aug 21 '12 at 7:15
@R.M.: Don't be discouraged by the close, the combination of detailed question with analytically solvable diff-eq made it just below the threshhold--- please feel welcome, and please ask more questions in the future. I hope the exact solution will be simple to find and understand. If it helps at all, your plot looks spot on for what is expected of this system, the red line is $N_a$, and is precisely exponential, while the green line is relaxing to the red line as appropriate. I can read off the graph that you made the parameters $\tau_a=\tau_b=1$, everything looks right for this choice. – Ron Maimon Aug 21 '12 at 7:20
show 1 more comment

closed as off topic by Qmechanic, David Zaslavsky Aug 21 '12 at 0:28

Questions on Physics Stack Exchange are expected to relate to physics within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.