1

My friends and I have been trying to resolve a localized problem in MATLAB code -

It deals with the "max" command - we are trying to find the maximum of R01 and R02, and plot this max against varying values of gamma1. The problem is in the for loop.\

The error message is:

In an assignment  A(I) = B, the number of elements in B and I must be the same.
Error in SolverDEqnsBoth (line 68)
R0(i) = max(transpose(R01),R02);

Here is the code:

B       = 2000;
mu      = 0.02;
deltaT  = 0.24;
deltaA  = 1.00;
alpha1  = 0.10;
alpha2  = 0.50;
beta1   = 2.00;
beta2   = 0.47;
gamma1  = 2.00;
gamma2  = 1.50;
v1      = 0.009;
v2      = 0.19;
c1      = 100;
c3      = 2.00;
c4      = 0.50;
N       = 35000;

%%%== Initial conditions ==
S0 = 10000;
H0 = 3000;
E10 = 3500;
E20 = 2000;
I10 = 4000;
I20 = 2500;
T10 = 3000;
T20 = 2000;
A10 = 1800;
A20 = 1700;
A30 = 800;
A40 = 700;

y0 = [S0 H0 E10 E20 I10 I20 T10 T20 A10 A20 A30 A40];

tf = 100;

[t y] = ode15s(@DEqnsBoth,[0:0.1:100],y0);

S = y(:,1);
H = y(:,2);
E1 = y(:,3);
E2 = y(:,4);
I1 = y(:,5);
I2 = y(:,6);
T1 = y(:,7);
T2 = y(:,8);
A1 = y(:,9);
A2 = y(:,10);
A3 = y(:,11);
A4 = y(:,12);


N = S + H + E1 + E2 + I1 + I2 + T1 + T2 + A1 + A2 + A3 + A4;

R01 =(beta1*c1*v1*B)/ ((mu + deltaT + gamma2)*(mu + v1 + gamma1))*mu*N;
R02 = (beta2*c3*B)/((mu+alpha1)*mu*N);

for i = 1:length(gamma1)
gamma1 = [0:0.005:5];
R0 = zeros(1, length(gamma1));
R0(i) = max(transpose(R01),R02);
end 
plot(gamma1,R0)

%plot(t,100.*( S)./N, 'r')
%hold on
%axis square
 %xlabel('days')
 %ylabel('Prevalence (% infected)')
 %axis([0 100 0 100])

Thanks in advance for any help.

  • For one thing, why do you redefine gamma1 inside the loop? Also, could pinpoint the line that gives you the error? – BoZenKhaa Apr 17 '14 at 18:50
  • I'm not sure. Line: R0(i) = max(transpose(R01),R02); –  Apr 17 '14 at 18:52
  • I changed the code to this: for i = 1:length(gamma1) R0 = zeros(1, 1001*length(gamma1)); R0(i) = max(transpose(R01),R02); end plot(gamma1,R0) –  Apr 17 '14 at 18:54

1 Answers1

1

try putting in:

 R0(i) = max([transpose(R01),R02]);

I'm getting an error, but it shouldn't be important though.

Undefined function 'DEqnsBoth' for input arguments of type 'double'.

Can you please write down what you get when you do the folowing:

size(R01)
size(R02)
WalyKu
  • 359
  • Thank you. This seems to work, except my R02 is something wrong. It's all zeros, except for the initial condition. So the graph is flatlining –  Apr 17 '14 at 18:56
  • R02 isn't even an array. It is just one value. You have probably missed something. – WalyKu Apr 17 '14 at 19:01
  • OK. I have: >> size(R01)

    ans =

        1001           1
    
    

    size(R02)

    ans =

           1        1001
    
    
    –  Apr 17 '14 at 19:03
  • Ok,

    gamma1 = [0:0.005:5]; R0 = zeros(1, length(gamma1)); for i = 1:length(gamma1) R0(i) = max([transpose(R01),R02]); end I can't suggest something better as I don't know what it is supposed to do.

    – WalyKu Apr 17 '14 at 19:09
  • 1
    Actually R0(i) = max([transpose(R01),R02]); will always give the same result because you always use the same R01 and R02. You should change something about it. – WalyKu Apr 17 '14 at 19:12