MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: export differential equation solution obtained through Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106344] Re: export differential equation solution obtained through Mathematica
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Fri, 8 Jan 2010 06:44:35 -0500 (EST)
  • References: <hi6t6f$a6k$1@smc.vnet.net>

Am 08.01.2010 10:17, schrieb Yun Zhao:
> Hi everyone,
> 
>   I would like to know if there is a way to export differential equation
> solution obtained through Mathematica to microsoft excel.  There are several
> data processing things that I want to know, that simply plotting the
> solution in Mathematica won't suffice.
> 
>   I have a system of differential equations like so:
> 
> 
> sol3=NDSolve[{p'[t]=-0.0491*p[t]-0.0089*p[t],n'[t]=0.0491*p[t]+(0.0491-0.0089)*n[t],p[0]=25000,n[0]=0},{p,n},{t,0,100}]
> 
> When I plotted it in Mathematica using this code:
> 
> Plot[Evaluate[{n[t]+p[t]}/.First[sol3]],{t,0,100}]
> 
> I get a exponential increasing function plotting number vs. time.
> 
> What I want is the values of both the time vector and number vector, and I
> want to copy and paste them into two columns in microsoft excel.
> 
> I tried using:
> 
> Export["newdata.dat", Evaluate[{n[t]+p[t]}/.First[sol3]]
> 
> But this gives me a .dat file with the time and number vectors all scrambled
> up.
> 
> My question is:
> 
> (1) is there a better way to tell Mathematica what I want to export, because
> I don't think Evaluate[{n[t]+p[t]}/.First[sol3] is the correct term to
> convey what I want

NDSolve returns InterpolatingFunctions, a concept I think is not known
to Excel, so when exporting to Excel, you need to extract an array of
numeric values, which are very straightforward to export to excel.

> (2) How do I properly export differential equation solutions that I can
> graph but not display in Mathematica, to spreadsheets software like
> Microsoft Excel?

A very simple approach is this:

Export[
 ToFileName[{$HomeDirectory, "Desktop"}, "nplusp.xls"],
 Table[{t, n[t] + p[t]} /. First[sol3], {t, 0, 100}]
 ]

of course you might want to adopt the sampling to your needs.

If you want to reflect the sampling points the values NDSolve was
actually using, you can use the function
InterpolatingFunctionCoordinates in the package
"DifferentialEquations`InterpolatingFunctionAnatomy`" to get a list of
the values NDSolve used:

Needs["DifferentialEquations`InterpolatingFunctionAnatomy`"]

Export[
 ToFileName[{$HomeDirectory, "Desktop"}, "nplusp.xls"],
 Map[
  {#, p[#] + n[#] /. sol3[[1]]} &,
  InterpolatingFunctionCoordinates[p /. sol3[[1]]][[1]]
  ]
 ]

hth,

albert


  • Prev by Date: Re: export differential equation solution obtained through Mathematica
  • Next by Date: Occurrences in Mathematica
  • Previous by thread: Re: export differential equation solution obtained through Mathematica
  • Next by thread: Occurrences in Mathematica