Solving system of equations for different combinations of symbolic variables

3 ビュー (過去 30 日間)
vaporcone767
vaporcone767 2019 年 10 月 12 日
コメント済み: Ana Soph 2020 年 5 月 9 日
Hello,
I have a system of equations like [x1; x2; x3] = [3x3 coefficient matrix]*[y1; y2; y3]
Depending on user inputs 3 of the x or y terms will have numerical values. I would like to solve for the remaining 3 terms symbolically so that I have something like:
x1=3
x3=4
y1=5
I can solve the equations using:
syms x1 x3 y1
A=[x1; x2; x3]
B=[3x3 coefficient matrix]
C=[y1; y2; y3]
D=B*C
equations = [A(1)==D(1), A(2)==D(2), A(3)==D(3)]
answer = solve(equations, [x1, x3, y1])
x1=answer.x1
x3=answer.x3
y1=answer.y1
I know the symbolic variables that I want solve for by checking what user input fileds were left empty:
unknowns=[x1, x3, y1]
The problem is I don't know how to get symbolic unknowns ie.(x1, x3, y1) to be the variable that are solved for in answer = solve(equations, [x1, x3, y1]).
It seems like x1, x3, y1 should be dynamic variables but this is not recommended in matlab. Is there a way to accomplish what I am trying to do? Eventually I will need to expand to 6 equations and perform these calculations potentially hundreds of times for different values.
Thanks

回答 (1 件)

Star Strider
Star Strider 2019 年 10 月 12 日
I am not certain what result you want.
Try this:
syms x y
A = sym('x',[3,1]);
C = sym('y',[3,1]);
A(1) = 3;
A(3) = 4;
C(1) = 5;
B = randi(9,3);
S = solve(A == B*C);
v = fieldnames(S);
for k = 1:numel(v)
Out{k,:} = sprintf('%s = %f', v{k}, getfield(S,v{k}));
end
For one random ‘B’ this produces:
Out =
3×1 cell array
{'x2 = -26.500000'}
{'y2 = -18.166667'}
{'y3 = 4.833333' }
Make appropriate changes to get the result you want.
  3 件のコメント
Star Strider
Star Strider 2019 年 10 月 12 日
My pleasure.
I characteristically do not use the eval function for a number of reasons, however that appears to be the only option for what you want to do:
for k = 1:numel(v)
eval(Out{k})
end
That will assign them appropriately for later use. (I checked to be sure.)
DO NOT use eval other than this restricted context!
Ana Soph
Ana Soph 2020 年 5 月 9 日
Hi, if i have an equation that this variable depend from a data of another equation ??

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by