-2

For a project I need to understand a matlab code, but as I am quite new I dont really understand what is happening. 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); 

So the question is: What is happening? I also need to plot S in the same figure as X and R. How do I do this?

I appreciate your help!

Joel
  • 3
  • 2

1 Answers1

0

This is a really basic Matlab question. There is tons of information about your requested topic. I think these slides will help you on the right path.

However, a quick explanation; the first code you provide is the function which describes your ordinary differential equation. This function always has to be of the form x' = f(t,x,...). Herein t is the time and x is the state. After the state (on the place of the dots ...) you can define other input parameters, such as is being done in your ode function. Furthermore, the interp1 function interpolates the data provided.

The second code you provide is the code you start within Matlab. Parameters are defined, after which the ordinary differential equation is solved and plotted.

If you have any further questions I would recommend that you first try to find your answer using a search engine.

Community
  • 1
  • 1
WG-
  • 1,046
  • 2
  • 14
  • 27