How to evaluate sym using certain known multiple values?

1 回表示 (過去 30 日間)
Rounak Saha Niloy
Rounak Saha Niloy 2023 年 12 月 31 日
回答済み: John D'Errico 2023 年 12 月 31 日
I have a 2*3 sym (named "A") as follows:
[ 1, 0, 0]
[-1/(2*(x1/(x2^2 - 10*cos(4*x3*pi) - 10*cos(4*x2*pi) + x3^2 + 21))^(1/2)), (x1*(2*x2 + 40*pi*sin(4*pi*x2)))/(2*(x1/(x2^2 - 10*cos(4*x3*pi) - 10*cos(4*x2*pi) + x3^2 + 21))^(1/2)*(x2^2 - 10*cos(4*pi*x3) - 10*cos(4*pi*x2) + x3^2 + 21)) - (2*x2 + 40*pi*sin(4*pi*x2))*((x1/(x2^2 - 10*cos(4*pi*x3) - 10*cos(4*pi*x2) + x3^2 + 21))^(1/2) - 1), (x1*(2*x3 + 40*pi*sin(4*pi*x3)))/(2*(x1/(x2^2 - 10*cos(4*x3*pi) - 10*cos(4*x2*pi) + x3^2 + 21))^(1/2)*(x2^2 - 10*cos(4*pi*x3) - 10*cos(4*pi*x2) + x3^2 + 21)) - (2*x3 + 40*pi*sin(4*pi*x3))*((x1/(x2^2 - 10*cos(4*pi*x3) - 10*cos(4*pi*x2) + x3^2 + 21))^(1/2) - 1)]
I want to evaluate this 2*3 sym at -
X=[0.394876, 0.963263, 0.173956]
where
x1=X(1);
x2=X(2);
x3=X(3);
How can I do this?
Note, I tried matlabFunction command. This causes certain problems in some certain scenarios. I do not want to use matlabFunction command.

採用された回答

Rounak Saha Niloy
Rounak Saha Niloy 2023 年 12 月 31 日
double(subs(A, [x1, x2, x3], X));
Could sort it out. The above code evaluates the sym at X.

その他の回答 (1 件)

John D'Errico
John D'Errico 2023 年 12 月 31 日
Easy peasy, though using matlabFunction here is not my recommendation.
syms('x',[1,3])
So x is a vector, with elements [x1,x2,x3].
A = [[ 1, 0, 0]
[-1/(2*(x1/(x2^2 - 10*cos(4*x3*pi) - 10*cos(4*x2*pi) + x3^2 + 21))^(1/2)), (x1*(2*x2 + 40*pi*sin(4*pi*x2)))/(2*(x1/(x2^2 - 10*cos(4*x3*pi) - 10*cos(4*x2*pi) + x3^2 + 21))^(1/2)*(x2^2 - 10*cos(4*pi*x3) - 10*cos(4*pi*x2) + x3^2 + 21)) - (2*x2 + 40*pi*sin(4*pi*x2))*((x1/(x2^2 - 10*cos(4*pi*x3) - 10*cos(4*pi*x2) + x3^2 + 21))^(1/2) - 1), (x1*(2*x3 + 40*pi*sin(4*pi*x3)))/(2*(x1/(x2^2 - 10*cos(4*x3*pi) - 10*cos(4*x2*pi) + x3^2 + 21))^(1/2)*(x2^2 - 10*cos(4*pi*x3) - 10*cos(4*pi*x2) + x3^2 + 21)) - (2*x3 + 40*pi*sin(4*pi*x3))*((x1/(x2^2 - 10*cos(4*pi*x3) - 10*cos(4*pi*x2) + x3^2 + 21))^(1/2) - 1)]];
Now you have X, with the values of x1,x2,x3.
X=[0.394876, 0.963263, 0.173956];
You can simply do this:
double(subs(A,x,X))
ans = 2×3
1.0000 0 0 -3.4478 -50.1285 95.5057
That stuffs the elements of X into x1,x2,x3 respectively. Then the call to double turns the result into numbers.

カテゴリ

Help Center および File ExchangeFormula Manipulation and Simplification についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by