2
$\begingroup$

I am going to use Nintegrate to integrate the result of NDsolve and also a Table. How I can do this? My code is:

Clear["Global`*"]

yy = 10^-4;
rr = 0.999;
xx = 10^-15;
zz = 10^-4;
mm = 10^-4;

ic = -17.5

s = NDSolve[{D[y[t], 
     t] == (3 y[t])/5 - (12 m[t]^2 y[t])/5 + (2 r[t] y[t])/
      5 - (6 x[t]^2 y[t])/5 + (3 y[t]^2)/5 + (7 y[t] z[t])/
      5 - (y[t] z[t]^2)/10, 
   D[r[t], t] == -((2 r[t])/5) - (12 m[t]^2 r[t])/5 + (2 r[t]^2)/
      5 - (6 r[t] x[t]^2)/5 + (3 r[t] y[t])/5 + (7 r[t] z[t])/
      5 - (r[t] z[t]^2)/10, 
   D[x[t], t] == (9 x[t])/5 - (6 m[t]^2 x[t])/5 + (r[t] x[t])/
      5 - (3 x[t]^3)/5 + (3 x[t] y[t])/10 + (x[t] z[t])/
      5 - (x[t] z[t]^2)/20, 
   D[z[t], t] == 
    12/5 + (12 m[t]^2)/5 - (12 r[t])/5 - (24 x[t]^2)/5 - (18 y[t])/
      5 - (18 z[t])/5 - (6 m[t]^2 z[t])/5 + (r[t] z[t])/
      5 - (3 x[t]^2 z[t])/5 + (3 y[t] z[t])/10 + (13 z[t]^2)/10 - 
     z[t]^3/20, 
   D[m[t], t] == -2 Sqrt[3] - (6 m[t])/5 - 
     2 Sqrt[3] m[t]^2 - (6 m[t]^3)/5 + 
     2 Sqrt[3] r[t] + (m[t] r[t])/5 + 
     2 Sqrt[3] x[t]^2 - (3 m[t] x[t]^2)/5 + 
     2 Sqrt[3] y[t] + (3 m[t] y[t])/10 + 
     2 Sqrt[3] z[t] + (6 m[t] z[t])/5 - 
     z[t]^2/(4 Sqrt[3]) - (m[t] z[t]^2)/20, x[ic] == xx, y[ic] == yy, 
   m[ic] == mm, z[ic] == zz, r[ic] == rr}, {x, y, z, m, r}, {t, ic, 
   10}]

I use the result to find

H = Table[
  Exp[NIntegrate[z[t]/5 +  (z[t]^2)/20 /. 
     First@s, {t, 0, i}]], {i, -17.5, 10, 0.2}]

Now I am going to compute:

NIntegrate[Exp[-t] x[t]^2 (H)^-1 , {t, 0, -7}] 

but this does not work. In fact I dont know how to call H and x[t] together?

$\endgroup$
8
  • 1
    $\begingroup$ Sara, it doesn't make much sense to me to multiply your integrand by a table of numbers. Can you show us what mathematical expression yo are trying to reproduce? $\endgroup$
    – MarcoB
    Jun 21, 2016 at 13:18
  • $\begingroup$ Did you forget a /. First@s in your last NIntegrate? Is H supposed to represent the interpolation of the table? $\endgroup$
    – Michael E2
    Jun 21, 2016 at 13:19
  • $\begingroup$ @MarcoB, In fact, this is a physical expression and I compute different parts with mathematica. The last step is integrating these quantities which originate from different parts. In other words, I dont know how to first, behave H as a function to be integrated beside x[t] and second how to gather H and x[t] with the analytical function Exp[t]. $\endgroup$
    – sara
    Jun 21, 2016 at 14:06
  • $\begingroup$ @MichaelE2, because of the existence of H, using /. First@s does not work. In fact I tried different type of writing my purpose and I couldn`t get result. $\endgroup$
    – sara
    Jun 21, 2016 at 14:09
  • 1
    $\begingroup$ @MichaelE2, It seems I was not specific enough, sorry, I want to first make a function out of H then integrate the expression so that the result is a number. $\endgroup$
    – sara
    Jun 21, 2016 at 15:37

1 Answer 1

5
$\begingroup$

I think this gives what you want: We construct the integral inside the definition of H by adding another ODE to the NDSolve system, which I called logH. This in fact calculates the integral from ic, not from 0. So to define H we need to subtract logH[0] from logH[t] before exponentiating. This should be a much more accurate (and faster) way of computing H than interpolating a table.

ClearAll[x, y, z, m, r, logH, t];

yy = 10^-4;
rr = 0.999;
xx = 10^-15;
zz = 10^-4;
mm = 10^-4;

ic = -17.5;

s = NDSolve[{D[y[t], 
      t] == (3 y[t])/5 - (12 m[t]^2 y[t])/5 + (2 r[t] y[t])/
       5 - (6 x[t]^2 y[t])/5 + (3 y[t]^2)/5 + (7 y[t] z[t])/
       5 - (y[t] z[t]^2)/10, 
    D[r[t], t] == -((2 r[t])/5) - (12 m[t]^2 r[t])/5 + (2 r[t]^2)/
       5 - (6 r[t] x[t]^2)/5 + (3 r[t] y[t])/5 + (7 r[t] z[t])/
       5 - (r[t] z[t]^2)/10, 
    D[x[t], t] == (9 x[t])/5 - (6 m[t]^2 x[t])/5 + (r[t] x[t])/
       5 - (3 x[t]^3)/5 + (3 x[t] y[t])/10 + (x[t] z[t])/
       5 - (x[t] z[t]^2)/20, 
    D[z[t], t] == 
     12/5 + (12 m[t]^2)/5 - (12 r[t])/5 - (24 x[t]^2)/5 - (18 y[t])/
       5 - (18 z[t])/5 - (6 m[t]^2 z[t])/5 + (r[t] z[t])/
       5 - (3 x[t]^2 z[t])/5 + (3 y[t] z[t])/10 + (13 z[t]^2)/10 - 
      z[t]^3/20, 
    D[m[t], t] == -2 Sqrt[3] - (6 m[t])/5 - 
      2 Sqrt[3] m[t]^2 - (6 m[t]^3)/5 + 
      2 Sqrt[3] r[t] + (m[t] r[t])/5 + 
      2 Sqrt[3] x[t]^2 - (3 m[t] x[t]^2)/5 + 
      2 Sqrt[3] y[t] + (3 m[t] y[t])/10 + 
      2 Sqrt[3] z[t] + (6 m[t] z[t])/5 - 
      z[t]^2/(4 Sqrt[3]) - (m[t] z[t]^2)/20, x[ic] == xx, y[ic] == yy,
     m[ic] == mm, z[ic] == zz, r[ic] == rr,
    (* add this line *)
    logH'[t] == z[t]/5 + (z[t]^2)/20, logH[ic] == 0
    },
   {x, y, z, m, r, logH}, {t, ic, 10}];

Now construct H from logH.

ClearAll[H];
With[{logH = logH /. First@s},
  H[t_] := Exp[logH[t] - logH[0]]];

H[t]

Mathematica graphics

We can see an image of the plot of logH inside the InterpolatingFunction in the image above.

Finally, here is the integral:

NIntegrate[
 Evaluate[Exp[-t] x[t]^2 (H[t])^-1 /. First@s],
 {t, 0, -7}]
(*  -2.25183  *)
$\endgroup$
1
  • $\begingroup$ That`s precious. Thank you.. @Michael E2 $\endgroup$
    – sara
    Jun 22, 2016 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.