3

Suppose i have an equation

NDSolve[{ca'[t]== -0.1*ca[t]*cb[t], cb'[t]== -0.1*2*ca[t]*cb[t], 
  ca[0]== 10, cb[0]== 12},{ca,cb},{t,0,5}]

how can i get the solution in pure function form(even if i don't consider time limit) of above equation.

There is an option with DSolve to get pure function for linear DE. But how to deal with non linear DE to get pure function ?

halirutan
  • 112,764
  • 7
  • 263
  • 474
swapnil90
  • 31
  • 1

2 Answers2

3

What you get as result are InterpolatingFunction objects. These can be used like pure functions. Therefore, you can transform the result of NDSolve so that you can call it like a function:

sol = NDSolve[{ca'[t] == -0.1*ca[t]*cb[t],cb'[t] == -0.1*2*ca[t]*cb[t], 
  ca[0] == 10, cb[0] == 12}, {ca, cb}, {t, 0, 5}];

f = With[{exp = Through[({ca, cb} /. First[sol])[#]]}, exp &];

Now you can use f

Plot[f[x], {x, 0, 5}]

Mathematica graphics

Does this help?

halirutan
  • 112,764
  • 7
  • 263
  • 474
  • actually i was looking for the solution in form {{ca->Function[{t},........], cb->Function[{t},.....]}} so that i can further use this function for optimization – swapnil90 Dec 05 '14 at 05:35
  • @swapnil90 You cannot use NDSolve to get a symbolic solution, except in terms of InterpolatingFunction. You can optimize such solutions. See this question, the question linked in it, and their answers. If they don't address your situation and you can find nothing else on the site that does, consider asking another question about how to do exactly what you want to do. – Michael E2 Dec 05 '14 at 11:16
1

In V10, DSolve gives the answer:

{sol} = DSolve[
  {ca'[t] == -1/10*ca[t]*cb[t], cb'[t] == -1/10*2*ca[t]*cb[t], ca[0] == 10, cb[0] == 12},
  {ca, cb}, t]

Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >>

(*
  {{cb -> Function[{t}, 24/(-3 + 5 E^(4 t/5))], 
    ca -> Function[{t}, (20 E^(4 t/5))/(-3 + 5 E^(4 t/5))]}}
*)
Plot[{ca[t], cb[t]} /. sol // Evaluate, {t, 0, 5}]

Mathematica graphics

Michael E2
  • 235,386
  • 17
  • 334
  • 747