Using surf to represent a function with color.

1 vue (au cours des 30 derniers jours)
John Draper
John Draper le 22 Déc 2015
Commenté : Walter Roberson le 23 Déc 2015
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?

Réponses (1)

Walter Roberson
Walter Roberson le 22 Déc 2015
Delete the theta = linspace(0,pi) that you are using to redefine theta.
  2 commentaires
John Draper
John Draper le 22 Déc 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 le 23 Déc 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')

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by