APS Excellence in Physics Education Award
November 2019

Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

First Order ODE solution with ODE interface and ODEsolver post and replies

Return to the First Order ODE solution with ODE interface and ODEsolver thread
Login to post to this thread

Simulation of nucleus decay by ODEsolver
Martin Jola
7 Posts

To learn more about ODE interface and ODEsolver I tired to solve the ODE of nuclear decay:
dN/dt=-lambda*N. Analyitical solution is easy, but to learn ODE/ODEsolver I would like to solve it numerically. I followed the example of FallingParticle in the CMS book, but failed. It's not a proble to compile the code, no error messages and the control panle opens with the correct vaiables. However when I press calculate I get a lot of error messages, the first one like NullPointerException followed by many other. I guess it has to do with the state and rate. Can somebody give me an example of how to use ODE interface and ODEsolver for a first order ODE like the nucleus decay?


Replies to Simulation of nucleus decay by ODEsolver

Re: Simulation of nucleus decay by ODEsolver -
Wolfgang
192 Posts

Have you tried to run the examples in the OSP Eclipse workspace?

http://www.compadre.org/osp/items/detail.cfm?ID=7147

There are many ODE examples in the first few chapters.



Re: Re: Simulation of nucleus decay by ODEsolver -
Martin Jola
7 Posts

Dear Wolfgang

yes I tried the ODE examples of the book, they all work fine.But they are all for second order ODE's. I have a few first order ODE's I would like to splve with ODE/ODEsolver.  Based on the book Open Source Physics (a User Guide with Examples) and the examples csm book I found out how to use ODe/ODEsolver for first order ODE's. Please have a look on the code below. May be you have some comments for improvements.

Thanks for your help

Martin

import org.opensourcephysics.numerics.ODE;
public class NucDecODE implements ODE{
double [] state =new double[3];
double lambda=0.01;
public  NucDecODE(double x){
state[0]=x;
state[1]=-lambda*state[0];
state[2]=0;
}
public double[] getState(){
return state;
}
public void getRate(double[] state,double[] rate){
rate[0]=state[1];
rate[1]=0;
rate[2]=1;
}
}
public class NucDecODEApp {
public NucDecODEApp(double x0) {
}
public static void main(String[] args) {
double dt=0.1;
double x0=100.0;
NucDecODE ode = new NucDecODE(x0);
ODESolver ode_solver=new Euler(ode);
ode_solver.initialize(dt);
while(ode.state[0]>90.0){
ode_solver.step();
}
System.out.println("final time= " +ode.state[2]);
System.out.println("final x =" +ode.state[0]);
}
}



OSP Projects:
Open Source Physics - EJS Modeling
Tracker
Physlet Physics
Physlet Quantum Physics
STP Book