ode45 and plotting problem

1 view (last 30 days)
Joel
Joel on 6 Oct 2015
Answered: Star Strider on 6 Oct 2015
Hi everyone. I have a matlab code, but I'm not really sure how it works. I have a function file and a script file.
Function:
function dxdt = sniffer_ode(t,x,par,tu)
X = x(1);
R = x(2);
k1 = par(1);
k2 = par(2);
k3 = par(3);
k4 = par(4);
S = interp1(tu(:,1),tu(:,2),t);
dxdt(1) = k3*S-k4*X;
dxdt(2) = k1*S-k2*X*R;
dxdt = dxdt(:); %dxdt should be column
and the script file:
%sniffer
close all
%initial conditions:
X0=0; R0=0;
x0=[X0 R0];
%parameters:
k1=1; k2=1; k3=1; k4=1;
par=[k1 k2 k3 k4];
%input:
tu=[ 0 , 0
1 , 0
1.01, 1
20 , 1];
[t,x] = ode45(@sniffer_ode,[0 20],x0, [],par,tu);
plot(t,x);
I need to plot S in the same figure as X and R. Now I don't really understand what happens at
S = interp1(tu(:,1),tu(:,2),t);
and at
[t,x] = ode45(@sniffer_ode,[0 20],x0, [],par,tu);
What are the empty brackets for? Deleting them gives me an error.
If you could help me I would be very grateful :D

Accepted Answer

Star Strider
Star Strider on 6 Oct 2015
The interp1 call interpolates the ‘tu’ matrix for whatever the value of ‘t’ is at a specific step. The ODE solvers all return the independent variable (here ‘t’), so you can easily create ‘S’ in your calling program by simply recalculating it in your main script:
tu=[ 0 , 0
1 , 0
1.01, 1
20 , 1];
t = 1:20;
S = interp1(tu(:,1),tu(:,2),t);
figure(1)
plot(t, S)
grid

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!