solve and plot symbolic equation

31 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 28 Sep 2017
Edited: Walter Roberson on 28 Sep 2017
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 Comments
Imad Ahmed
Imad Ahmed on 3 Oct 2017
This is a great answer and solves the problem. Thank you so much.
hang zhao
hang zhao on 16 Mar 2020
Very helpful thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!