ode45 Matlab system of differential equations

1 view (last 30 days)
Hi,
I'm new to Matlab and I have been trying to solve/simulate values of my system of ordinary differential equations. Even when I copy and paste examples from the net Matlab tells me that my function is undefined. For example:
function ypsir =ypsir(t,y) a = .01; b = .1; ypsir(1) =-a*y(1)*y(2); ypsir(2) = a*y(1)*y(2)-b*y(2); ypsir(3) = b*y(2); ypsir = [ypsir(1) ypsir(2) ypsir(3)]';
Matlab will respond with
??? Input argument "y" is undefined.
Error in ==> Untitled at 4 ypsir(1) =-a*y(1)*y(2);
Can someone please explain what this means and how to fix it.
Thanks! :)

Accepted Answer

Mischa Kim
Mischa Kim on 8 Apr 2015
Edited: Mischa Kim on 8 Apr 2015
Leah, use something like
function ODE_test()
[t,Y] = ode45(@ypsir_ODE,[0 10],[1 1 1]);
plot(t,Y)
function ypsir = ypsir_ODE(t,y)
a = .01; b = .1;
ypsir(1) =-a*y(1)*y(2);
ypsir(2) = a*y(1)*y(2)-b*y(2);
ypsir(3) = b*y(2);
ypsir = [ypsir(1) ypsir(2) ypsir(3)]';
  • Re-name the ODE function name since you use it also as the return value; I used ypsir_ODE.
  • Save the functions in one file, called ODE_test.m.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!