Problem plotting a function

조회 수: 2 (최근 30일)
Erbaz Khan
Erbaz Khan 2018년 4월 23일
댓글: Walter Roberson 2018년 4월 24일
I am having problem plotting the function. When I try to run the code it says "Error using plot Data must be numeric, datetime, duration or an array convertible to double."
syms w;
X = (2*w + 3).*(heaviside(w+6)-heaviside(w+4));
X = X + ((-2*w - 1).*(heaviside(w+4)-heaviside(w+2)));
w = -10:10;
plot(w, X)

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 23일
plot(w, subs(X))
  댓글 수: 2
Erbaz Khan
Erbaz Khan 2018년 4월 23일
Can you please explain why did you use subs(X) instead of X?
Walter Roberson
Walter Roberson 2018년 4월 24일

When you use

syms w

and then construct a formula using w, then the w in the resulting formula is not a reference to "the current value of the variable w if it has been assigned a value"

Just like if you do

A = 2
B = A * 3
A = 5

then B copied the current value of A at the time of execution, rather than making B a formula that has a reference to the current value of A with B intended to update as A updates.

The situation is the same with the symbolic engine: when you refer to w the current value of w is copied, rather than a reference being made to whatever value of w is current. Therefore when you assign a numeric value to w after constructing a formula with symbolic w, the formula does not change to evaluate according to the new w value. You need to instruct the symbolic engine to look through the workspace to find values for the symbolic variables in the expression and incorporate them into the expression; subs() tells it to do that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by