0
$\begingroup$

I want to get the answer of my NDSolve in the form of table (the value of each x and y). Let's say that I want to copy this result into the excel or microsoft word and then put it in the appendix or make a chart from it.

I tried to use Reap and Sow function in order to do this.

s = NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]
Reap[Sow[s,{x,0,30}]

However, instead of giving me a number (x,y), it gives me an interpolation function. Anyone has an idea how to get x and y from this type of equation?

$\endgroup$
  • 1
    $\begingroup$ does this give what you need: f = y /. s // First; Table[{k, f[k]}, {k, 0, 30, .5}]? $\endgroup$ – kglr Feb 9 '13 at 4:32
  • 4
    $\begingroup$ Please confess you were joking about using Excel, please $\endgroup$ – Dr. belisarius Feb 9 '13 at 5:49
  • $\begingroup$ It should also be pointed out that the usage of Sow in the question is incorrect. The second argument of Sow is a tag to identify the object for later use, not a range specification as seems to be implied in {x, 0, 30}. In fact because of this error I think the question is probably not going to help anyone looking for Reap and Sow... $\endgroup$ – Jens Feb 9 '13 at 6:04
4
$\begingroup$

If you want to see the sampling points used internally, you can extract the properties of the InterpolatingFunction[] object like so:

s = y /. First @ NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}];

Short[pts = Transpose[Append[s["Coordinates"], s["ValuesOnGrid"]]], 10]
   {{0., 1.}, {0.00017069, 1.00009}, {0.00034138, 1.00018}, {0.00068276, 1.00037},
    {0.00102414, 1.00055}, {0.00136552, 1.00074}, {0.00477932, 1.00257},
    {0.00819312, 1.00439}, {0.0116069, 1.0062}, {0.0150207, 1.008},
    <<318>>, {28.7031, 0.0334113}, {28.8529, 0.0293757}, {29.0026, .0261519},
    {29.1524, 0.0236348}, {29.3022, 0.0217328}, {29.4519, 0.0203714}, {29.6017, 0.0194947},
    {29.7515, 0.0190656}, {29.8757, 0.019036}, {30., 0.0193018}}

If you need regularly spaced points, however, then you do what kguler says:

Table[{x // N, s[x]}, {x, 0, 30, 1/2}]
   {{0., 1.}, {0.5, 1.12111}, {1., 0.991387}, {1.5, 0.761165}, {2., 0.528358},
    {2.5, 0.338378}, {3., 0.206604}, {3.5, 0.128089}, {4., 0.0880718}, {4.5, 0.0731787},
    {5., 0.0773121}, {5.5, 0.103268}, {6., 0.16258}, {6.5, 0.264004}, {7., 0.37356},
    {7.5, 0.412578}, {8., 0.358411}, {8.5, 0.259648}, {9., 0.166528}, {9.5, 0.10145},
    {10., 0.064349}, {10.5, 0.0469302}, {11., 0.0424986}, {11.5, 0.0491715},
    {12., 0.0704073}, {12.5, 0.113928}, {13., 0.181682}, {13.5, 0.246869}, {14., 0.262814},
    {14.5, 0.22087}, {15., 0.154679}, {15.5, 0.0968125}, {16., 0.059186}, {16.5, 0.0392205},
    {17., 0.0310103}, {17.5, 0.0310352}, {18., 0.0394376}, {18.5, 0.0600941},
    {19., 0.0984724}, {19.5, 0.15096}, {20., 0.191026}, {20.5, 0.188813}, {21., 0.148847},
    {21.5,0.0993506}, {22., 0.060922}, {22.5, 0.0379967}, {23., 0.0268224},
    {23.5, 0.0233117}, {24., 0.025871}, {24.5, 0.0358221}, {25., 0.057114},
    {25.5, 0.0927678}, {26., 0.133994}, {26.5, 0.155512}, {27., 0.141107}, {27.5, 0.103738},
    {28., 0.0662557}, {28.5, 0.040382}, {29., 0.0262021}, {29.5, 0.0200394},
    {30., 0.0193018}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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