I am writing Matlab code to solve equations with an unknown number of sym
variables. The number of variables supplied by the user. Here is code for three sym
variables, but the code produces an error.
clc;
clear
format long;
m=3;
mv = sym(zeros(m, 1));
for k=1:m
mv(k,1) = sym(sprintf('m%d', k));
end
display(mv)
A=[ 1.000 2.000 3.000
2.000 -2.000 -3.000
3.000 -3.000 7.000
];
B=[1;0;5];
w=A*mv-B
s=solve(w==0)
F=s.mv;
display(F)
This is not different from solving a simple equation with solve
.
s.m1
,s.m2
, etc. – Andras Deakfor k=1:m, mv(k,1)=s.(sprintf('m%d',k)); end
after thesolve
, then useF=mv;
as you intended to. OR:F=struct2cell(s); F=[F{:}].';
. – Andras Deak