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

Combining 2D graphs into a 3D graph

56 views
Skip to first unread message

Carol Ting

unread,
Jul 17, 2004, 7:13:58 AM7/17/04
to
Hi List,

I have many curves in the 2D space, each associated with a specific
value of parameter n. (These curves are determined with a somewhat
complicated process depending on the value of n, so I cannot just use
one bivariate function to describe this family of curves.)

Can anyone tell me how to plot these curves in a 3D plot, while adding
n as the second axis?

For example, how can I make Mathematica plot the following lines in a
3D plot?

n=1, l1 = Line[{{2, 7}, {3, 9}}]
n=2, l2 = Line[{{2, 6}, {3, 8}}]

Thank you very much!

Carol Ting

BobH...@aol.com

unread,
Jul 18, 2004, 8:12:09 AM7/18/04
to
Needs["Graphics`"];


lines={Line[{{2,7},{3,9}}],Line[{{2,6},{3,8}}]};


Module[{m=Length[lines],k=1},
Show[Graphics[
{Hue[((k++)+m-2)/(2m-2)],#}&/@lines],
Axes->True]];


Module[{m=Length[lines],k=1},
Show[StackGraphics[Graphics[
{Hue[((k++)+m-2)/(2m-2)],#}]&/@lines],
Axes->True, ViewPoint->{1.7, -2.8, 0.85}]];


Module[{m=Length[lines],k=0},
Show[Graphics3D[
{Hue[((++k)+m-2)/(2m-2)],
#/.{x_?NumericQ, y_?NumericQ}:>{x,k,y}}&/@lines],
Axes->True, AspectRatio->1,ImageSize->288]];

Bob Hanlon

> In a message dated Sat, 17 Jul 2004 11:13:58 +0000 (UTC),
> suom...@yahoo.com writes: I have many curves in the 2D space, each associated with

David Park

unread,
Jul 18, 2004, 8:16:11 AM7/18/04
to
Carol,

Try out the StackGraphics command in the Graphics`Graphics` standard
package.

David Park
dj...@earthlink.net
http://home.earthlink.net/~djmp/

From: Carol Ting [mailto:suom...@yahoo.com]


Hi List,

I have many curves in the 2D space, each associated with a specific
value of parameter n. (These curves are determined with a somewhat
complicated process depending on the value of n, so I cannot just use
one bivariate function to describe this family of curves.)

Can anyone tell me how to plot these curves in a 3D plot, while adding
n as the second axis?

For example, how can I make Mathematica plot the following lines in a
3D plot?

n=1, l1 = Line[{{2, 7}, {3, 9}}]
n=2, l2 = Line[{{2, 6}, {3, 8}}]

Thank you very much!

Carol Ting

Bill Rowe

unread,
Jul 18, 2004, 8:17:12 AM7/18/04
to
On 7/17/04 at 6:40 AM, suom...@yahoo.com (Carol Ting) wrote:

>I have many curves in the 2D space, each associated with a specific
>value of parameter n. (These curves are determined with a somewhat
>complicated process depending on the value of n, so I cannot just
>use one bivariate function to describe this family of curves.)

>Can anyone tell me how to plot these curves in a 3D plot, while
>adding n as the second axis?

>For example, how can I make Mathematica plot the following lines in
>a 3D plot?

>n=1, l1 = Line[{{2, 7}, {3, 9}}]
>n=2, l2 = Line[{{2, 6}, {3, 8}}]

Try the following:

s = {{2, 7, 1}, {3, 9, 1}};
m = {{2, 6, 2}, {3, 8, 2}};

<< "Graphics`Graphics3D`"

a = ScatterPlot3D[s, PlotJoined -> True, PlotStyle -> Hue[0],
DisplayFunction -> Identity];
b = ScatterPlot3D[m, PlotJoined -> True, PlotStyle -> Hue[0.6],
DisplayFunction -> Identity];

Show[a, b, DisplayFunction -> $DisplayFunction];
--
To reply via email subtract one hundred and four

Carol Ting

unread,
Jul 20, 2004, 8:13:02 AM7/20/04
to
Hello,

I've tried all the suggestions from you and others, and they all work.
Thank you guys very much!

But I have two more related questions:

1. When I run all the three modules together, I get an error message
for the second one, which reads "Show::gtype: StackGraphics is not a
type of graphics. " And if I run them separately things work out
beautifully. Is there an explanation for this? I don't get it.

2. Is there a way for me to fit a surface to the family of curves I
have? Being able to stack the curves is good enough, but I guess my
boss will have this further suggestion.

Again, thank you so much!

Carol

BobH...@aol.com wrote in message news:<cddpep$pcp$1...@smc.vnet.net>...

Daniel Herring

unread,
Jul 31, 2004, 3:27:50 AM7/31/04
to
> 2. Is there a way for me to fit a surface to the family of curves I
> have? Being able to stack the curves is good enough, but I guess my
> boss will have this further suggestion.

I spent *way* too much time on this, but it was interesting learning.
Here's an example which may be of some help.

(* Generate some interesting test functions *)
f[1][x_] := Sin[x];
f[2][x_] := Sin[2 x];
f[3][x_] := Cos[x];
f[4][x_] := Cos[x + Pi/3];
f[5][x_] := Cos[x + Pi/2];

(* Specify the problem domain *)
functions = 5;
xmin = -Pi;
xmax = Pi;

(* Sample and interpolate the information *)
data = Table[f[n][x], {n, functions}, {x, xmin, xmax}];
func = ListInterpolation[data, {{1, functions}, {xmin, xmax}}];

(* Make a pretty plot *)
Plot3D[func[n, x], {n, 1, functions}, {x, xmin, xmax},
AxesLabel -> {"n", "x", "f[x]"},
Mesh -> False, PlotPoints -> 50];


Hope that helps,
Daniel

Peltio

unread,
Aug 1, 2004, 4:16:06 AM8/1/04
to
I hang my post here since I no longer have the OP's post.
The OP wrote:

>> 2. Is there a way for me to fit a surface to the family of curves I
>> have? Being able to stack the curves is good enough, but I guess my
>> boss will have this further suggestion.

Looks like a transfinite interpolation problem.
I once wrote some code to perform ordinary cartesian transfinite
interpolation:

[1] Interpolating two functions (f1[x], f2[x])along the same direction
( at y=a and y=) is straightforward:

F[x_, y_] = (y - b)/(a - b) f1[x] + (y - a)/(b - a) f2[x]

So let's move on.

[2] The generalization to several functions could use the Lagrange
functions to create a connection in the orthogonal direction (an alternative
could be a piecewise transfinite interpolation, but I did not feel like to
venture that far, at the time)

L[x_Symbol, k_Integer] := (
(Times @@ (x - Drop[#, {k}]))/(Times @@ (#[[k]] - Drop[#, {k}]))
)&

TransfiniteInterpolation[Fxj_List, {x_Symbol, xj__}] :=
Fxj.Table[L[x, j][{xj}], {j, 1, Length[{xj}]}]

The example given in the post I am answering tis using fourth degree
polynomials, since there are five 'function lines' to interpolate:

dd[x_, y_] = TransfiniteInterpolation[{Sin[x], Sin[2 x], Cos[x], Cos[x +
Pi/3], Cos[x + Pi/2]}, {y, 1, 2, 3, 4, 5}]
Plot3D[dd[x, y], {x, -Pi, Pi}, {y, 1, 5}, PlotPoints -> 50, Mesh -> False]

[3] To interpolate several functions along orthogonal directions the
following code could be used:

TransfiniteInterpolation[
{Fxj_List, Fyk_List}, {x_Symbol, xj__}, {y_Symbol, yk__}] :=
Module[
{Lxj, Lyk, xvals, yvals},
xvals = {xj}; yvals = {yk};
Lxj = Table[L[x, j][xvals], {j, 1, Length[xvals]}];
Lyk = Table[L[y, k][yvals], {k, 1, Length[yvals]}];
Lxj.Fxj + Lyk.Fyk - Plus @@ Flatten[
Outer[Times, Lxj, Lyk] Outer[ReplaceAll, Fxj,
Rule[y, #] & /@ yvals]]
]

An example with a 3 x 3 grid of functions is:

F[x_, y_] = TransfiniteInterpolation[
{{Sin[y], 1 - Sin[y], Sin[y]}, {Sin[x], 1 - Sin[x], Sin[x]}},
{x, 0, Pi/2, Pi}, {y, 0, Pi/2, Pi}
] // FullSimplify
Plot3D[F[x, y], {x, 0, Pi}, {y, 0, Pi}]

None of the procedures performs a check of the consinstency of the data
passed to it. The values of the functions at the intersections points should
be consistent.

[4] To interpolate only two functions delimiting a rectangular domain I
had an 'ad hoc' code, later superseded by the general form given above. I
kept the procedure to plot the four functions at the boundaries of the
rectangular domain:

ShowTransfiniteInterpolation[{{fw_, fe_}, {fs_, fn_}},
{x_Symbol, x1_, x2_},
{y_Symbol, y1_, y2_}] := (
Block[
{$DisplayFunction = Identity, style = {Thickness[.01], Hue[.84]}, F},
F = TransfiniteInterpolation[{{fw, fe}, {fs, fn}},
{x, x1, x2}, {y,y1, y2}];
surf = Plot3D[F, {x, x1, x2}, {y, y1, y2},
Mesh -> False, PlotPoints -> 35];
Print[F];
lw = ParametricPlot3D[{x1, y, fw, style}, {y, y1, y2}];
le = ParametricPlot3D[{x2, y, fe, style}, {y, y1, y2}];
ls = ParametricPlot3D[{x, y1, fs, style}, {x, x1, x2}];
ln = ParametricPlot3D[{x, y2, fn, style}, {x, x1, x2}];
];
Show[{surf, le, lw, ls, ln},
DisplayFunction -> $DisplayFunction];
)

Try this

ShowTransfiniteInterpolation[
{{Sin[y], Cos[y]}, {Sin[x/2], Sin[3/2x]}},
{x, 0, Pi}, {y, 0, Pi}
]

Hope the code is not too rusty. : )

cheers,
Peltio
Invalid address in reply-to. Crafty demunging required to mail me.

0 new messages