MATLAB: How to optimize this program using a for loop

repeating a process several times

clear
close
clc
x=linspace(-5,5,1000);
y = sin(x.^2).*cos(x.^2);
fx = exp(-x.^2);
fm1 = ifft(fft(fx)/norm(fx).*y);
figure(1)
plot(abs(fm1))
fm2 = ifft(fft(fm1)/norm(fm1).*y);
figure(2)
plot(abs(fm2))
fm3 = ifft(fft(fm2)/norm(fm2).*y);
figure(3)
plot(abs(fm3))
fm4 = ifft(fft(fm3)/norm(fm3).*y);
figure(4)
plot(abs(fm4))
fm5 = ifft(fft(fm4)/norm(fm4).*y);
figure(5)
plot(abs(fm5))
fm6 = ifft(fft(fm5)/norm(fm5).*y);
figure(6)
plot(abs(fm6))

Best Answer

As far as I can tell, you're just doing this:
fm1 = ifft(fft(fx)/norm(fx).*y);
figure(1)
plot(abs(fm1))
fm2 = ifft(fft(fm1)/norm(fm1).*y);
several times. So, just replacing that latter section of your code with a loop should work:
fm = ifft(fft(fx)/norm(fx).*y);
figure(1)
plot(abs(fm))
for i = 2:6
fm = ifft(fft(fm)/norm(fm).*y);
figure(i)
plot(abs(fm))
end
Related Question