create subplot with loop - matlab

i have following question and please help me to solve following task:
i want to create subplot in matlab which depend on loop variable,for example as i know to crate plot menu 2X2,we are doing like this
subplot(2,2,1)
subplot(2,2,2)
subplot(2,2,3)
subplot(2,2,4)
but can i do linear form?like 1:100?or something like this ,more generally like this
n=100;
for i=1:n
subplot(1,n,i)
?
thanks very much
EDITED CODE
function [order]=find_order(y,fs);
order=0;
n=length(y);
n1=nextpow2(n);
ndft=2^n1;
for i=1:floor(n/2)
[Pxx,f]=pburg(y,i,ndft,fs);
subplot(ndft,ndft,i);
plot(f,Pxx);
title(['order',num2str(i),'i']);
order=i;
end
end
picture :
i can't understand what happens

1-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
figure,
for k=1:4
subplot(4,1,k)
plot(t((k-1)*1000+1:k*1000),y1((k-1)*1000+1:k*1000))
xlim([0 40])
end
Output
2-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
colors=['r' 'g' ; 'y' 'k'];
figure,
for k1=1:2
for k2=1:2
subplot(2,2,(k1-1)*2+k2)
plot(t,y1,colors(k1,k2));
end
end
Output
Hopefully these demos would guide to you something meaningful for your case.

Yes, it is:
n=5;
for i=1:n
subplot(1,n,i)
end
gives

for pat=1: N % main loop
% Define the sublot grid
s1=3; % subplot rows
s2=3; % subplot columns
% find the figure number
fig_num=floor(pat/(s1*s2))+1 % Figure number
% Find the subplot number
sub_fig=mod(pat,s1*s2) % subplot number
% correct for corners
if(sub_fig==0)
sub_fig=s1*s2;
fig_num=fig_num-1;
end
% plot something
figure(fig_num);
subplot(s1,s2,sub_fig) ;
plot(1,1) % plot something
end % of main loop

Related

finding streamline and plotting it on a graph

I am trying to plot the streamlines for the following flow:
.
We are plotting our stagnation points with a sparatrix line but somehow the streamline just goes over it...
We cannot use the stream function. Any idea what is wrong?
Somehow the plot is not plotting correctly...here is my matlab code:
%% constant parameters
a=1;
U=100;
T=3*pi*a*U;
%% plot the stagnation points
syms z
k=100./z^2 - 1i*3./(2*z) - 100==0; %diff(w)==0
stagpoint=solve(k,z); %get the points
plot(stagpoint,'MarkerFaceColor','g');
%% plot the the sparatrix line
[x,y]=meshgrid([-7:0.1:7],[-2:0.1:2]);
z=x+1i*y;
w1=-100*(z+1./z)-1i*log(z).*3*pi*100./(2*pi);
psi=imag(w1);
limit=sqrt(x.^2+y.^2);
x((limit<0.99))=NaN;
y((limit<0.99))=NaN;
contour(x,y,psi,'k');
xlabel('Re');
ylabel('Im');
title('streamline for question4(a)');
axis([-5 5 -5 5])
daspect([1 1 1]);
hold on
%% plot streamline
RK4_4A(z);
%% plot the surface of the cylinder
angle=[0:0.1:360];
x=cosd(angle); %x component of the cylinder
y=sind(angle); %y component of the cylinder
plot(x,y);
hold on
fill(x,y,'r');
Here is the function we use for the 4th Order Runge-Kutta Method:
function RK4_4A(zn)
h=0.001;
x=5;
y=[-5:0.5:5];
zn=x+1i*y;
for j=1:length(y)
z(1)=zn(j);
t=0:h:2;
for i=1:length(t)-1
k1 = ffunc(z(i));
k2 = ffunc(z(i)+h/2*k1);
k3 = ffunc(z(i)+h/2*k2);
k4 = ffunc(z(i)+h*k3);
z(i+1) = z(i) + h*(1/6*k1+1/3*k2...
+1/3*k3+1/6*k4);
end
plot(real(z),imag(z),'b');
hold on
end
function s=ffunc(z)
s = 100./z^2 - 1i*3./(2*z) - 100;
s=conj(s);
end
end

