RE: Plot3D with NDSolve
- To: mathgroup at smc.vnet.net
- Subject: [mg82016] RE: Plot3D with NDSolve
- From: "Andrew Moylan" <andrew.j.moylan at gmail.com>
- Date: Tue, 9 Oct 2007 05:43:34 -0400 (EDT)
- References: <fdnlg5$klu$1@smc.vnet.net> <feca11$fl1$1@smc.vnet.net> <4709E5FD.3090201@gmail.com>
Szabolcs, you are right: one has to be careful with this sort of caching manouvre, because Plot3D is liable to try to evaluate solution[a][x] for all sorts of values of a. We can avoid this problem by using FunctionInterpolation. We know that FunctionInterpolation will only evaluate its argument on rectangular lattice points when constructing its 2-D interpolation. This means not too many different values of solution[a] are cached. To see this, add a Print[a] statement to the definition of solution[a]. Here is the entire proposed solution again, with the Print[a] statement added: eqn[(a_)?NumericQ] = Derivative[1][y][x] == b/(a*y[x]) /. b -> 1 solution[(a_)?NumericQ] := ( Print[a]; solution[a] = Block[{x}, y /. NDSolve[{eqn[a], y[0] == 0.1}, y, {x, 0.1, 5}][[1]]] ) solution3d = FunctionInterpolation[solution[a][x], {a, 1, 5}, {x, 0.1, 5}] Plot3D[solution3d[a, x], {a, 1, 5}, {x, 0.1, 5}] When you do it this way, Plot3D can (and does) evaluate the resulting interpolating function as much as it wants, at all sorts of values of a, but the expensive solution[a] is only evaluated a few times. Andrew Moylan (PhD student) Department of Physics, Faculty of Science Physics Building 38a, Corner Science and Daley Roads The Australian National University Canberra ACT 0200 Australia M: 0402 4757 04 T: +61 (0)2 6125 9977 F: +61 (0)2 6125 0741 W: http://gr.anu.edu.au/ CRICOS Provider #00120C -----Original Message----- From: Szabolcs Horv=E1t [mailto:szhorvat at gmail.com] Sent: Monday, 8 October 2007 6:11 PM To: Andrew Moylan Subject: [mg82016] Re: Plot3D with NDSolve Andrew Moylan wrote: > On Sep 30, 6:07 pm, sean_incali <sean_inc... at yahoo.com> wrote: >> Hello group, >> >> Let's say I can solve the following DE that depends on 2 paparmeters >> a and b, and plot it accordingly. >> >> eqn = y'[x] == b/(a y[x]) >> par = {a -> 1, b -> 1} >> >> solution = NDSolve[{eqn /. par, y[0] == 0.1}, y, {x, 0.1, 5}]; >> >> Plot[y[x] /. solution, {x, 0.1, 5}]; >> >> Above shows a 2d graph in x and y axes. >> >> What I want to do is now use one of the parameter as z axis. >> >> So I need to solve the DE while varying a from 1 to 5 for instance. >> >> Then I want to graph the solutions as a 3D object. with x. y and a >> axis (where a will be the new z axis.) >> >> I guess I can use Table to iterate the whole procedure, but I wanted >> to see how others would approach it. >> >> For instance, if I use >> >> eqn = y'[x] == b/(a y[x]) >> par = {b -> 1} >> >> solution = Table[NDSolve[{eqn /. par, y[0] == 0.1}, y, {x, 0.1, 5}], >> {a, 1, 5}]; >> >> Plot[Evaluate[y[x] /. solution], {x, 0.1, 5}]; >> >> It will shows all the solutions in one 2D graph. I want to see them >> in 3D. >> >> On the side note.. Why does the Plot require the "Evaluate" in the >> second code I posted and not in the first code??? >> >> If someone can explain that that will be great also. >> >> Thanks in advance as usual. >> >> sean > > > Here's a way Sean: > > eqn[(a_)?NumericQ] = Derivative[1][y][x] == b/(a*y[x]) /. b -> 1 > > solution[(a_)?NumericQ] := solution[a] = > Block[{x}, > y /. NDSolve[{eqn[a], y[0] == 0.1}, y, {x, 0.1, 5}][[1]]] Yes, this result caching technique is what came to my mind first when I read the question of the OP, but the problem is that there is nothing to limit the number of results cached. Gradually, 'solution' will fill up with junk, especially if it is used with unpredictable functions, such as Plot3D. If 'solution' took only integers as its argument, the situation would be somewhat better ... but there are infinitely many floating point numbers in a bounded interval. > solution3d = FunctionInterpolation[solution[a][x], {a, 1, 5}, > {x, 0.1, 5}] Why do you use FunctionInterpolation before passing the result to Plot3D? > Plot3D[solution3d[a, x], {a, 1, 5}, {x, 0.1, 5}] > -- Szabolcs