How do I display numeric values in a struct?

I'm trying to solve a system of equations.
syms m F1 F2 l a1 a2
%Equations
eqn1 = F1 + F2 -9.81*m == 0;
eqn2 = a1 + a2 - l == 0;
eqn3 = F2*a2 - F1*a1 == 0;
The values for some of these variables are given, but three are to be solved by the program. The following code solves it, but I can't get it to display the numerical values of sol:
m = 1576;
F1 = 4562.3;
l = 2.65;
sol = solve([eqn1, eqn2, eqn3], [F2, a1, a2])
disp('Problem1 1: Values of F2, a1, a2');
disp(structfun(@double, sol));
No semicolon for line 4 here - sol displays in the command window as a 1x1 struct with 3 fields:
sol =
F2: [1x1 sym]
a1: [1x1 sym]
a2: [1x1 sym]
How do I "convert" the fields of sol to numerical solutions I can display?

1 件のコメント

RAVI SINGH
RAVI SINGH 2021 年 8 月 2 日
I too am seeking the solution for similar issue.

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

 採用された回答

madhan ravi
madhan ravi 2019 年 2 月 4 日

2 投票

sol.F2 % use dot indexing

2 件のコメント

madhan ravi
madhan ravi 2019 年 2 月 4 日
Alternatively
[F2,a1,a2]=solve(...)
madhan ravi
madhan ravi 2019 年 2 月 4 日
or use vpasolve() with same number of output arguments as shown in the above comment

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 8 月 2 日

3 投票

format long g
syms m F1 F2 l a1 a2
%Equations
eqn1 = F1 + F2 -9.81*m == 0;
eqn2 = a1 + a2 - l == 0;
eqn3 = F2*a2 - F1*a1 == 0;
m = 1576;
F1 = 4562.3;
l = 2.65;
sol = solve(subs([eqn1, eqn2, eqn3]), [F2, a1, a2])
sol = struct with fields:
F2: [1×1 sym] a1: [1×1 sym] a2: [1×1 sym]
disp(structfun(@double, sol, 'uniform', 0));
F2: 10898.26 a1: 1.86800406971028 a2: 0.781995930289718

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by