MATLAB Programming/Order Differential Equations
Order of Differential Equations
editA differential equation is an equation with a function and one or more of its derivatives. It is usually to describe changes.
Note: Derivatives = .
An order of differential equation (ODE) is an equation that has highest derivative of the dependent variable involved some ordinary derivatives of a function with respect to the independent variable. Often, the goal is to solve an ODE, i.e., determine what function or functions satisfy the equation. That means the differential equation defines the relationship between variables and their derivatives.
There are a primarily two of ODE types
- 1st Order Differential Equation (1st ODE)
- 2nd Order Differential Equation (2nd ODE)
First Order Differential Equation
editAll the linear equations in the form of derivatives are in the first order. It has only the first derivative such as dy/dx, where x and y are the two variables and is represented as
Second-Order Differential Equation
editWhen the order of the highest derivative present is 2, then it is a second order differential equation.
Using dsolve solve differential equation
editWe have an differential equations as follow,
,
In order to solve this differential equation in MATLAB , we uses dsolve command to get the answer as followed
>>s=dsolve('D3y+2*D2y+5*Dy+4*y=5*x','y(0)=1','Dy(0)=3')
s =
(5*x)/4 - exp(-t)*(15^(1/2)*C1 - (5*x)/4 + 7) + exp(-t/2)*cos((15^(1/2)*t)/2)*(15^(1/2)*C1 - (5*x)/2 + 8) - C1*exp(-t/2)*sin((15^(1/2)*t)/2)
Type of ODE solver in MATLAB
editThere are few types of ODE solver but we will focus on just the commonly used :
ODE | Accuracy | Use case suggestion |
---|---|---|
ode45 | Medium | For most use case, ode45 should be enough to solve the ODE |
ode23 | Low | ode23 can be more efficient than ode45 at problems with crude tolerances |
ode113 | Low to High | ode113 can be more efficient than ode45 at problems with stringent error tolerances, or when the ODE function is computationally intensive. |
Examples: ODE45 to solve differential equations
editThere are $10,000 deposited into the bank fixed deposit accounts and the interest rate is 2% annually.
To start with, we need to do a equation to model the money.
Equation 1: : Annual saving interest is 2% annually on the balance of the bank balance over time ,
Equation 2: : Amount of money initial is at $10,000
From Equation 1, we take out (Note: Need to consult on other Wikiprojects on how to derive the linear equations)
, where
, taken from equation 2
Using dsolve functions , we can similarly convert the differential equations
>> syms M(t) p
>> eqn = diff(M,t) == 0.02*M;
>> S= dsolve(eqn,M(0)==10000)
S =
10000*exp(t/50)
Hence. we have equations (shown as S) to do our modelling.
In MALAB to calculate differential equations using ode45 , the syntax is as followed
[t,y] = ode45(odefun,tspan,y0)
where,
t = Evaluation points, returned as a column vector
y = Solutions, returned as an array
odefun = Function of the ode. You may use anonymous functions instead of writing a new files for the functions
tspan = Interval of integration, specified as a vector. At minimum, tspan must be a two element vector [t0 tf] specifying the initial and final times
y0 = Initial conditions, specified as a vector. y0 must be the same length as the vector output of ode function,
Using dsolve for converted differential equations ode45 functions to solve ODE
function [T,M] = money_in_bank(R)
% Enter the initial values for the amount of money in the bank
M0 = 1000;
% Enter the interest rate
R = 2;
% Enter the time period
T0 = 1;
Tf = 30;
% using dsolve previously
S=10000*exp(T0/50)
% Solve the differential equation
[T, M] = ode45(@(T,M)S, [T0, Tf], M0);
% Plot the solution
plot(T,M,'-o')
% Add a legend and labels
legend('M(t)')
xlabel('Years')
ylabel('Money in Bank')