Message Boards Message Boards

0
|
9941 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Evaluate an Interpolating function and export results as a table?

Hello, this is a ridiculously simple question but I have been working on this far too long and I'm getting no where.

I would like to produce a table of output values based on an interpolating function. The interpolating function is a solution to a nonlinear differential equation and I am able to plot the solution. Frustratingly, I am not able to evaluate the function at specific points or make a table of output values. Ultimately, I'd like to export a table of values say at points t=0 to t=10 that I can import into Excel.

Thank you!! Scott Vaughen

Attachments:
5 Replies

Ok!! Yes!!! I'm going to try this!! thank you - Michael, your very helpful reply was posted while I was typing out my response to Bill

Thank you!!

Thank you again Bill and Michael!

I basically got it now, attached is my result.... this will eventually become a lesson in my class, thank you!

-Scott

Attachments:

I would drop the [t] from v[t] in the requested form of the solution, and possibly use NDSolveValue, depending on how I wanted to use the solution.

sol = First@NDSolve[DE, v, {t, 0, 10}]  (* returns a rule  v -> InterpolatingFunction[..] *)

vIF = NDSolveValue[DE, v, {t, 0, 10}]  (* return an  InterpolatingFunction[..] *)

Then you can evaluate v like this:

v[4] /. sol
(*  225.484  *)

vIF[4]
(*  225.484  *)

To get a table of the actual steps taken by NDSolve, you can take advantage of built-in methods of InterpolatingFunctions that allow you some access to the internal data:

vIF@"Methods"
(*
  {"Coordinates", "DerivativeOrder", "Domain", "ElementMesh", 
   "Evaluate", "GetPolynomial", "Grid", "InterpolationMethod", 
   "InterpolationOrder", "MethodInformation", "Methods", 
   "OutputDimensions", "Periodicity", "PlottableQ", "Properties", 
   "QuantityUnits", "Unpack", "ValuesOnGrid"}
*)

The following returns a table of {t, v} steps:

soltab = Transpose@{Flatten@vIF@"Grid", vIF@"ValuesOnGrid"};

The following returns a table of {t, v, v'} including the values of the derivative v', in case you want to use the table to do Hermite interpolation in another application:

soltab = Transpose@{Flatten@vIF@"Grid", vIF@"ValuesOnGrid", vIF'@"ValuesOnGrid"};

But if you want an equal-step table, you can use Table and either sol or vIF:

soltab = Table[{t, v[t] /. sol}, {t, 0., 10.}]
soltab = Table[{t, vIF[t]}, {t, 0., 10.}]

The tables can be exported as Bill Nelson shows:

Export["file.csv", soltab]
POSTED BY: Michael Rogers
Posted 5 years ago

Perhaps you can adapt this

sol=v[t]/.NDSolve[DE,v[t],{t,0,10}][[1]];
points=Table[{t,sol},{t,0,10,1}];
Export["file.csv",points]
POSTED BY: Bill Nelson

Thank you!!! Thank you!!! Yes, I am making some progress with this!!

I can eventually turn this into a lesson for my class!

I wasn't able to retrieve the .csv file, I just got the output line "file.csv".... but I couldn't find the file, maybe it's because I'm currently using the free version of the wolfram programming lab.

But I was able to actually see all the results of my interpolating function evaluated at a series of input values for t=0 to t=10. That was my main goal. At this point, I'm just typing the data into excel based on what I can read from the output.

THANK YOU THANK YOU for the quick reply!!!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract