Working with numerical solutions in mathematica

In summary, the conversation is about trying to fit a polynomial to a numerical solution of a non-linear ODE in Mathematica. The person is having trouble using the "Fit" command and is seeking help with integrating the solution with a quadratic. The expert explains that the "NDSolve" command gives an interpolation function, and suggests creating a table of data points to work with instead. They also ask for clarification on the integration problem.
  • #1
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
  • #2
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?
 
  • #3
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
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 member 428835
  • #5
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$$
 
  • #6
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).
 

1. How can I input numerical data into Mathematica?

There are a few ways to input numerical data into Mathematica. You can use the "List" command to input a list of numbers, or use the "Table" command to create a table of numbers. You can also import data from external sources such as CSV or Excel files. Additionally, you can use the "Manipulate" command to create interactive sliders and input boxes for numerical values.

2. How do I solve equations numerically in Mathematica?

To solve equations numerically in Mathematica, you can use the "NSolve" command. This command will attempt to find numerical solutions for the given equation. You can also use the "FindRoot" command to find a numerical solution for a single equation, or the "NDSolve" command to numerically solve differential equations.

3. Can I plot numerical solutions in Mathematica?

Yes, you can plot numerical solutions in Mathematica using the "Plot" command. Just input the numerical expression or function and specify the range of values you want to plot. You can also use the "ListPlot" command to plot a list of numerical data points.

4. How can I evaluate a numerical expression in Mathematica?

To evaluate a numerical expression in Mathematica, you can use the "Evaluate" command. This will force Mathematica to immediately evaluate the expression and give you the numerical result. You can also use the "N" function to evaluate a numerical expression to a given precision.

5. How can I use numerical methods in Mathematica to solve real-world problems?

There are many numerical methods available in Mathematica that can be used to solve real-world problems. Some examples include using the "Interpolation" function to approximate a function from a set of data points, using the "Fit" function to find a best-fit curve for a set of data, and using the "NIntegrate" function to numerically integrate a function. You can also create your own custom numerical methods using Mathematica's programming capabilities.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
419
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
58
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
24
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
Back
Top