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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Geology에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by