0
votes

As part of a group project we have a system of 2 non linear differential equations and we have to draw the S=S(t) , I=I(t) graphic using the midpoint method.

And I'm getting the following error when trying to insert the matrix with the corresponding differential equations:

"Error in inline expression ==> matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]]) Undefined function 'matrix' for input arguments of type 'double'.

Error in inline/subsref (line 23) INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);"

The code I have done is the following:

syms I S
u=[S;I];
F=[-0.001*S*I;0.001*S*I-0.3*I];
F1=inline(char(F),'I','S');
h=100; %Valores aleatórios
T=100000;
ni=(T/h);
u0=[799;1];
f=zeros(1,2);
k=zeros(1,2);
i=1;

while i<=ni
f(1)=F1(u0(1));  
f(2)=F1(u0(2));
dx=h*f;
k(1)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));  
k(2)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));
u1=u0+h*k;
disp('i:'),disp(i)
disp('u= '),disp(u1)
u0=u1;
i=i+1;
end

I'm new to this so the algorithm it's very likely to be wrong but if someone could help me with that error I'd apreciate it. Thank you!

1
F1 is an inline function that will call function matrix. Is function matrix visible to Matlab, that is, is it in the local folder of your code or in the path?Lord Henry Wotton
Using "which matrix" it says its not found, but I didn't create any matrix functionTiago Roseiro
Hum, after some more research, I reckon my previous comment was a bit misleading. I don't really know much about sym but the error you are getting should be related to this. I think you should try going from there. Good luck!Lord Henry Wotton

1 Answers

0
votes

The problem that specifically creates the error is that you are putting two symbolic functions into a matrix and then calling char (which outputs matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]]) rather than converting nicely to string).

The secondary problem is that you are trying to pass two functions simultaneously to inline. inline creates a single function from a string (and using anonymous functions instead of inline is preferred anyway). You cannot put multiple functions in it.

You don't need sym here. In fact, avoid it (more trouble than it's worth) if you don't need to manipulate the equations at all. A common method is to create a cell array:

F{1} = @(I,S) -0.001*S*I; 
F{2} = @(I,S) 0.001*S*I-0.3*I;

You can then pass in I and S as so:

F{1}(500,500)

Note that both your functions include both I and S, so they are always necessary. Reconsider what you were expecting when passing only one variable like this: f(1)=F1(u0(1));, because that will also give an error.