bode plot discrepancy - matlab

I am plotting the following
Cu4 = tf([1 2], [1 2 6]);
[magCu4 phaseCu4 wout] = bode(Cu4,logspace(-2,7,300));
magCu4 = squeeze(magCu4);
phaseCu4 = squeeze(phaseCu4);
semilogx(wout,20*log10(magCu4)),grid;
hold on
bode(Cu4,'r')
I would expect that the semilogx plot would return an identical plot as 'bode'. however, this doesn't seem to be the case. Does anyone know what is going wrong here?

The difference is that you do not specify a frequency vector in your second call to bode so MATLAB chooses a default vector (in your code it had length of 46).
Instead you could try:
bode(Cu4,'r',logspace(-2,7,300))
Compare the plots made by the following code
[magCu4 phaseCu4 wout] = bode(Cu4,logspace(-2,7,300));
magCu4 = squeeze(magCu4);
figure(1);
semilogx(wout,20*log10(magCu4))
hold on;
bode(Cu4,'r')
hold off;
figure(2);
semilogx(wout,20*log10(magCu4))
hold on;
bode(Cu4,'r',logspace(-2,7,300))
hold off;

Related

Update plot using hold on inside a for loop

I'm combining two plots using this code
plot(x1,y1,'.','MarkerSize',20,'Color','r');
hold on; grid on;
plot(x2,y2,'x','MarkerSize',10,'Color','b');
xlim([-a a]);
ylim([-a a]);
Now I want to change the values of x1,y1 and x2,y2 in order to have more than one point and one cross inside my figure. I tried to use a for loop where I compute new values, but every iteration this code generates another figure - whereas I want just one figure with all the points in it.
I did something like this:
for i=1:1:8
% do things that compute x1,x2,y1,y2
figure; hold on
plot(x1,y1,'.','MarkerSize',20,'Color','r');
hold on; grid on;
plot(x2,y2,'x','MarkerSize',10,'Color','b');
xlim([-a a]);ylim([-a a]);
i=i+1;
end
I also tried to put the hold on just before i=i+1 but still give me a new figure.
There are several things you can do:
The simple solution would be to put the figure command outside the loop:
figure(); hold on;
for ...
plot(x1, ...);
plot(x2, ...);
end
A better solution would be to first compute all values and then plot them:
[x1,y1,x2,y2] = deal(NaN(8,1));
for ind1 = 1:8
% do some computations
x1(ind1) = ...
...
y2(ind1) = ...
end
figure(); plot(x1,y1,'.',x2,y2,'x');
The best solution (in my opinion) would be to update existing plot objects with new data points as they become available:
[x1,y1,x2,y2] = deal(NaN(8,1));
figure(); hP = plot(x1,y1,'.',x2,y2,'x');
for ind1 = 1:8
% do some computations
hP(1).XData(ind1) = <newly-computed x1>
hP(1).YData(ind1) = <newly-computed y1>
hP(2).XData(ind1) = <newly-computed x2>
hP(2).YData(ind1) = <newly-computed y2>
end

matlab plots as movie with legend

i have a question regarding legend for movies.
This is my code:
fig = figure();
for i = 1: 70000
plot(signal1)
hold on;
plot([i,i],[-5,5])
plot(signal2,'r')
hold off;
title('\fontsize{14} my data');
legend('signal1','signal2');
axis tight;
f(i) = getframe(fig);
end
The legend shows the same colors for the first two things I plot. if I plot more it works for the other plots. Is there a trick I don't know?
The strings defined in the legend command are assigned in order of the plots being generated. This means that your first string 'signal1' is assigned to the plot for signal1 and the second string 'signal2' is assigned to the vertical line.
You have two possibilities to fix this problem.
Execute plot for the vertical line after the plot for the two signals.
Use handles to the plots to assign the legends directly.
Here is an example of changing the order:
plot(signal1)
hold on;
plot(signal2,'r')
plot([i,i],[-5,5],'k')
hold off;
legend('signal1','signal2');
Here is an example that uses handles (sp1 and sp2):
sp1 = plot(signal1)
hold on;
plot([i,i],[-5,5],'k')
sp2 = plot(signal2,'r')
hold off;
title('\fontsize{14} my data');
legend([sp1,sp2],'signal1','signal2');

Plot in child (Matlab)

Can someone please tell me how does the last plot command work in following script?
close all;
s=tf('s');
sys1 = 5/(s+5);
sys2=exp(-1*s);
G=ss(sys1)*ss(sys2);
opts = bodeoptions('cstprefs');
opts.Grid= 'ON';
% create a figure and get the handle of the figure
figHnd = figure;
bode(G,opts)
% get and display the children handles of the figure
childrenHnd =get(figHnd, 'Children');
% select magnitude plot and plot a line
axes(childrenHnd(3));
hold on;
plot([1 1], [-20 20], 'r')
hold off;
I am trying to add a horizontal line for cut-off frequency to my Bode plot (magnitude diagram) but I can't figure out how to do that. The current code adds a vertical line for me.
The question is about the line
plot([1 1], [-20 20], 'r')
which is a simple plot command. In general, you use
plot(x,y)
here it is the same: the x-vector is [1, 1] and the y-vector is [-20, 20]. So you draw a line from the point (1,-20) to (1,20). The last part (r) only specifies the color, i.e. red. This is exactly what you can see in the bode plot.
To create a horizontal line e.g. from (10^-1, -20) to (10^0, -20) you can similarily draw
plot([10^-1, 10^0], [-20, -20], 'r');
(don't forget to put it within hold on; ... hold off;, so the bode plot isn't erased.
change the last plot code similar to this:
plot([1 10], [-20 -20], 'r')
Refer plot and work on some examples to get an idea of how it works.

Matlab - Legend does not match my graph

I am having trouble matching my graph with my axis. The first two plots work and the second two do not. I am trying to plot Temperature versus Pressure for two Argo floats and then Salinity versus Pressure. Here is my code:
% First Plot
subplot(221);
plot(float1winter.T,float1winter.P,'b');
hold on;
plot(float1summer.T,float1summer.P,'r');
hold on;
tempAdiff = abs(float1summer.T-float1winter.T)
plot(tempAdiff,float1summer.P,'--k');
hold on;
set(gca,'ydir','reverse');
title('Argo Float #1901440 Temp Profiles');
legend(['float1winter','float1summer','tempAdiff'],{'11-29-2013','07-01-2013','Temperature Difference'},'location','southwest');
xlabel('Temperature (°C)');
ylabel('Pressure');
shg;
% Second Plot
subplot(222);
plot(float2winter.S,float2winter.P,'m');
hold on;
plot(float2summer.S,float2summer.P,'c');
hold on;
set(gca,'ydir','reverse');
title('Argo Float #1901440 Salinity Profiles');
legend(['float2winter','float2summer'],{'11-29-2013','06-02-2013'},'location','southwest');
xlabel('Salinity (psu)');
ylabel('Presure');
shg;
% Third Plot
subplot(223);
% Matrix demensions did not agree bewteen winter and summer profiles. The summer profile was 71 x 2 and the winter was 70 x 2. I tried "reshape"
% and that didn't work. So I changed the vector of float3summer.T to
% float3bsummer.T with an array of 70 x 2
float3bsummer.T = float3summer.T(1:70,1:2);
float3bsummer.P = float3summer.P(1:70,1:2);
plot(float3winter.T,float3winter.P,'Linewidth',1,'color','blue');
hold on;
plot(float3bsummer.T,float3bsummer.P,'Linewidth',1,'color','red');
hold on;
tempdiff = abs(float3bsummer.T-float3winter.T)
plot(tempdiff,float3bsummer.P,'--k');
hold on;
set(gca,'ydir','reverse'); % this line reverses the y-axis so that depth increases downward
title('Argo Float #1901415 Tempearture Profiles');
hold on;
summerfloat = plot(float3bsummer.T,float3bsummer.P,'r');
legend(['float3winter.T','summerfloat','tempdiff'],{'12-03-2013','07-03-2013','Temp Diff'},'location','southeast');
xlabel('Temperature (°C)');
ylabel('Pressure');
axis ([-1,4,0,2000]);
shg;
% Fourth Plot
subplot(224);
plot(float3winter.S,float3winter.P,'g');
% Changed matrix dimensions for Salinity of Summer
float3bsummer.S = float3summer.S(1:70,1:2);
float3bsummer.P = float3summer.P(1:70,1:2);
plot(float3bsummer.S,float3bsummer.P,'.b');
hold on;
set(gca,'ydir','reverse');
title('Argo Float #1901415 Salinity Profiles');
h4 = plot(float3winter.S,float3winter.P,'g');
hold on;
h5 = plot(float3bsummer.S,float3bsummer.P,'.b');
hold on;
legend(['float3winter','float3bsummer.T'],{'12-03-2013','07-03-2013'},'location','southwest');
xlabel('Salinity (psu)');
ylabel('Pressure');
axis ([33.8,34.8,0,2000]);
shg;
% Save File to Desktop
set(gcf,'color','w');
saveas(gcf,'~/Desktop/hw1_figure1.pdf','pdf');![enter image description here][1]
I guess that you're trying to associate the set of strings for your legend, {'11-29-2013','07-01-2013','Temperature Difference'}, with the plots made from the variables ['float1winter','float1summer','tempAdiff'].
However, this isn't how legend works. MATLAB has no way of associating the plot produced by plot(float1winter.T,float1winter.P,'b'); to the string float1winter. If you want to specify which plots go with which legend entries, you need to pass the object handles of the plots to legend, which is easiest done by returning the handles when you plot originally:
h(1) = plot(float1winter.T,float1winter.P,'b');
hold on;
h(2) = plot(float1summer.T,float1summer.P,'r');
h(3) = plot(tempAdiff,float1summer.P,'--k');
legend(h,{'11-29-2013','07-01-2013','Temperature Difference'});
Side note: you only need to call hold on once per axis - so once for each subplot but not after every plot call.
Alternatively, you can not give handles at all; legend will assign the text to the plots in the order that they were plotted:
legend({'11-29-2013','07-01-2013','Temperature Difference'})
Understanding graphics handles allows you a lot more control over plots, especially if you might want to make small adjustments to them. For example, if I decide that I want the first plot to be green rather than blue, then I can just do:
set(h(1),'Color','g');
This will change the plot color and the legend changes to match automagically. To see a list of all the properties of an object, use get with only the handle. You can set more than one property at a time. For example:
get(h(1))
set(h(1),'DisplayName','Winter','LineWidth',3,'Marker','x')

In MATLAB, how does one clear the last thing plotted to a figure?

In MATLAB, I plot many different vectors to a figure. Now, what I would like to do is simply undo the last vector that I plotted to that figure, without clearing everything else. How can this be accomplished? Can it be accomplished?
Thanks
Edit:
figure(1); clf(1);
N = 100;
x = randn(1,N);
y = randn(1,N);
z = sin(1:N);
plot(x); hold on;
plot(y,'r');
plot(z,'k');
Now here, I would like to remove the plot z, which was the last plot I made.
If you know before plotting that you want to remove it again later, you can save the handle returned by plot and delete it afterwards.
figure;
h1 = plot([0 1 2], [3 4 5]);
delete(h1);
Try
items = get(gca, 'Children');
delete(items(end));
(or maybe delete(items(1)))
The answer that #groovingandi gives is the best way to generally do it. You can also use FINDALL to find the handle based on the properties of the object:
h = findall(gca, 'type', 'line', 'color', 'k');
delete(h);
This searches the current axes for all line objects (plot produces line objects) that are colored black.
To do this on, say, figure 9, you need to find the axes for figure 9. Figure handles are simply the figure number, so:
ax = findall(9, 'axes');
h = findall(ax, 'type', 'line', 'color', 'k');
delete(h);