フィルターのクリア

Run simulation 1000 times

4 ビュー (過去 30 日間)
Luke Radcliff
Luke Radcliff 2016 年 7 月 13 日
コメント済み: Luke Radcliff 2016 年 7 月 14 日
This code I made is a simulation that finds the the total profit(yearprofit) from trading a product
Q= 240 + 9*randn(250,1);
for k = 1:length(Q)
if Q(k) < 240
buy = Q(k)*100 + 150;
elseif Q(k) > 245
sell = Q(k)*100 - 150;
buy = 0;
elseif buy > 0
P(k) = sell - buy;
yearprofit = sum(P);
end
end
This finds the profit for 1 year, how do I make the simulation run 1000 times giving me 1000 different values to resemble 1000 years? Do I have to use another for-loop around it?

採用された回答

James Tursa
James Tursa 2016 年 7 月 14 日
Yes. Use an outer for loop. E.g.,
n = 1000;
result = zeros(n,1);
for k=1:n
% your code
result(k) = whatever you want to save from your code
end
  1 件のコメント
Luke Radcliff
Luke Radcliff 2016 年 7 月 14 日
thank you sir

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by