MATLAB: How to create a matrix from given equation system

differential equationsequationstomatrixode

This must be a standard case but I can't figure how to do this. I also would like to know what is the best practise for this: I have an equation system let's say MyOde = {'-x','x-y','y'}; of which I want Matlab to create the corresponding matrix which is obviously A=([[-1 0 0]; [1 -1 0]; [0 1 0]]); My approach so far:
function displayMatrix(ode)
u=sym(ode(1));
v=sym(ode(2));
w=sym(ode(3));
A = zeros(3,3);
A(1) = equationsToMatrix([u]);
A(2) = equationsToMatrix([v]);
A(3) = equationsToMatrix([w]);
disp(A);
end
The result is the error In an assignment A(:) = B, the number of elements in A and B must be the same." which is clear to me but I don't know how to fix it. I thought equationsToMatrix would take care of putting the equations properly to a 3×3 matrix which is obviously not the case. Thanks in advance.

Best Answer

u = sym(ode(1));
v = sym(ode(2));
w = sym(ode(3));
A = equationsToMatrix([u; v; w]);
Note that you only have two variables in your sample ode so you get a 3 x 2 matrix. If you need to generalize to 3 variables then you need to pass in the list of possible variables.
Related Question