MATLAB: Defining a system of equations

MATLAB

In a previous post, a system of three ODE is set as
f = @(t,x) [-x(1)*x(2);x(1)*x(2)-x(2);x(2)]
But I want to define each equation in a separate line and then put all of the three together. Please advise.

Best Answer

You can define it as an anonymous function. Creating a new file is not necessary.
First method
f = @(t,x) [-x(1)*x(2);
x(1)*x(2)-x(2);
x(2)];
Second method
f1 = @(t,x) -x(1)*x(2);
f2 = @(t,x) x(1)*x(2)-x(2);
f3 = @(t,x) x(2);
f = @(t,x) [f1(t,x);
f2(t,x);
f3(t,x)];
Both are equivalent.