MATLAB: How to subplot two or more multiple-plots created by different loops

plotting

Hi,
I am trying to create a subplot(2,1), but I do not know how I should use this function when I have two plots created by differents loops. Here the code without subplot:
clear all; clc;
n=input('The value of n is:');
k=100;
X1=linspace(-20,20,k);
X2=linspace(-1,1,k);
M=zeros(n,k);
N=zeros(n,k);
figure(); hold on
for i = 1:n
plot(X1,besselj(i,X1));
legendInfo{i} = ['n = ' num2str(i)];
end
legend(legendInfo)
title('Bessel')
xlabel('X')
ylabel('Y')
hold off
figure(); hold on
for i = 1:n
plot(X2,legendre(i,X2));
legendInfo{i} = ['n = ' num2str(i)];
end
legend(legendInfo)
title('Legendre')
xlabel('X')
ylabel('Y')
hold off
My question is: Where I put the subplot function? I have tried and the only thing that I get is error.
Thank you very much.
Have a nice day.

Best Answer

Please put it just before 'plot' function, like:
n = input('The value of n is:');
k = 100;
X1 = linspace(-20,20,k);
figure
for kk = 1:n
subplot(n,1,kk)
plot(X1,besselj(kk,X1));
legend(['n = ' num2str(kk)])
if kk == 1
title('Bessel');
end
xlabel('X')
ylabel('Y')
end