Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NDSolve with functions of vectors

156 views
Skip to first unread message

John Lee

unread,
Nov 1, 2007, 6:34:49 AM11/1/07
to
Hi,
I'm trying to integrate a function with NDSolve (ExplicitRungeKutta method), and it doesn't seem to recognize vector variables (I keep getting an incorrect dimension error). In addition, I would like to integrate over an absolute distance and I don't know how to do that in Mathematica. For simplicity's sake, I will post a random example of what I'm talking about:
U[x] = {2*x[[2]],x[[1]]/3,x[[3]]} is the derivative
X0 = {1,1,1} is the initial condition
so if a'[s] = U[x[s]], I'd like to solve for a[s] where s is the total distance. And s is to be integrated from 0 to 6 with a[0]=X0.
Ultimately, I have a derivative that depends on a 3-component position vector. I want to integrate that derivative over a total distance traveled from a starting point.

Any advice would be helpful. Thanks,
John

Jens-Peer Kuska

unread,
Nov 2, 2007, 4:25:25 AM11/2/07
to
Hi,
U[x_List] := {2*x[[2]], x[[1]]/3, x[[3]]}

X0 = {1, 1, 1};

and

sol = NDSolve[{a'[s] == U[a[s]], a[0] == X0}, a[s], {s, 0, 1}]

will do it. If yoy wish to use the path length you have to transform
a[s]== a[Sqrt[x[s]^2+y[s]^2+z[s]^2]].

Regards
Jens

John Lee

unread,
Nov 2, 2007, 4:31:31 AM11/2/07
to
I'd like to clarify or make this question simpler:

M0=NDSolve[{a'[s]==U[a[s]],a[0]==X},a,{s,0,6},Method->"ExplicitRungeKutta"]

Where X = {1,0,0} and U = {f(x,y,z),g(x,y,z),h(x,y,z)}

That does not work and I can't figure out why. The error I get is "Part::partw: Part 2 of a[s] does not exist." Same with "Part 3".

-John

Steve Luttrell

unread,
Nov 2, 2007, 4:36:36 AM11/2/07
to
This does what you want:

solution =
NDSolve[{x'[s] == {2*x[s][[2]], x[s][[1]]/3, x[s][[3]]},
x[0] == {1, 1, 1}}, x[s], {s, 0, 1}]

gives

{{x[s]->InterpolatingFunction[{{0.,1.}},<>][s]}}

Then

ParametricPlot3D[Expand[x[s]/.solution[[1]]],{s,0,1}]

gives a 3D plot of the trajectory for 0<=s<=1.

--
Steve Luttrell
West Malvern, UK

"John Lee" <johnl...@hotmail.com> wrote in message
news:fgca49$9ho$1...@smc.vnet.net...

Jerry

unread,
Nov 3, 2007, 4:21:43 AM11/3/07
to
Hi, I tried your code and got these errors. What did I miss?
Thanks.

Part::partw: Part 2 of x[s] does not exist. >>

Part::partw: Part 3 of x[s] does not exist. >>

DrMajorBob

unread,
Nov 3, 2007, 4:26:48 AM11/3/07
to
If I interpret your question properly...

Clear[x, y, z, a, u, arc, s, t]
a[t_] = {x[t], y[t], z[t]};
u[{x_, y_, z_}] = {2 y, x/3, z};
ds[{x_, y_, z_}] = Sqrt[#.#] &@u[{x, y, z}];
eqns = Thread[
Flatten@{a'[t] - u[a[t]], a[0] - {1, 1, 1}, arc'[t] - ds[a[t]],
arc[0]} == 0];
{x, y, z, arc} = {x, y, z, arc} /.
Last@NDSolve[eqns, {x, y, z, arc}, {t, 0, 6}];

The "arc" function is s in terms of t. To get x, y, and z in terms of s,
we invert the "arc" function:

Clear[si]
si = si /. First@
NDSolve[{si[arc[0]] == 0, si'[u] == 1/arc'[si@u]},
si, {u, arc@0, arc@6}];

Plot[si@arc[t], {t, 0, 6}]

Plot[arc@si[t], {t, arc@0, arc@6}]

Finally, here's a plot of x, y, and z in terms of arc length:

Plot[a[si[s]], {s, arc@0, arc@6}]

or

ParametricPlot3D[a[s], {s, arc@0, arc@6}]

Bobby

On Fri, 02 Nov 2007 03:28:04 -0500, John Lee <johnl...@hotmail.com>
wrote:

--

DrMaj...@bigfoot.com

Steve Luttrell

unread,
Nov 6, 2007, 3:47:18 AM11/6/07
to
Now I have had a look at my Messages window I see that I am getting the same
warning messages as you. But the final result appears to be the correct
solution to the differential equation.

I have $Version = "6.0 for Microsoft Windows (32-bit) (April 20, 2007)".

--
Steve Luttrell
West Malvern, UK

"Jerry" <JLK...@yahoo.com> wrote in message
news:fghb2n$i9j$1...@smc.vnet.net...

John Lee

unread,
Nov 8, 2007, 6:10:24 AM11/8/07
to
Thank you so much! This worked. For the longest time, I did not notice U[x_List]. That is what fixed my problem. Thank you everyone else for helping me.

0 new messages