Wednesday 25 January 2017

plot - MATLAB Not properly plotting graph



It seems that my MATLAB has some sort of trouble plotting anything that is over tan(x). For example, trying to plat (tan(x) + sin(x))/(2*tan(x)):




clc
clear all

x = 0:0.1:pi;

y1 = cos(x/2).^2;
subplot(1,2,1);
plot(x, y1);


y2 = (tan(x) + sin(x))/(2*tan(x));
subplot(1,2,2);
plot(x, y2);


I've tried putting it on it's own plot as well, but all I seem to get is a blank graph, but the axes are all lined up for the range I've set. The only thing that has made anything appear is removing the tan(x) on the bottom.


Answer



You should use the element-wise division operator ./:



y2 = (tan(x) + sin(x))./(2*tan(x));



Indeed with a = (tan(x) + sin(x)) and b = (2*tan(x)), what you wrote is:



y2 = a / b;


which is matrix division, y2 = a * pinv(b), a scalar number in this case.



NB: As b cannot be inverted, matlab uses pseudo-inverse pinv



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...