一. 将隐式函数化作参数形式,如球面,x2
+y2
+z2
=R2
ezsurf
sphere.m
function sphere(R)
x=[num2str(R),'*sin(s)*cos(t)'];
y=[num2str(R),'*sin(s)*sin(t)'];
z=[num2str(R),'*cos(s)'];
ezsurf(x,y,z,[0,pi,0,2*pi]);
axis square;
shading interp;
colormap(spring);
view(10,40)
在命令行中执行命令sphere(r)就可以得到半径为r的球形了
转化成参数方程后,用mesh或surf画
% x,y,z are equally sized matrices with coordinates.
s=0:0.05:10;
t=-0.05:0.05:2*pi;%取-0.05 使得圆锥体没有缝隙
[s,t]=meshgrid(s,t);
x=(s+10).*cos(t);
y=(s+10).*sin(t);
z=s;
mesh(x,y,z);
saveobjmesh('temp.obj',x,y,z);
二. 利用isosurface, 可将隐式函数直接画出来
1. isosurface与patch连用,可将曲面显示出来
Example: 在同一个图内,画出球面,圆柱面
x=-10:0.1:10;y=x;z=x;
[x,y,z]=meshgrid(x,y,z);
f1=x.^2+y.^2+z.^2-4;
f2=x.^2+y.^2-1;
f3=x.^2+z.^2-1;
f4=z.^2+y.^2-1;
p1=patch(isosurface(x,y,z,f1,0));
set(p1, 'FaceColor', 'b', 'EdgeColor', 'none');
p2=patch(isosurface(x,y,z,f2,0));
set(p2, 'FaceColor', 'r', 'EdgeColor', 'none');
p3=patch(isosurface(x,y,z,f3,0));
set(p3, 'FaceColor', 'y', 'EdgeColor', 'none');
p4=patch(isosurface(x,y,z,f4,0));
set(p4, 'FaceColor', 'h', 'EdgeColor', 'none');
daspect([1 1 1])
view(3); axis tight
camlight;
lighting phong
2. 将isosurface输出.obj文件
首先,先写vertface2obj.m
function vertface2obj(v,f,name)
% VERTFACE2OBJ Save a set of vertice coordinates and faces as a Wavefront/Alias Obj file
% VERTFACE2OBJ(v,f,fname)
% v is a Nx3 matrix of vertex coordinates.
% f is a Mx3 matrix of vertex indices.
% fname is the filename to save the obj file.
fid = fopen(name,'w');
for i=1:size(v,1)
fprintf(fid,'v %f %f %f/n',v(i,1),v(i,2),v(i,3));
end
fprintf(fid,'g foo/n');
for i=1:size(f,1);
fprintf(fid,'f %d %d %d/n',f(i,1),f(i,2),f(i,3));
end
fprintf(fid,'g/n');
fclose(fid);
Example:
a=2*pi; s=2*pi/60; [x,y,z]=meshgrid(-a:s:a,-a:s:a,-a:s:a); cx = cos(2*x); cy = cos(2*y); cz = cos(2*z); u = 10.0*(cos(x).*sin(y) + cos(y).*sin(z) + cos(z).*sin(x))- 0.5*(cx.*cy + cy.*cz + cz.*cx) - 14.0; [f,v]=isosurface(u,0); vertface2obj(v,f,'gyroid.obj')
三. 由ezimplot3,可将隐士函数直接画出曲面
function h = ezimplot3(varargin)
% EZIMPLOT3 Easy to use 3D implicit plotter.
% EZIMPLOT3(FUN) plots the function FUN(X,Y,Z) = 0 (vectorized or not)
% over the default domain:
% -2*PI < X < 2*PI, -2*PI < Y < 2*PI, -2*PI < Z < 2*PI.
% FUN can be a string, an anonymous function handle, a .M-file handle, an
% inline function or a symbolic function (see examples below)
%
% EZIMPLOT3(FUN,DOMAIN)plots FUN over the specified DOMAIN instead of the
% default domain. DOMAIN can be vector [XMIN,XMAX,YMIN,YMAX,ZMIN,ZMAX] or
% vector [A,B] (to plot over A < X < B, A < Y < B, A < Z < B).
%
% EZIMPLOT3(..,N) plots FUN using an N-by-N grid. The default value for
% N is 60.
% EZIMPLOT3(..,'color') plots FUN with color 'color'. The default value
% for 'color' is 'red'. 'color' must be a valid Matlab color identifier.
%
% EZIMPLOT3(axes_handle,..) plots into the axes with handle axes_handle
% instead of into current axes (gca).
%
% H = EZIMPLOT3(...) returns the handle to the patch object this function
% creates.
%
% Example:
% Plot x^3+exp(y)-cosh(z)=4, between -5 and 5 for x,y and z
%
% via a string:
% f = 'x^3+exp(y)-cosh(z)-4'
% ezimplot3(f,[-5 5])
%
% via a anonymous function handle:
% f = @(x,y,z) x^3+exp(y)-cosh(z)-4
% ezimplot3(f,[-5 5])
%
% via a function .m file:
%------------------------------%
% function out = myfun(x,y,z)
% out = x^3+exp(y)-cosh(z)-4;
%------------------------------%
% ezimplot3(@myfun,[-5 5]) or ezimplot('myfun',[-5 5])
%
% via a inline function:
% f = inline('x^3+exp(y)-cosh(z)-4')
% ezimplot3(f,[-5 5])
%
% via a symbolic expression:
% syms x y z
% f = x^3+exp(y)-cosh(z)-4
% ezimplot3(f,[-5 5])
%
% Note: this function do not use the "ezgraph3" standard, like ezsurf,
% ezmesh, etc, does. Because of this, ezimplot3 only tries to imitate that
% interface. A future work must be to modify "ezgraph3" to include a
% routine for implicit surfaces based on this file
%
% Inspired by works of: Artur Jutan UWO 02-02-98 ajutan@julian.uwo.ca
% Made by: Gustavo Morales UC 04-12-09 gmorales@uc.edu.ve
%
%%% Checking & Parsing input arguments:
if ishandle(varargin{1})
cax = varargin{1}; % User selected axes handle for graphics
axes(cax);
args{:} = varargin{2:end}; %ensuring args be a cell array
else
args = varargin;
end
[fun domain n color] = argcheck(args{:});
%%% Generating the volumetric domain data:
xm = linspace(domain(1),domain(2),n);
ym = linspace(domain(3),domain(4),n);
zm = linspace(domain(5),domain(6),n);
[x,y,z] = meshgrid(xm,ym,zm);
%%% Formatting "fun"
[f_handle f_text] = fix_fun(fun); % f_handle is the anonymous f-handle for "fun"
% f_text is "fun" ready to be a title
%%% Evaluating "f_handle" in domain:
try
fvalues = f_handle(x,y,z); % fvalues: volume data
catch ME
error('Ezimplot3:Functions', 'FUN must have no more than 3 arguments');
end
%%% Making the 3D graph of the 0-level surface of the 4D function "fun":
h = patch(isosurface(x,y,z,fvalues,0)); % "patch" handles the structure...
% sent by "isosurface"
isonormals(x,y,z,fvalues,h)% Recalculating the isosurface normals based...
% on the volume data
set(h,'FaceColor',color,'EdgeColor','none');
%%% Aditional graphic details:
xlabel('x');ylabel('y');zlabel('z');% naming the axis
alpha(0.7) % adjusting for some transparency
grid on; view([1,1,1]); axis equal; camlight; lighting gouraud
%%% Showing title:
title([f_text,' = 0']);
%
%--------------------------------------------Sub-functions HERE---
function [f dom n color] = argcheck(varargin)
%ARGCHECK(arg) parses "args" to the variables "f"(function),"dom"(domain)
%,"n"(grid size) and "c"(color)and TRIES to check its validity
switch nargin
case 0
error('Ezimplot3:Arguments',...
'At least "fun" argument must be given');
case 1
f = varargin{1};
dom = [-2*pi, 2*pi]; % default domain: -2*pi < xi < 2*pi
n = 60; % default grid size
color = 'red'; % default graph color
case 2
f = varargin{1};
if isa(varargin{2},'double') && length(varargin{2})>1
dom = varargin{2};
n = 60;
color = 'red';
elseif isa(varargin{2},'double') && length(varargin{2})==1
n = varargin{2};
dom = [-2*pi, 2*pi];
color = 'red';
elseif isa(varargin{2},'char')
dom = [-2*pi, 2*pi];
n = 60;
color = varargin{2};
end
case 3 % If more than 2 arguments are given, it's
f = varargin{1}; % assumed they are in the correct order
dom = varargin{2};
n = varargin{3};
color = 'red'; % default color
case 4 % If more than 2 arguments are given, it's
f = varargin{1}; % assumed they are in the correct order
dom = varargin{2};
n = varargin{3};
color = varargin{4};
otherwise
warning('Ezimplot3:Arguments', ...
'Attempt will be made only with the 4 first arguments');
f = varargin{1};
dom = varargin{2};
n = varargin{3};
color = varargin{4};
end
if length(dom) == 2
dom = repmat(dom,1,3); %domain repeated in all variables
elseif length(dom) ~= 6
error('Ezimplot3:Arguments',...
'Input argument "domain" must be a row vector of size 2 or size 6');
end
%
%--------------------------------------------
function [f_hand f_text] = fix_fun(fun)
% FIX_FUN(fun) Converts "fun" into an anonymous function of 3 variables (x,y,z)
% with handle "f_hand" and a string "f_text" to use it as title
types = {'char','sym','function_handle','inline'}; % cell array of 'types'
type = ''; %Identifing FUN object class
for i=1:size(types,2)
if isa(fun,types{i})
type = types{i};
break;
end
end
switch type
case 'char' % Formatting FUN if it is char type. There's 2 possibilities:
% A string with the name of the .m file
if exist([fun,'.m'],'file')
syms x y z;
if nargin(str2func(fun)) == 3
f_sym = eval([fun,'(x,y,z)']); % evaluating FUN at the sym point (x,y,z)
else
error('Ezimplot3:Arguments',...
'%s must be a function of 3 arguments or unknown function',fun);
end
f_text = strrep(char(f_sym),' ',''); % converting to char and eliminating spaces
f_hand = eval(['@(x,y,z)',vectorize(f_text),';']); % converting string to anonymous f_handle
else
% A string with the function's expression
f_hand = eval(['@(x,y,z)',vectorize(fun),';']); % converting string to anonymous f_handle
f_text = strrep(fun,'.',''); f_text = strrep(f_text,' ',''); % removing vectorization & spaces
end
case 'sym' % Formatting FUN if it is a symbolic object
f_hand = eval(['@(x,y,z)',vectorize(fun),';']); % converting string to anonymous f_handle
f_text = strrep(char(fun),' ',''); % removing spaces
case {'function_handle', 'inline'} % Formatting FUN if it is a function_handle or an inline object
syms x y z;
if nargin(fun) == 3 && numel(symvar(char(fun))) == 3 % Determining if # variables == 3
f_sym = fun(x,y,z); % evaluating FUN at the sym point (x,y,z)
else
error('Ezimplot3:Arguments',...
'%s must be function of 3 arguments or unknown function',char(fun));
end
f_text = strrep(char(f_sym),' ',''); % converting into string to removing spaces
f_hand = eval(['@(x,y,z)',vectorize(f_text),';']); % converting string to anonymous f_handle
otherwise
error('First argument "fun" must be of type character, simbolic, function handle or inline');
end
Example:
Plot x^3+exp(y)-cosh(z)=4, between -5 and 5 for x,y and z
% via a string:
f = 'x^3+exp(y)-cosh(z)-4'
ezimplot3(f,[-5 5])
四. 由implot,可将直接画出隐式曲线
implot.m
function implot(fun,rangexy,ngrid)
% Implicit plot function
% function implot(fun,rangexy,ngrid)
% fun is 'inline' function f(x,y)=0 (Note function written as equal to zero)
% rangexy =[xmin,xmax,ymin,ymax] range over which x and y is ploted default(-2*pi,2*pi)
% ngrid is the number of grid points used to calculate the plot,
% Start with course grid (ngrid =20) and then use finer grid if necessary
% default ngrid=50
%
% Example
% Plot y^3+exp(y)-tanh(x)=0
%
% write function f as an 'inline' function of x and y-- right hand side
% equal to zero
%
% f=inline('y^3+exp(y)-tanh(x)','x','y')
% implot(f,[-3 3 -2 1])
% A.Jutan UWO 2-2-98 ajutan@julian.uwo.ca
if nargin == 1 ;% grid value and ranges not specified calculate default
rangexy=[-2*pi,2*pi,-2*pi,2*pi];
ngrid=50;
end
if nargin == 2; % grid value not specified
ngrid=50;
end
% get 2-D grid for x and y
xm=linspace(rangexy(1),rangexy(2),ngrid);
ym=linspace(rangexy(3),rangexy(4),ngrid);
[x,y]=meshgrid(xm,ym);
fvector=vectorize(fun);% vectorize the inline function to handle vectors of x y
fvalues=feval(fvector,x,y); %calculate with feval-this works if fvector is an m file too
%fvalues=fvector(x,y); % can also calculate directly from the vectorized inline function
contour(x,y,fvalues,[0,0],'b-');% plot single contour at f(x,y)=0, blue lines
xlabel('x');ylabel('y');
grid
Example:
Example
% Plot y^3+exp(y)-tanh(x)=0
%
% write function f as an 'inline' function of x and y-- right hand side
% equal to zero
%
% f=inline('y^3+exp(y)-tanh(x)','x','y')
% implot(f,[-3 3 -2 1])
五. 由复平面画复杂曲面
%画圆锥曲面
%利用复平面
z=cplxgrid(20);%在复平面上面得到一个21*41((m+1)*(2m+1))的网格,x,y分别为
其实部和虚部
x=real(z);
y=imag(z);
fz=sqrt((x.^2+y.^2));%圆锥公式,z^2=x^2+y^2来求fz
cplxmap(z,fz)%作图