function hh=lforce3d(n,clr)
% Lforce3D
d2r = pi/180; r2d=1/d2r;
tht=d2r*(0:5:360)';
phi=d2r*(0:ceil(180/n):180);
hh=phi*r2d;
A=50;
r=A*sin(tht).^2;
rho=r.*sin(tht);
x=rho*cos(phi);
y=rho*sin(phi);
[nR,nC]=size(x);
u=ones(1,nC);
z=r.*cos(tht)*u;
% Draw Line of Force
h = figure;
plot3(x,y,z,'r'); %,'LineWidth',1.0);
hold on;
axis equal
% Draw The Earth's Spheroid
[u,v,w]=sphere(25);
surf(10*u,10*v,9*w);
colormap('default');
camlight right;
lighting phong;
% shading interp
% Add 3D arrow heads
hold on;
for iC=1:nC
p1=[x(1:4:ceil(nR/2)-1,iC) y(1:4:ceil(nR/2)-1,iC) -z(1:4:ceil(nR/2)-1,iC)];
p2=[x(2:4:ceil(nR/2),iC) y(2:4:ceil(nR/2),iC) -z(2:4:ceil(nR/2),iC)];
% hn=arrow3(p1,p2,s,w,h,ip,alpha,beta)
arrow3(p1,p2,clr,1.5,3);
end
% return
for iC=1:nC
p1=[x(ceil(nR/2):4:nR-1,iC) y(ceil(nR/2):4:nR-1,iC) z(ceil(nR/2):4:nR-1,iC)];
p2=[x(ceil(nR/2)+1:4:nR,iC) y(ceil(nR/2)+1:4:nR,iC) z(ceil(nR/2)+1:4:nR,iC)];
arrow3(p1,p2,clr,1.5,3);
end
title('Magnetic Field of A Dipole','FontSize',15);
hold off;
grid
view(3);
% cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
function hn=arrow3(p1,p2,s,w,h,ip,alpha,beta)
% ARROW3
% ARROW3(P1,P2) will draw vector lines (2D/3D) from P1 to P2 with
% arrowheads, where P1 and P2 can be either nx2 matrices (for 2D),
% or nx3 matrices (for 3D).
%
% ARROW3(P1,P2,S,W,H,IP,ALPHA,BETA) can be used to specify properties
% of the line and arrowhead. S is a character string made with one
% element from any or all of the following 3 columns:
%
% Color Switches LineStyle LineWidth
% ------------------ ------------------- --------------------
% k black (default) - solid (default) 0.5 points (default)
% y yellow : dotted 0 no lines
% m magenta -. dashdot / LineWidthOrder
% c cyan -- dashed
% r red * LineStyleOrder
% g green
% b blue
% w white __________ __
% a apple green ^ |
% d dark gray / \ |
% e evergreen / \ |
% f fuchsia / \ |
% h honey / \ Height
% i indigo / \ |
% j jade / \ |
% l lilac / \ |
% n nutbrown /______ ______\ __|__
% p pink | | | |
% q kumquat |---- Width ----|
% s sky blue | | | |
% t tan | |
% u umber | |
% v violet | |
% z zinc -->| |<--LineWidth
% x random named color | |
% o ColorOrder
% | magnitude
%
% The components of S may be specified in any order. Invalid
% characters in S will be ignored and replaced by default settings.
%
% Prefixing the color code with '_' produces a darker shade, e.g.
% '_t' is dark tan; prefixing the color code with '^' produces a
% lighter shade, e.g. '^q' is light kumquat. The relative
% brightness of light and dark color shades is controled by the
% scalar parameter BETA. Color code prefixes do not affect the
% basic colors (kymcrgbw) or the special color switches (xo|).
%
% ColorOrder may be achieved in two fashions: The user may either
% set the ColorOrder property (using RGB triples) or define the
% global variable ColorOrder (using a string of valid color codes).
% If the color switch is specified with 'o', and the global variable
% ColorOrder is a string of color codes (color switches less 'xo|',
% optionally prefixed with '_' or '^'), then the ColorOrder property
% will be set to the sequence of colors indicated by the ColorOrder
% variable. If the color switch is specified with 'o', and the
% global variable ColorOrder is empty or invalid, then the current
% ColorOrder property will be used. Note that the ColorOrder
% variable takes precedence over the ColorOrder property.
%
% The magnitude color switch is used to visualize vector magnitudes
% in conjunction with a colorbar. If the color switch is specified
% with '|', colors are linearly interpolated from the current ColorMap
% according to the length of the associated line. This option sets
% CLim to [MinM,MaxM], where MinM and MaxM are the minimum and maximum
% magnitudes, respectively.
%
% The current LineStyleOrder property will be used if LineStyle is
% specified with '*'. MATLAB cycles through the line styles defined
% by the LineStyleOrder property only after using all colors defined
% by the ColorOrder property. If however, the global variable
% LineWidthOrder is defined, and LineWidth is specified with '/',
% then each line will be drawn with sequential color, linestyle, and
% linewidth.
%
% W is a vector of arrowhead widths. For linear plots with equal
% axes, the units of W (default = 1/72 of the PlotBox diagonal) are
% the same as those of the coordinate data (P1,P2). For linear plots
% with unequal axes, the units of W are scaled to fit as if the axes
% were equal. For log plots, the units of W (default = 1) are 1/72
% of the PositionRectangle diagonal.
%
% H (default = 3W) is a vector of arrowhead heights. If vector IP is
% neither empty nor negative, initial point markers will be plotted
% with diameter IP; for default diameter W, use IP = 0. The units of
% W, H, and IP are absolute for linear plots and relative to the
% PositionRectangle for log plots.
%
% ALPHA (default = 1) is a vector of FaceAlpha (MATLAB 6+) values
% ranging between 0 (clear) and 1 (opaque). FaceAlpha is a surface
% (arrowhead and initial point marker) property and does not affect
% lines. FaceAlpha is not supported for 2D rendering.
%
% BETA (default = 0.4) is a scalar that controls the relative
% brightness of light and dark color shades, ranging between 0 (no
% contrast) and 1 (maximum contrast).
%
% Plotting lines with a single color, linestyle, and linewidth is
% faster than plotting lines with multiple colors and/or linestyles.
% Plotting lines with multiple linewidths is slower still. ARROW3
% chooses renderers that produce the best screen images; exported
% or printed plots may benifit from different choices.
%
% HN = ARROW3(P1,P2,...) returns a vector of handles to line and
% surface objects created by ARROW3.
%
% ARROW3 COLORS will plot a table of named colors with default
% brightness. ARROW3('colors',BETA) will plot a table of named
% colors with brightness BETA.
%
% If a particular aspect ratio is required, use DASPECT, PBASPECT,
% AXIS, or XYZLIM commands before calling ARROW3. Changing limits
% or aspect ratios after calling ARROW3 may alter the appearance
% of arrowheads and initial point markers. ARROW3 sets XYZCLimMode
% to manual for all plots, sets DataAspectRatioMode to manual for
% linear plots, and sets PlotBoxAspectRatioMode to manual for log
% plots and 3D plots.
%
% ARROW3 UPDATE will restore the the appearance of arrowheads and
% initial point markers that have become corrupted by changes to
% limits or aspect ratios. ARROW3('update',SF) will redraw initial
% point markers and arrowheads with scale factor SF. If SF has one
% element, SF scales W, H and IP. If SF has two elements, SF(1)
% scales W and IP, and SF(2) scales H. If SF ha
评论5