Using surf to represent a function with color.

2 views (last 30 days)
Hi everyone, I would like to represent the magnitude of a function on a surface (hemisphere) using a color map. Here is my current attempt
r=22; %radius of sphere
phi=linspace(0,pi,30);
theta=linspace(0,pi,40);
[phi,theta]=meshgrid(phi,theta); %meshgrid of sphere
x=r*sin(phi).*cos(theta);
y=r*sin(phi).*sin(theta);
z=r*cos(phi);
mesh(x,y,z)
hold on
theta = linspace(0,pi)
R=1/22 % R=Rs/r (constant)
Bo=21000 %constants
Bmag = Bo*((R).^3).*((1+3*(sin(theta)).^2)).^0.5 %magnetic field function
surf(x,y,z,Bmag)
I then get the error message
Warning: Error creating or updating Surface
Error in value of property CData
Array is wrong shape or size
Does anyone have any idea how I could represent this?

Answers (1)

Walter Roberson
Walter Roberson on 22 Dec 2015
Delete the theta = linspace(0,pi) that you are using to redefine theta.
  2 Comments
John Draper
John Draper on 22 Dec 2015
Thanks, that gives me a figure. But the hemisphere is all one color now. Is there any way to plot it such that you can see the variation in Bmag across the surface of the hemisphere?
Walter Roberson
Walter Roberson on 23 Dec 2015
Remove the mesh()
The default for mesh is to color by z, so your color values for that range the same as your z, -22 to +22. Then when you surf() with Bmag as the color values (about 1.97 to twice that), MATLAB still has to use a color interpolation range large enough to include the now-hidden mesh, but only a narrow range of values are present in the visible image so they map to a narrow range of visible colors.
By default surf() draws edges, and so outlines the mesh anyhow. You can turn off those edges with
surf(x, y, z, Bmag, 'edgecolor', 'none')

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!