I wanna plot a function in the interval 1-10 using the plot command but I don't get a line inside the box.
Anyone can see what the problem is with the code?
syms x
y = 1 + exp(x).*sin(5*x);
dy = diff(y)
ddy = diff(dy)
x = 0:.004:10;
gy = ddy - 2*dy + 26*y;
plot(x,gy);

回答 (2 件)

madhan ravi
madhan ravi 2018 年 10 月 9 日
編集済み: madhan ravi 2018 年 10 月 9 日

0 投票

syms x
y = 1 + exp(x).*sin(5*x);
dy = diff(y)
ddy = diff(dy)
gy = ddy - 2*dy + 26*y;
fplot(y,[1 10]) % interval 1-10
hold on
fplot(dy,[1 10])
fplot(ddy,[1 10])
fplot(gy,[1 10])
legend('y','dy','ddy','gy')
Remark: Use fplot when using symbolic variable/s

2 件のコメント

bob bobsson
bob bobsson 2018 年 10 月 9 日
All I get is a straight line. for gy. for but the other plots seems to be giving a correct line.
madhan ravi
madhan ravi 2018 年 10 月 9 日
編集済み: madhan ravi 2018 年 10 月 9 日
did you try my code first? see the attached screenshot , I got all the graphs

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

Walter Roberson
Walter Roberson 2018 年 10 月 9 日

0 投票

plot(x, subs(gy));
Consider: if your code had
X = 0
Y = X+3
X = 7
Would you expect the value of Y to suddenly become 10 instead of 3? No! Because you know that the value of the variable X as of the time of execution is copied.
When you
syms X
Then that is
X = sym('X')
Which assigns specific value to the variable, namely s reference to a symbolic variable. When you use X to construct y then that value is retrieved and used, creating further symbolic expressions. If you then assign a numeric value to X then just like the numeric case that does not go back and change things that used the value of the variable X: they continue to be symbolic expressions referring to a symbolic X rather than to the MATLAB X. So the gy you construct has no connection to the numeric X unless you use subs() to force a connection.

2 件のコメント

bob bobsson
bob bobsson 2018 年 10 月 9 日
I do not completely follow I just end up with it not properly working.
Walter Roberson
Walter Roberson 2018 年 10 月 9 日
Your gy is the constant 26 for all x values.

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

製品

タグ

質問済み:

2018 年 10 月 9 日

編集済み:

2018 年 10 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by