How to optimize this program using a for loop?

1 回表示 (過去 30 日間)
David Zarate Villegas
David Zarate Villegas 2016 年 7 月 14 日
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))

採用された回答

jgg
jgg 2016 年 7 月 14 日
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 件のコメント
David Zarate Villegas
David Zarate Villegas 2016 年 7 月 14 日
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?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by