How to optimize this program using a for loop?

1 visualizzazione (ultimi 30 giorni)
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))

Risposta accettata

jgg
jgg il 14 Lug 2016
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
  1 Commento
David Zarate Villegas
David Zarate Villegas il 14 Lug 2016
the thing is that I use the previous calculated term, I use fm2 to calculate fm3, and fm3 to calculate fm4, and so on, does your code works with that change?

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by