solve and plot symbolic equation

5 ビュー (過去 30 日間)
Imad Ahmed
Imad Ahmed 2017 年 9 月 28 日
コメント済み: hang zhao 2020 年 3 月 16 日
I need to solve the following equation and plot the results. Here is my code:
syms t X
F = zeta*(1-3*(X^2) + 2*(X^3) ) ==t %zeta is a constant
X = solve(F, X) %the solved expression of X
t = linspace(0,100)
plot(t, X)
The problem is that I get an error "data must be numeric, datetime, duration or an array convertible to double". I am not sure I understand what is needdd to be done here in order to plot the solved equation.
Please note that plotting the function F using fplot is not a problem but that is not what I want. I need to plot t vs X. Could you please help?

採用された回答

Walter Roberson
Walter Roberson 2017 年 9 月 28 日
編集済み: Walter Roberson 2017 年 9 月 28 日
plot(t, subs(X,'t',t) )
Which is to say that if you use a symbolic variable in an expression and later assign a numeric value to the symbol, then the expressions that used the symbolic variable are not automatically updated to use the numeric value.
The situation is exactly the same as if you had used
A = 1;
B = A + 10;
A = 15;
then afterwards, B is not updated to 25, because at the time the B=A+10 is executed, the value of A (1) is extracted and used.
syms A
B = A + 10;
A = 15;
then afterwards B is not updated to 25, because at the time the B=A+10 is executed, the value of A (sym('A')) is extracted and used.
The line
syms A
is the same as
A = sym('A');
This does not make A identical to the symbol named 'A': it establishes a link to the symbol named 'A', and
B = A + 10;
would copy that link to the symbol named 'A', not a link to the variable named 'A'.
A = 15;
would break the link between the variable named 'A' and the symbol named 'A', but B does not refer to the variable so B does not get updated to evaluate to 25: it still has the link to the symbol named 'A'.
Symbols themselves live in a different workspace (effectively).
  2 件のコメント
Imad Ahmed
Imad Ahmed 2017 年 10 月 3 日
This is a great answer and solves the problem. Thank you so much.
hang zhao
hang zhao 2020 年 3 月 16 日
Very helpful thanks!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by