MATLAB: I want this to run 3 times with values of p =5 10 and 20 and graph each of them in subplot instead of having p=input

MATLABsubplot

if true
% code
end
t=linspace(-2,2,101);
xa=zeros(1,size(t,2));
xp=zeros(1,size(t,2));
p=input('please input value for p:');
for i=1:1:size(t,2)
xa(i)=1-2*sin(pi*t(i))+cos(2*pi*t(i))+3*cos(3*pi*t(i));
end
for i=1:1:size(t,2)
for k=-p:1:p
Xa=1-2*sin(pi*2*k)+cos(2*pi*2*k)+3*cos(3*pi*2*k);
s=6*pi*(t(i)-2*k);
xp(i)=xp(i)+Xa*u_sinc(s);
end
end
figure(2)
plot(t,xp,'b');
hold on
plot(t,xa,'r');

Best Answer

Try this:
t=linspace(-2,2,101);
xa=zeros(1,size(t,2));
xp=zeros(1,size(t,2));
figure(2)
p = [5 10 20];
for k1 = 1:length(p);
for i=1:1:size(t,2)
xa(i)=1-2*sin(pi*t(i))+cos(2*pi*t(i))+3*cos(3*pi*t(i));
end
for i=1:1:size(t,2)
for k=-p:1:p
Xa=1-2*sin(pi*2*k)+cos(2*pi*2*k)+3*cos(3*pi*2*k);
s=6*pi*(t(i)-2*k);
xp(i)=xp(i)+Xa*u_sinc(s);
end
end
subplot(length(p),1,k1)
plot(t,xp,'b');
hold on
plot(t,xa,'r');
hold off
end