how to change value of a parameter inside a for loop
1 view (last 30 days)
Show older comments
Hi everyone this is my code clear all
a1 = 1;
a2 = 1;
a3 = 1;
b1=1;
b2=1;
b3=1;
alpha = 1.2;
F1 = 200;
F2 = 3;
M=0.8;
dt=0.01; % step size
t=0:dt:10; % time vector
W1_new = zeros(length(t),1);
W2_new = zeros(length(t),1);
W3_new = zeros(length(t),1);
f = zeros(length(t),1);
% Initial conditions for W1 and W2 given that W1 + W2 + W3 = 6
W1_new(1,1) = 1;
W2_new(1,1) = 2;
W3_new(1,1) = 3;
for i=1:length(t)-1
W1_new1 = W1_new(i,1) + dt*((a1*W1_new(i,1)^alpha - F1*W1_new(i,1))*W1_new(i,1)+b1*(M-W1_new(i,1)));
W2_new1 = W2_new(i,1) + dt*((a2*W2_new(i,1)^alpha - F1*W2_new(i,1))*W2_new(i,1)+b2*(M-W2_new(i,1)));
W3_new1 = W3_new(i,1) + dt*((a3*W3_new(i,1)^alpha - F1*W3_new(i,1))*W3_new(i,1)+b3*(M-W3_new(i,1)));
E_F1 = W1_new1 + W2_new1 +W3_new1 - 6;
W1_new2 = W1_new(i,1) + dt*((a1*W1_new(i,1)^alpha - F2*W1_new(i,1))*W1_new(i,1)+b1*(M-W1_new(i,1)));
W2_new2 = W2_new(i,1) + dt*((a2*W2_new(i,1)^alpha - F2*W2_new(i,1))*W2_new(i,1)+b2*(M-W2_new(i,1)));
W3_new2 = W3_new(i,1) + dt*((a3*W3_new(i,1)^alpha - F2*W3_new(i,1))*W3_new(i,1)+b3*(M-W3_new(i,1)));
E_F2 = W1_new2 + W2_new2 + W3_new2- 6;
f (i,1) = (F1*E_F2-F2*E_F1)/(E_F2-E_F1); % the value of f we keep
W1_new(i+1,1) = W1_new(i,1) + dt*((a1*W1_new(i,1)^alpha - f(i,1)*W1_new(i,1))*W1_new(i,1)+b1*(M-W1_new(i,1)));
W2_new(i+1,1) = W2_new(i,1) + dt*((a2*W2_new(i,1)^alpha - f(i,1)*W2_new(i,1))*W2_new(i,1)+b2*(M-W2_new(i,1)));
W3_new(i+1,1) = W3_new(i,1) + dt*((a3*W3_new(i,1)^alpha - f(i,1)*W3_new(i,1))*W3_new(i,1)+b3*(M-W3_new(i,1)));
E_f = W1_new(i+1,1) + W2_new(i+1,1) + W3_new(i+1,1) - 6; % this quantity has to be equal to zero
end
end
plot(t, W1_new,'r')
hold on
plot(t,W2_new,'r')
hold on
plot(t, W3_new,'r')
I WOULD LIKE TO CHANGE THE VALUE OF THE PARAMETER M INSIDE THE FOR LOOP. WHEN T>1.5 THEN M=0.5 RATHER THAN THE SET VALUE OF M=0.8 AND PLOT THIS
THANKS!
0 Comments
Answers (1)
Star Strider
on 22 Aug 2014
Just after your for statement, add the two following lines:
for i=1:length(t)-1
ti = t(i);
M = [0.8*(ti <= 1.5) + 0.5*(ti > 1.5)];
... CODE ...
That works.
0 Comments
See Also
Categories
Find more on Time Series in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!