ODEs
From Sutherland_wiki
Revision as of 16:58, 19 February 2009 by 00033394 (talk | contribs) (New page: Category:Matlab == Solving ODEs in Matlab == To solve an ODE (or a system of ODEs) in Matlab, you need to build a function that evaluates the right-hand-side of the ODEs. For examp...)
Solving ODEs in Matlab
To solve an ODE (or a system of ODEs) in Matlab, you need to build a function that evaluates the right-hand-side of the ODEs. For example, let's consider a simple ODE that describes first-order decay
This can easily be separated and solved analytically to find
where is the concentration at .
To solve this in Matlab, we create a function like the following:
function rhs = decay( k, c )
% calculates the rate of decay given the
% rate constant (k) and concentration (c)
rhs = -k*t;
|
The ODE solvers in Matlab have the following requirements:
- Provide a function that calculates the RHS of the ODE. This function must take two arguments: the independent variable (e.g. time), and the dependent variable(s).
- Provide the time interval over which you desire the solution.
- Provide the value of the dependent variables at the beginning of the time interval (the initial condition).
We can now solve our problem above using the function contained in decay.m to integrate this ODE. The Matlab code to do this is outlined below.
|
Of course, for this ODE we could solve this without a separate function for the RHS as follows:
c0 = 10;
k = 0.1;
rhs = @(t,c)( -k*c );
[time,conc] = ode45( rhs, [0,50], c0 );
plot(time,conc); xlabel('t'); ylabel('c');
|
Systems of ODEs in Matlab
|