# Working with numerical solutions in mathematica

1. Apr 8, 2015

### joshmccraney

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!

2. Apr 9, 2015

### Staff: Mentor

Can you give some details of what you have tried?

3. Apr 9, 2015

### joshmccraney

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!

4. Apr 10, 2015

### Staff: Mentor

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 (Text):

points = Table[{x, Evaluate[y[x] /. data][[1]]}, {x, 0, 1, 0.1}];
parabola = Fit[points, {1, x, x^2}, x]

What is it exactly you want to integrate?

5. Apr 10, 2015

### joshmccraney

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$$

6. Apr 10, 2015

### joshmccraney

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).