Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am solving two simple differential equation using NDsolve.

solution = NDSolve[{X'[t] == μ*S[t]*X[t]/(k + S[t]), 
                    S'[t] == -μ*S[t]*X[t]/(0.664252*(k + S[t])),
                    X[0] == 10,S[0] == 300}, {X, S}, {t, 0, 40.7}]
 bacteria[t_] = X[t] /. solution;
 food[t_] = S[t] /. solution;
 qq = Table[310 - bacteria[t] - food[t], {t, time}];
 error = MapThread[Plus,Abs[(exp[[All, 1]] - qq)/exp[[All, 1]]*100/298]]

I need to evaluate error for different values of k and μ in order to check the values of k and μ for which error is less than 5%. exp and time are results which I have.

When I try to put my code in a For-loop, it fails.

For[μ = 0.1, μ <= 1, μ = μ + 0.1,
  k = 100
  solution = NDSolve[{X'[t] == μ*S[t]*X[t]/(k + S[t]), 
                      S'[t] == -μ*S[t]*X[t]/(0.664252*(k + S[t])), 
                      X[0] == 10, S[0] == 300}, {X, S}, {t, 0, 40.7}]
  bacteria[t_] = X[t] /. solution;
  food[t_] = S[t] /. solution;
  qq = Table[310 - bacteria[t] - food[t], {t, time}];
  error = MapThread[Plus,Abs[(exp[[All, 1]] - qq)/exp[[All, 1]]*100/298]]
  Print[error]]

The above is giving me errors and I do not get any results. My plan is to use this For-loop to vary μ and then use a nested loop for varying k.

Any comments?

share|improve this question
    
There are some simple mistakes in your code that must be corrected before we can see if there is a more serious error. 1) exp and time must have values defined for them. 2) semicolons (;) are needed after k = 100 and solution = NDSolve[...] –  m_goldberg May 4 at 10:30
    
@m_goldberg I did put semicolon as told you but still I am getting the same error. And I have pre defined values of exp and time. –  user11948 May 4 at 21:09
    
If I understand what you are doing correctly, you are trying loop through 100 values of k, for each value of u. I would use Table for this. Judging from your specification of exp in your code, I am suspecting that exp has to be a list of values, as in a time series. For each time point that you have, do you have a experimental data point? If so, it seems as if you would want to fit that your model to that data, where k and u are your free parameters. For two parameters it shouldn't be too expensive to take this brute force approach that you have. But look into Monte Carlo Methods as well. –  tarhawk May 21 at 0:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.