Re: Immediate or Delayed Definitions in NDSolve?
- To: mathgroup at smc.vnet.net
- Subject: [mg45078] Re: Immediate or Delayed Definitions in NDSolve?
- From: "Peltio" <peltio at twilight.zone>
- Date: Tue, 16 Dec 2003 06:21:03 -0500 (EST)
- References: <brchoh$2nt$1@smc.vnet.net>
- Reply-to: "Peltio" <peltioNOSP at Miname.com.invalid>
- Sender: owner-wri-mathgroup at wolfram.com
"extrabyte" wrote
>Method 1 (Delayed Definition)
>eq[a_] := y''[x] + a^2*y[x] == 0;
>sol[a_] := NDSolve[{eq[a], y[0] == 1, y'[0] == -1}, y, {x, 0, 10}]
>sol[a]
> NDSolve::"ndnum": "Encountered non-numerical value
As we have already seen, you can use either an immediate or a delayed
assignment as long as you provide a numerical value to all of the parameters
included in your ODE.
The immediate assignment is fast, but requires that you input the numerical
values directly
ode[a_] := y''[x] + a^2*y[x] == 0;
f[x_] = y[x] /. NDSolve[
{ode[1], y[0] == 1,y'[0] == -1}, y[x], {x, 0, 10}][[1]]
data = Table[{x, f[x]} , {x, 0, 1, 10^-3}]
-table here -
Since you want a procedure that accepts the parameter a and the initial
values as arguments you can use a (rather inefficient) delayed assignment
ode[a_] := y''[x] + a^2*y[x] == 0;
f[a_, {y0_,yp0_}, x_] := y[x] /. NDSolve[
{ode[1], y[0] == y0,y'[0] == yp0}, y[x], {x, 0, 10}][[1]]
data[a_, {y0_,yp0_}] := Table[{x, f[a, {y0,yp0}, x]} /. x -> t, {t, 0,
1, 10^-3}]
data[1,{1,-1}]
-table here -
To overcame the efficiency problems we can use a mixed assignment, by
decoupling the parameters a, y0 e yp0 from the variable x. A pure function
and the notation f[params][vars] come in handy:
ode[a_] := y''[x] + a^2*y[x] == 0;
f[a_, {y0_,yp0_}] := y /. NDSolve[
{ode[1], y[0] == y0,y'[0] == yp0}, y, {x, 0, 10}][[1]]
data[a_, {y0_,yp0_}] :=
(
g = f[a, {y0,yp0}];
Table[{x, g} , {x, 0, 1, 10^-3}]
)
data[1,{1,-1}]
-table here -
So, my question to the group is: is there a neater way to do this?
cheers,
Peltio
--
invalid address in reply-to. Crafty deminging required to mail me.