How do i produce an animated GIF in MATLAB?

I want to produce a animated gif of a solution to a partial differential equation. That is the gif should show the solution at specific time.
Currently I can only make pictures in which all times are plotted.
Below is my entire program, with figure(3) being my attempt of making a gif.
clear all;
close all;
%%%%%%%%%%%%
% For slide 27 of Diffusion 1D
% The equation to be graphed in latex form is
% u(x,t)=\frac{1}{L}+\frac{2}{L}\sum^{\infty}_{n=1}cos(\frac{n\pi x_0}{L})cos(\frac{n\pi x}{L})e^{-k(\frac{n\pi}{L})^2t}
%%%%%%%%%%%%
%define constants
%note that the constants listed in the file are arbitrary
L = 2; %length of the rod
k= 0.01; % Diffusivity, which is assume to be constant but can be a function of x
x0 = 1; %location of the inital condition i.e. f(x)=delta(x-x0)
tmax= 50; %maximum amount of time the simulation runs
nmax = 200; % maximum value for n, increase to accuracy
tgrid = 21; %The number of points to be evaluated in the time domain
xgrid = 51; %The number of points to be evaluated in the space domain
%initialize variables
u=zeros(tgrid,xgrid); %preallocate array used for storing values of the solution
t=linspace(0,tmax,tgrid);%We assume that time is evenly distributed
x=linspace(0,L,xgrid); %We assume that space is evenly distributed
%Plotting variables
figure(1);
hold on;
axis([0 L -inf inf]);
xlabel('x');
ylabel('u(x,t)');
%Calculation,
for i=1:tgrid
for j=1:xgrid
seriesSum=0;
%Calculate the fourier series up to nmax for each point u(x,t)
for n= 1:nmax
seriesSum= seriesSum + cos(n*pi*x0/L)*cos(n*pi*x(j)/L)*exp(-k*t(i)*(n*pi/L)^2);
end
%Finish calcuation for solution at a specific point
u(i,j)= 1/L+(2/L)*seriesSum;
end
%After we have calculated all points at time t, we graph it for time t
plot(x,u(i,:),'linewidth',4);
end
saveas(gcf,'PDE_sol.png')%Save figure as png in current directory
%run a second loop that does not include the initial condition to get a
%better view of the long term behaviour.
%Plotting variables
figure(2);
hold on;
axis([0 L -inf inf]);
xlabel('x');
ylabel('u(x,t)');
for i=2:tgrid
plot(x,u(i,:),'linewidth',4);
end
saveas(gcf,'PDE_sol_without_inital.png')%Save figure as png in current directory
%Create a gif verison of figure 2
figure(3);
axis([0 L -inf inf]);
xlabel('x');
ylabel('u(x,t)');
filename = 'PDE_sol.gif';
for i=2:tgrid
plot(x,u(i,:),'linewidth',4);
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 2;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
The output gif that I get is
which is clearly not animated.
Note: If you think there is a better place to post this question please direct me to it. As my issue is with the MATLAB programming language and not the math involved I thought this would be the best place to post my question.
The first input to getframe is the handle of the figure that you'd like to take a screenshot of. As you have it written, you are grabbing figure 1 which is actually referring to the first figure that you create that you aren't updating within your loop.
You have assigned a numeric handle of 3 to the figure that you create right before your last loop so you'll want to tell getframe to use that figure instead.
Additionally, I would create one plot object and update the XData and YData rather than continuously creating new plot objects. The issue with calling plot continuously is that it's slow AND it completely resets all of your axes settings such as x and y labels as well as x and y limits.
% Store the handle to the figure in hfig
hfig = figure(3);
% Create the initial plot object
hplot = plot(NaN, NaN, 'LineWidth', 4);
axis([0 L 0 2]);
xlabel('x');
ylabel('u(x,t)');
filename = 'PDE_sol.gif';
for i=2:tgrid
% Update the plot appearance
set(hplot, 'XData', x, 'YData', u(i,:));
drawnow
% Get a screenshot of THIS figure
frame = getframe(hfig);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 2;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end

