I have a question regarding time dependent loop, as I want to have a variable changing with time in the loop from 0 to 30

1 回表示 (過去 30 日間)
gamma(t) =4-(6/90*t);

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 9 日
編集済み: Ameer Hamza 2020 年 4 月 10 日
Without loop:
t = linspace(0,30,100); % create 100 points between 0 and 30
gamma = 4-(6/90*t);
Using for loop:
t = linspace(0,30,100);
gamma = zeros(size(t));
for i=1:numel(t)
gamma(i) = 4-(6/90*t(i));
end
  10 件のコメント
Fae ba
Fae ba 2020 年 4 月 10 日
編集済み: Fae ba 2020 年 4 月 10 日
I want to develope the code to the case where the gamma is changes in every loop iteration by assuming that x is changing between 0 and 5, then how can I plot (x, c1) wrt to three cases of gamma for a fixed point eg t = 5? I define f (t) and v1, v2 and c1 just for the first case, is there any way to define the same for the two other case where there are gammaB and gammaC with the same formula? this is the primary code:
r = 0.02;
mu = 0.08;
sigma = 0.2;
T = 30;
rho = 0.02;
for x = 0: 1: 5
t = 5;
gammaA (i) = 4;
gammaB (j) = 5+ (0.07 * t (j));
gammaC (k) = 5- (0.07 * t (k));
for i = 0:30
f1 = (exp (-rho * t) / gammaA (i)) * exp (((1-gammaA (i)) / gammaA (i)) * (r + (mu - r) ^ 2 / (2 * sigma ^ 2 * gammaA (i))) * (T - t));
v1 = (f1. ^ gammaA (i)). * (x. ^ - gammaA (i));
C1 = exp (-rho * t / gammaA (i)) * (v1. ^ (-1 / gammaA (i)));
end
for j = 0:30
f2 = (exp (-rho * t) / gammaB (j)) * exp (((1-gammaB (j)) / gammaB (j)) * (r + (mu - r) ^ 2 / (2 * sigma ^ 2 * gammaB (j))) * (T - t));
v2 = (f2 ^ gammaB (j)) * (x. ^ - gammaB (j));
C2 = exp (-rho * t / gammaB (j)) * (v2. ^ (-1 / gammaB (j)));
end
for k = 0:30
f3 = (exp (-rho * t) / gammaC (k)) * exp (((1-gammaC (k)) / gammaC (k)) * (r + (mu - r) ^ 2 / (2 * sigma ^ 2 * gammaC (k))) * (T - t));
v3 = (f3 ^ gammaC (k)) * (x. ^ - gammaC (k));
C3 = exp (-rho * t / gammaC (k)) * (v3. ^ (- 1 / gammaC (k)));
end
set (0, 'DefaultAxesColorOrder', [0 0 0], 'DefaultAxesLineStyleOrder', '- | -. | -')
hold on
plot (x, c1, x, c2, x, c3)
legend ('gammaA', 'gammaB', 'gammaC')
hold off
Ameer Hamza
Ameer Hamza 2020 年 4 月 10 日
The question is confusing. Can you clarify it?

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by