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

2 views (last 30 days)
gamma(t) =4-(6/90*t);

Accepted Answer

Ameer Hamza
Ameer Hamza on 9 Apr 2020
Edited: Ameer Hamza on 10 Apr 2020
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 Comments
Fae ba
Fae ba on 10 Apr 2020
Edited: Fae ba on 10 Apr 2020
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!