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 CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by