Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am looking for any ideas about how to colorize an ellipsoid with a custom gradient style or by a Blend function. I would like to colorize ellipsoid as follows:

  • at $(5,0,0)$ and $(-5,0,0)$ should be red color,
  • at $(0,2,0)$ and $(0,-2,0)$ should be green color
  • at $(0,0,2)$ and $(0,0,-2)$ should be blue color

All other points should be interpolated, respectively to their locations.

I tried to use Blend function, but something went wrong.

My code:

ParametricPlot3D[{5 Cos[u] Cos[v], 2 Cos[v] Sin[u], 2 Sin[v]},
{u, 0, 2 Pi, Pi/10}, {v, -Pi/2, Pi/2, Pi/10}, Axes -> False, 
Boxed -> False, Mesh -> None, 
ColorFunction ->Function[u, v, 
Blend[{Red, Green, Blue}, {5 Cos[u] Cos[v], 2 Cos[v] Sin[u], 2 Sin[v]}]]]
share|improve this question
    
Did you possibly mean Function[{u,v},Blend[...]]? –  celtschk Mar 31 '12 at 19:37
    
Thanks! But unfortunately, it renders me something similar to arc in three-dimensional space. –  user897 Mar 31 '12 at 20:03
add comment

1 Answer

up vote 9 down vote accepted

There are a few things wrong with your code. First of all, the ranges for the parameters are in the wrong format. They should be of the form {u, umin, umax} etc. If you want to control the step size you could use PlotPoints and MaxRecursion.

Secondly, as celtschk pointed out, the arguments in a pure function should be combined in a list. If you check the documentation of ColorFunction, you'll see that for ParametricPlot3D, the arguments provided to the ColorFunction are in this case the three spacial coordinates $x, y, z$ and the two parameters $u, v$ in that order. Therefore you could define your ColorFunction for example as

Function[{x, y, z, u, v}, Blend[{Red, Green, Blue}, {(x/5)^2, (y/2)^2, (z/2)^2}]

You would also need to set ColorFunctionScaling -> False. With these settings, the ellipsoid then becomes something like

ParametricPlot3D[{5 Cos[u] Cos[v], 2 Cos[v] Sin[u], 2 Sin[v]}, {u, 0, 
  2 Pi}, {v, -Pi/2, Pi/2}, Axes -> False, Boxed -> False, Mesh -> None,
 PlotPoints -> 40,
 ColorFunctionScaling -> False, 
 ColorFunction -> Function[{x, y, z, u, v}, Blend[{Red, Green, Blue}, 
   {(x/5)^2, (y/2)^2, (z/2)^2}]]]

Mathematica graphics

share|improve this answer
    
Thank you @Heike for clear explanation! I'm not very familiar with Mathematica. I'am Matlab user, but unfortunately my University changed to Mathematica. –  user897 Mar 31 '12 at 20:25
4  
@user897 I would say fortunately... Welcome to the club –  Sjoerd C. de Vries Mar 31 '12 at 21:14
    
@user897 A high quality answer as usual from Heike. I would like to point out that you do not need to include the trailing unused arguments in the Function, so you could use Function[{x, y, z}, . . .] (I presume she included them for didactic clarity). Also, you can use an anonymous or "pure" function, but so as not to run into precedence problems you should wrap the whole thing in ( ) until you understand more: ColorFunction -> (Blend[{Red, Green, Blue}, {(#/5)^2, (#2/2)^2, (#3/2)^2}] &) –  Mr.Wizard Apr 1 '12 at 4:29
1  
@SjoerdC.deVries: If fortunately or unfortunately depends on what you need. Mathematica has abilities which AFAIK are not found in Matlab (esp. symbolic calculation), but if you don't need them and have considerable experience in Matlab, having to learn a new syntax to just do the same things you already did in Matlab is a disadvantage to you. Also I'd be surprised if there was not some area where Matlab is better (well, there definitely exists at least one: executing existing Matlab code!). There's almost always a case where the more general tool is worse than a more specialized one. –  celtschk Apr 1 '12 at 13:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.