Working with numerical solutions in mathematica

  • Context: Mathematica 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Mathematica Numerical
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 3K views
member 428835
Hi PF!

I have just solved a non-linear ODE numerically in mathematica using the "NDSolve" command over a small interval ##[0,1.5]##. I'd like to be able to fit a polynomial to this solution, perhaps using the "Fit" command, although I've had no luck on my own.

Any help is greatly appreciated!
 
Physics news on Phys.org
joshmccraney said:
Hi PF!

I have just solved a non-linear ODE numerically in mathematica using the "NDSolve" command over a small interval ##[0,1.5]##. I'd like to be able to fit a polynomial to this solution, perhaps using the "Fit" command, although I've had no luck on my own.

Any help is greatly appreciated!
Can you give some details of what you have tried?
 
Sure! I typed "data = {NDSolve[{
NDSolve[{y[x] y''[x] + 2 (y'[x])^2 + x y'[x] == 0, y[1] == .00000001,
y'[1] == -1/2}, y, {x, 0, 3/2}]};

Then I typed "parabola = Fit[data,{1,x,x^2},x]"

but I had no luck. I think this is because I am not sure what the NDSolve command is giving me. I can graph it, and it looks simple enough. I just don't know how to deal with it.

Also, when I try to integrate over the NDSolve with respect x there is no problem. However, when I integrate NDSolve with respect to x but include a constant, say something like Ax^2 + Bx + C, I get some sort of error.

Can you help with both of these issues?

Thanks so much fro taking an interest!
 
What NDSolve gives you is an interpolation function, from which you can extract values for different x. The way I found to do what you want, which might not be elegant or the better way to do it with Mathematica, is to first create table of data points, and then work with those points:
Code:
points = Table[{x, Evaluate[y[x] /. data][[1]]}, {x, 0, 1, 0.1}];
parabola = Fit[points, {1, x, x^2}, x]

joshmccraney said:
Also, when I try to integrate over the NDSolve with respect x there is no problem. However, when I integrate NDSolve with respect to x but include a constant, say something like Ax^2 + Bx + C, I get some sort of error.
What is it exactly you want to integrate?
 
  • Like
Likes   Reactions: member 428835
DrClaude said:
What is it exactly you want to integrate?
I want to integrate the numerical solution, call this ##y(x)##, and a quadratic. So I want to do this $$\int_0^1 (y(x)-(Ax^2+Bx+C))\, dx$$
 
And I'm not sure what I'm doing wrong but mathematica is not giving me a solution for the quadratic. After inputting the NDSolve I copied what you posted into mathematica, and it is giving me out 10 responses. Am I doing something wrong here? (Sorry if these are annoying questions, I'm trying to learn mathematica).