plot in all the subplots at the same time

I have a figure with 2 subplots.
I would like to know if it's possible (and how) to draw the same plots in all the subplots at the same time.
For example in the following plot I'd like to plot (x,y) simultaneously and then proceed separately.
fig1 = figure
subplot(2,1,1)
plot(x,y)
hold on
subplot(2,1,2)
plot(x,y)
hold on
subplot(2,1,1)
plot(x,z)
subplot(2,1,2)
plot(x,k)
You can do it with set using cell arrays as follows. See the documentation for details.
subplot(2,1,1);
h1 = plot(x,y); %// get a handle to the plot
subplot(2,1,2)
h2 = plot(x,y); %// get a handle to the plot
set([h1; h2], {'xdata'}, {x1; x2}, {'ydata'}, {y1; y2})
%// new values: x1 x2 y1 y2
If you're asking because you want to plot the same plot behind say 16 subplots, then you could do it in a loop:
for k= 1:16
s(k) = subplot(4,4,k);
plot(x,y);
hold on;
end
If you just want it for 2 subplots then there is nothing wrong with your current code

error in using fftoneside

Hi I'm trying to calculate mfcc for which i'm windowing. I have seen this one post I'm getting error in fftOneSide.
my code is
waveFile='test_preEmphasis.wav';
[y, fs]=wavread(waveFile);
n=512;
t=(1:n)'/fs;
startIndex=30418;
endIndex=startIndex+n-1;
original=y(startIndex:endIndex);
windowed=original.*hamming(n);
[mag1, phase1, freq1]=fftOneSide(original, fs);
[mag2, phase2, freq2]=fftOneSide(windowed, fs);
subplot(3,2,1); plot(original); grid on; axis([-inf inf -1 1]);
title('Original signal');
subplot(3,2,2); plot(windowed); grid on; axis([-inf inf -1 1]);
title('Windowedsignal');
subplot(3,2,3); plot(freq1, mag1); grid on;
title('Energy spectrum (linear scale)');
subplot(3,2,4); plot(freq2, mag2); grid on;
title('Energy spectrum (linear scale)');
subplot(3,2,5); plot(freq1, 20*log(mag1)); grid on;
axis([-inf inf -80 120]); title('Energy spectrum (db)');
subplot(3,2,6); plot(freq2, 20*log(mag2)); grid on; axis([-inf inf -80 120]);
title('Energy spectrum (db)');
the error i'm getting is
??? Undefined function or method 'fftOneSide' for input arguments of type 'double'.
any help is appreciated
thanks
This is a really old post, it'd be neat if someone still cared. I just provided what I believe to be the answer in the recent post below, which I arrived at after a fair bit of frustration: Undefined function 'fftOneSide' for input arguments of type 'double'.
It should be noted here there's a call to a file, which I'm not sure if the author had originally or not. I suspect all the problems are related to a similarly named file in the sourcecode.
If you look at my discussion in the other post, within the function definition there is a call to a method demo with a file - which isn't present if you just have the function definition, not the original file. Calling [mag1, phase1, freq1]=fftOneSide(original, fs,1), after you comment out the first line if nargin <1... and the demo routine worked fine on my machine with the code which I'll show below. Having the third argument equal to 1 guarantees that the code will run the print routines, which is important.
In case the other thread is closed, I just want to show the output, when the input is manually defined, and I call [mag1, phase1, freq1]=fftOneSide(original, fs,1) on the properly edited method, with the inputs as in the original post shown below (notice the call is to original in fftOneSide..in the other post it was like this as well.. I believe the call was was meant to be instead for windowed, but I don't have the signals toolbox with hamming anyways):
fs=8000;
t=(1:512)'/fs; %'// <-- prevents string markdown
f=306.396;
original=sin(2*pi*f*t)+0.2*randn(length(t),1);
% windowed=original.*hamming(length(t)); % defined in other post
[mag1,phase1,freq1]=fftOneSide(original,fs); % I call fftOneSide(original,fs,1);
The output is as follows (source code below!)
Anyways, here's the source code in case anyone wants to use this function
function [magSpec, phaseSpec, freq, powerSpecInDb]=fftOneSide(signal, fs, plotOpt)
% fftOneSide: One-sided FFT for real signals
% Usage: [magSpec, phaseSpec, freq, powerSpecInDb]=fftOneSide(signal, fs)
%
% For example:
% [y, fs]=wavread('welcome.wav');
% frameSize=512;
% startIndex=2047;
% signal=y(startIndex:startIndex+frameSize+1);
% signal=signal.*hamming(length(signal));
% plotOpt=1;
% [magSpec, phaseSpec, freq, powerSpecInDb]=fftOneSide(signal, fs, plotOpt);
% Roger Jang, 20060411, 20070506
if nargin<1, selfdemo; return; end %=== (MathBio: Comment this out!)
if nargin<2, fs=1; end
if nargin<3, plotOpt=0; end
N = length(signal); % Signal length
freqStep = fs/N; % Frequency resolution
time = (0:N-1)/fs; % Time vector
z = fft(signal); % Spectrum
freq = freqStep*(0:N/2); % Frequency vector
z = z(1:length(freq)); % One side
z(2:end-1)=2*z(2:end-1); % Assuming N is even, symmetric data is multiplied by 2
magSpec=abs(z); % Magnitude spectrum
phaseSpec=unwrap(angle(z)); % Phase spectrum
powerSpecInDb=20*log(magSpec+realmin); % Power in db
if plotOpt
% ====== Plot time-domain signals
subplot(3,1,1);
plot(time, signal, '.-');
title(sprintf('Input signals (fs=%d)', fs));
xlabel('Time (seconds)'); ylabel('Amplitude'); axis tight
% ====== Plot spectral power
subplot(3,1,2);
plot(freq, powerSpecInDb, '.-'); grid on
title('Power spectrum');
xlabel('Frequency (Hz)'); ylabel('Power (db)'); axis tight
% ====== Plot phase
subplot(3,1,3);
plot(freq, phaseSpec, '.-'); grid on
title('Phase');
xlabel('Frequency (Hz)'); ylabel('Phase (Radian)'); axis tight
end
% ====== Self demo (MathBio: Comment all of this out! )
function selfdemo
[y, fs]=wavread('welcome.wav');
frameSize=512;
startIndex=2047;
signal=y(startIndex:startIndex+frameSize+1);
signal=signal.*hamming(length(signal));
[magSpec, phaseSpec, freq, powerSpecInDb]=feval(mfilename, signal, fs, 1);

MATLAB: Plotting on one axes with a loop: solid line & legend

I have two distinct problems, but they're posted together because I believe the solutions are related. I'm testing Newton's and secant methods (each of which is implemented with a loop) and plotting the results versus computing time on the same axes to compare them. I want the (discrete) Newton's method results to be connected by a blue line and the secant method results by a red line. These lines, in turn, are annotated by a corresponding legend. This is not happening because each and every point on the plot seems to be considered at individual object because they were individually created. And the legend command brings up two blue asterisks instead a blue one and a red one (I wish I could post my plot here, but I don't have the image privilege yet.)
Here's my abbreviated code:
f = (x) % define function
figure
hold on
%% Newton
tic
while % terminating condition
% [Newtons method]
t = toc;
plot(t,log(abs(f(z)),'b*-')
end
%% Secant
tic
while % terminating condition
% [secant method]
t = toc;
plot(t,log(abs(f(z)),'r*-')
end
legend('Newton''s','Secant')
Needless to day, the '-' in the linespec doesn't do anything because only a point is being plotted, not a line. I know I could make a line plot with each iteration with something like plot([t_old t],[log(abs(f(z_old) log(abs(f(z)]), but that isn't ideal, not least because log(abs(f(z_old))) would have to be reevaluated each time. Besides, that would not solve the problem with the legend.
I think both problems will be solved if I can get MATLAB to understand that I'm trying to create just two objects on the axes, one blue line and one red line. Thank you.
If you don't want to store the x/y data in a vector and then replot the entire vector you could just add to the plotting line using code like this:
hNewton = [];
while % terminating condition
% [Newtons method]
t = toc;
if isempty(hNewton)
hNewton = plot(t,log(abs(f(z))),'b*-'); % First time through plot and save the line handle
else
% On all subsequent passes, just add to the lines X/Y data
set(hNewton,'XData',[get(hNewton,'XData') t]);
set(hNewton,'YData',[get(hNewton,'YData') log(abs(f(z)))]);
end
end
Since there are now only 2 lines, the legend works as expected.
Alternatively, you could put the code to add data to an existing line in a function
function hLineHandle = AddToLine( hLineHandle, xData, yData, lineStyle )
% AddToLine - Add data to a plotted line
if isempty(hLineHandle)
hLineHandle = plot(xData,yData, lineStyle);
else
set(hLineHandle,'XData',[get(hLineHandle,'XData') xData]);
set(hLineHandle,'YData',[get(hLineHandle,'YData') yData]);
end
end
Which makes the code in the main script/function a lot cleaner.
hNewton = [];
while % terminating condition
% [Newtons method]
t = toc;
hNewton = AddToLine(hNewton,t, log(abs(f(z))),'b*-' );
end
You can use line object, for example:
f = (x) % define function
figure
hold on
lHandle1 = line(nan, nan); %# Generate a blank line and return the line handle
lHandle2 = line(nan, nan); %# Generate a blank line and return the line handle
%% Newton
tic
while % terminating condition
% [Newtons method]
t = get(lHandle1, 'XData');
Y1 = get(lHandle1, 'YData');
t = toc;
Y1 = [Y1 log(abs(f(z)];
set(lHandle1, 'XData', t, 'YData', Y1, 'LineWidth', 2 ,'Color' , [0 1 0]);
end
%% Secant
tic
while % terminating condition
% [secant method]
t = get(lHandle2, 'XData');
Y2 = get(lHandle2, 'YData');
t = toc;
Y2 = [Y2 log(abs(f(z)];
set(lHandle2, 'XData', t, 'YData', Y2, 'LineWidth', 2 ,'Color' , [1 0 0]);
end
legend('Newton''s','Secant')
Good example of only showing the relevant parts of your code to ask a question. The others have explained tricks to have the legend behave as you want. I would go for a different solution, by saving your measurements in a vector and doing the plots after the loops. This has 2 advantages: you do not have to do the tricks with the legend, but more importantly, you are not doing a plot inside your loop, which potentially takes a lot of time. I would guess that your timing is dominated by the plotting, so the influence of your algorithm will hardly show up in the results. So change your code to something like this (untested):
f = (x) % define function
% preallocate plenty of space
[t_newton, t_secant, f_newton, f_secant] = deal(nan(1, 1000));
%% Newton
tic;
i = 1;
while % terminating condition
% [Newtons method]
f_newton(i) = current_result;
t_newton(i) = toc;
i = i + 1;
end
%% Secant
tic;
i = 1;
while % terminating condition
% [secant method]
f_secant(i) = current_result;
t_secant(i) = toc;
i = i + 1;
end
% trim NaNs (not really needed, not plotted anyhow)
t_newton = t_newton(isfinite(t_newton));
f_newton = f_newton(isfinite(f_newton));
t_secant = t_secant(isfinite(t_secant));
f_secant = f_secant(isfinite(f_secant));
% do the plot
semilogy(t_newton, abs(f_newton), t_secant, abs(f_secant))
legend('Newton''s','Secant')