Message Boards Message Boards

Problem with conditionals

Posted 10 years ago

Hi everyone,

I am having some problems with the if statment in Mathematica. I following code the program is taking the t value from the for loop statment (tmax) and doing the calculation. But I use the if statment to tell the program to do the calculations only for t<tmax. But it is ignoring. If t == tmax I tell the program to do specific calculations, but if t<tmax it must do another calculation. It is not working at all.

Anyone can help.

thanks

tmax = 20;
tmin = 1;
patchnum = 5;
xmax = 10;
xcrit = 3;
Array[alpha, 5];
Array[betai, 5];
Array[lamda, 5];
Array[Y, 5];

alpha[1] = 1;
alpha[2] = 1;
alpha[3] = 1;
alpha[4] = 1;
alpha[5] = 1;

betai[1] = 0.05;
betai[2] = 0.04;
betai[3] = 0.03;
betai[4] = 0.02;
betai[5] = 0.01;


lamda[1] = 0.5;
lamda[2] = 0.25;
lamda[3] = 0.166666;
lamda[4] = 0.125;
lamda[5] = 0.1;


Y[1] = 1;
Y[2] = 2;
Y[3] = 3;
Y[4] = 4;
Y[5] = 5;

Array[beh, {19, 10}];
Array[f, {20, 10}];

For[t = tmax, t >= 1, t--,
 For[x = xcrit, x <= xmax, x++,
  if [t == tmax,
    if [x <= xcrit,
      f[t, x] = 0, f[t, x] = 1
     ]]
   if [t < tmax,
    maxfit = -5;
    For[patch = 1, patch <= patchnum, patch++,
     x1 = x - alpha[patch] + Y[patch]
         if [x1 > xmax, x1 = xmax]
         if [x1 < xcrit,
          x1 = xcrit;
          x2 = x - alpha[patch];
          ]
         if [x2 > xmax, x2 = xmax;]
         if [x2 < xcrit, x2 = xcrit;]
         tempfit = (1 - 
          betai[patch])*(lamda[patch]*
           f[(t + 1), x1] + (1 - lamda[patch])*f[(t + 1), x2]);
     if [x2 <= xcrit,
        tempfit = (1 - betai[patch])*(lamda[patch]*
             f[(t + 1), x1]);] if [tempfit > maxfit,
        maxfit = tempfit;
        beh[t, x] = patch;
        ]
       f[t, x] = maxfit;
     ]
    ]
  ]
 ]
POSTED BY: Rafael Dettogni
2 Replies

Oh function names in Mathematica begin with capital letters. In fact, they are all written in what is called a CamelCase.

All of your if the statements should be If statements. Make that change and perhaps then your code will work as expected.

POSTED BY: David Reiss
Posted 10 years ago

Thanks, solved the problem!

POSTED BY: Rafael Dettogni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract