%% Numerical Solution of ODE
% How to use Matlab's ODE solver to solve a differential equation
%
% BJ Furman
%
% 15SEP2014
%% Solve the ODE numerically
% Consider the first order ODE describing a simple open-loop cruise control
% (m/b)*dV(t)/dt + V(t) = (Ke/b)*Theta0
%  which can be written:
%  dV(t)/dt = (b/m)*[(Ke/b)*Theta0 - V(t)]
clear all;
% Constants for the physical system
m = 1000; % kg
b = 50; % N-s/m
tau = m/b;  % the time constant
g = 9.81; % m/s^2
Theta0 = 30*pi/180; % radians
Ke = 500/Theta0; % N/rad
K = (Ke/b)*Theta0; 
v0 = 0; % initial velocity at t=0-

% Create an 'anonymous' function, vdot, of time and v
vdot=@(t,v) (b/m)*((Ke/b)*Theta0 - v);  % vdot stores the 'handle' of the function

% Use the ode45 solver to integrate the equation over t=0 to t=100, with v0=0
[t v] = ode45(vdot,[0 100],0);

% Plot the results
plot(t,v)
grid
