No line when trying to plot Matlab
Show older comments
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);
Answers (2)
madhan ravi
on 9 Oct 2018
Edited: madhan ravi
on 9 Oct 2018
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 Comments
bob bobsson
on 9 Oct 2018
madhan ravi
on 9 Oct 2018
Edited: madhan ravi
on 9 Oct 2018
did you try my code first? see the attached screenshot , I got all the graphs
Walter Roberson
on 9 Oct 2018
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 Comments
bob bobsson
on 9 Oct 2018
Walter Roberson
on 9 Oct 2018
Your gy is the constant 26 for all x values.
Categories
Find more on Common Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!