2
votes

I am trying to create an SIR Model (solving 3 differential equations with two parameters) and plot the solution for this on MATLAB and I keep getting the error 'Not enough input arguments'. I really can't see where I am going wrong. Here is the code:

function dx = sir(t,x) 
dx=[0; 0; 0]; 
beta = .003;
delta = 1;
dx(1)= -beta *x(1)*x(2); 
dx(2)=beta*x(1)*x(2)-delta*x(2); 
dx(3)=delta*x(2); 
%options = odeset('RelTol', 1e-4, 'NonNegative', [1 2 3]); 
[t,x] = ode45('sir', [0 10], [1000 1 0], options); 
plot(t,x); 
%legend('S', 'I', 'R'); 
end
1
First, why don't you just copy the code in the Question? Second, can you show how you call the function sir(t,x);?tashuhka
The code in the question:Anona anon
function dx = sir(t,x) dx=[0; 0; 0]; beta = .003; delta = 1; dx(1)= -beta x(1)*x(2); dx(2)=betax(1)*x(2)-deltax(2); dx(3)=deltax(2); %options = odeset('RelTol', 1e-4, 'NonNegative', [1 2 3]); [t,x] = ode45('sir', [0 10], [1000 1 0], options); plot(t,x); %legend('S', 'I', 'R');Anona anon
i've been told that I have to call outside the function in order for my solution to be plot - how do i do this? thanksAnona anon
Also note the line ode45('sir', [0 10], [1000 1 0], options); will recall you function infinity times because of the name used which is sir, you need to rename it.Alyafey

1 Answers

1
votes

I think you have a serious misconception about how function calls and recursion work, or possibly you're just not familiar with programming. You need two separate things:

  1. Define a function and save it as sir.m. This defines your ODE. It should contain:
    function dx = sir(t,x) beta = .003; delta = 1; dx(1)= -beta *x(1)*x(2); dx(2)=beta*x(1)*x(2)-delta*x(2); dx(3)=delta*x(2);

  2. Run the code to solve your ODE. This can be directly pasted into the command window as long as sir.m is in your path or current directory:
    options = odeset('RelTol', 1e-4, 'NonNegative', [1 2 3]); [t,x] = ode45('sir', [0 10], [1000 1 0], options); plot(t,x); legend('S', 'I', 'R');

Note that step 1 defines the sir function and step 2 uses it. This needs to happen in two separate steps to prevent sir from calling itself (or more precisely in your case, calling a function, ode45, which calls sir again). This is called recursion and is not what you should be doing here.