solve and plot symbolic equation

4 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by