4

If given a rotation axis normalized, such as {1/Sqrt[3],1/Sqrt[3],1/Sqrt[3]}, and a 3d plot, for example,

z[x_, y_] := Exp[-(Sqrt[x^2 + y^2]/Power[4, (3)^-1]) + Power[4, (3)^-1]*Sqrt[1/2*(Sqrt[x^2 + y^2] + x)]];

Plot3D[2*z[x, y], {x, -5, 5}, {y, -5, 5}]

I want to create an animation for this plot about the axis {1/Sqrt[3],1/Sqrt[3],1/Sqrt[3]} (could be any other arbitary one), and then export it as an animated gif. Would anyone please help? Many thanks.

Edit

I also left out one degree of freedom in specifying the rotation. Could any one please help, if also given the coordinate of a point which the rotational axis must pass, how to do the visualization/animation? Thanks again.

Qiang Li
  • 10,593
  • 21
  • 77
  • 148
  • I beg you to review your decision and accept Daniel's answer instead of mine. I copied the code there and just added the export feature, not a big deal. It's a shame to get 15 points just for copying and pasting. Thanks! – – Dr. belisarius Mar 04 '11 at 22:25
  • @belisarius: i understood, and I would go with what you requested here. :) Initially I thought about this, and I thought yours is a complete solution. It'll be easier for people who come to read this post. But anyhow... – Qiang Li Mar 04 '11 at 22:32

2 Answers2

7

Copying what Daniel did, just prepared for exporting.

axis = {1, 1, 1};
l = {-7, 7};

s = Table[

      Plot3D[2*z[x, y], {x, -5, 5}, {y, -5, 5}, PlotRange -> {l, l, l}] /. 

      gg : GraphicsComplex[___] :> Rotate[gg, theta, axis], {theta, 0., 2. Pi}];

Export["c:\\test.gif", s]

enter image description here

The following parameters are available for the gif export (as per the docs):

"AnimationRepetitions" how many times the animation is played before stopping
"Background"           background color shown in transparent image regions 
"BitDepth"             bits used to represent each color channel in the file
"ColorMap"             color reduction palette, given as a list of color values
"GlobalColorMap"       default color palette for individual animation frames
"DisplayDurations"     display durations of animation frames, given in seconds
"ImageCount"           number of frames in an animated GIF
"ImageSize"            overall image size
"RawData"              array of color map indices
"Comments"             user comments stored in the file

I used "DisplayDurations" in the past, and it worked.

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • thanks a lot. I am just wondering whether there is an automatic way of choosing the PlotRange for this purpose. Sometimes, it is rather difficult to look at the graph to guesstimate the range. – Qiang Li Mar 01 '11 at 19:31
  • @Daniel, @belisarius: it looks like that Animate produces very poor graphics when compared with the output animated gif. Did you notice the same effect? I am wondering why this is the case; maybe there is an improvement for Animate? – Qiang Li Mar 01 '11 at 19:32
  • @Qiang Li: The reason for the gif being nicer is that the data is not computed along the way. Try `ListAnimate[s]` with s as above. There are options for frame-rate, etc. – Janus Mar 02 '11 at 01:55
6

Could do as below.

axis = {1, 1, 1};

Animate[
  Plot3D[2*z[x, y], {x, -5, 5}, {y, -5, 5}] /. 
    gg : GraphicsComplex[___] :> Rotate[gg, theta, axis],
  {theta, 0., 2.*Pi}]

enter image description here

Daniel Lichtblau Wolfram Research

Daniel Lichtblau
  • 6,854
  • 1
  • 23
  • 30
  • @Daniel: thanks a lot. Do you happen to remember how to export the animation? – Qiang Li Feb 28 '11 at 23:32
  • @Qiang To export is easier to make a Table[] with the plots and then Export["filename.gif", tableWithPlots] – Dr. belisarius Feb 28 '11 at 23:52
  • @belisarius: could you please give a complete solution to do the export part? – Qiang Li Mar 01 '11 at 00:00
  • @Daniel: I found during rotation, the origin is actually moving around. How to fix it? – Qiang Li Mar 01 '11 at 00:01
  • @Qiang I copied what Daniel did and completed for exporting. – Dr. belisarius Mar 01 '11 at 00:51
  • @Qiang: The axis is not actually moving, but as the PlotRange changes during the animation, the whole thing wobbles a bit. I've placed some code in a new answer showing how to fix it. – Simon Mar 01 '11 at 04:33
  • @Simon yep. Getting these animations right sometimes is tricky. This one was easy. – Dr. belisarius Mar 01 '11 at 04:40
  • 1
    @Simon The last one here was more difficult: http://stackoverflow.com/questions/4362498/curve-fitting-points-in-3d-space/4362773#4362773 – Dr. belisarius Mar 01 '11 at 04:43
  • @Simon: where is your new answer? :-) – Qiang Li Mar 01 '11 at 04:44
  • @Qiang - I deleted it, since belisarius had already fixed the PlotRange. I thought I had also removed the comment... sorry! – Simon Mar 01 '11 at 06:22
  • 1
    @belisarius, +1 on your old 4362498 answer. Sometimes you put a crazy (read: more than the question deserves or the OP expects) amount of effort into these things! Not that I or they are complaining. – Simon Mar 01 '11 at 06:27
  • @belisarius: That reminds me - I can't believe your (really good) [understanding randomness](http://stackoverflow.com/questions/3956478/understanding-randomness/3956538#3956538) answer is up to 806 votes! That has to be a record... – Simon Mar 01 '11 at 06:32
  • 1
    @Simon Regarding the randomness answer, I guess most 101 courses are too boring and people tend to forget them. Then, a few years later, someone tells them the same using visually appealing examples and they realize the importance of the concept and the dangers of ignoring the basics. Its incredible score was a surprise for me too, mostly because my poor command of the English language drives me usually to write answers that either are very short or depict me as a simpleton. – Dr. belisarius Mar 01 '11 at 11:40
  • @Simon @belisarius Thanks for fixing the wobbles. Probably just as well you didn't add a helicopter to the picture. – Daniel Lichtblau Mar 01 '11 at 16:46
  • @Daniel The code was yours :D. I only add helicopters to questions without the Mma tag, because this community is too propense to scorn publicly on graphics exuberance :P – Dr. belisarius Mar 01 '11 at 16:56
  • @balisarius, @Simon, @Daniel, please also see my edit where I also want to completely specify the rotational axis, here how to do the visualization? thanks a lot again. – Qiang Li Mar 01 '11 at 19:45
  • @Qiang I beg you to review your decision and accept this answer instead of mine. I copied the code here and just added the export feature, not a big deal. It's a shame to get 15 points just for copying and pasting. Thanks! – Dr. belisarius Mar 04 '11 at 22:24
  • @belisarius I'm always happy for a point gain, but I was surviving on what I had. At least, I think I was surviving...maybe best I check my pulse. – Daniel Lichtblau Mar 04 '11 at 22:45
  • @Daniel If you need to analyze your pulse, Mma has wavelets now! – Dr. belisarius Mar 04 '11 at 23:09
  • I'm running both @DanielLichtblau 's answer and Dr. belisarius' answer in Mathematica 11, and it seems to just produce an empty box. Is it working in v11 for anyone else? – billc Aug 26 '16 at 03:28
  • 1
    @billc Did you remember to define `z[x_,y_]`? – Daniel Lichtblau Aug 26 '16 at 14:53