悠若谷 发表于 2009-6-16 19:42

关于绘制隐函数图形

在学习画隐函数图形的时候看到论坛的FAQ集里边有这样的问答
Q:如何在Matlab中画隐函数曲线?

A: 在http://www.mathworks.com/matlabcentral/fileexchange/index.jsp
      查找implicit,会找到一个Arthur Jutan写的implot.m


上面这个网站我打不开,显示
The page you were looking for doesn't exist.You may have mistyped the address or the page may have moved.

不知道哪位有implot.m原代码?能贴上来吗?

friendchj 发表于 2009-6-16 20:49

Description
A basic plotting function for implicit functions of exactly two variables.
Plots in the same way as implot.m (by Arthur Jutan), but extends the capabilities of implot.m to allow specification of one or more contourgroup properties: e.g. line width, line color, line style etc.

function = implot2(varargin)
% IMPLOT2: Plots implicit functions.
%
% = IMPLOT2(F,RANGE,N) plots an implicit function of exactly 2
% variables (e.g 'x' and 'y', but any variables will work).
% * F is a MATLAB inline function defined in form F(x,y)=0
% * RANGE = is the range over which F is plotted
% * N is no. of grid-points, per variable, in the discretisation used
% to calculate the plot ==> total no. grid-points ~ N^2
% * H is a handle to the plotted lineseries object; lineData is a
% two-row matrix containing the plotted x and y data. Each contiguous
% line segment is demarcated by a pair of NaN values.
%
% IMPLOT2(F), IMPLOT2(F,RANGE) plots as above, but using the default
% values {RANGE}=[-2*pi,2*pi,-2*pi,2*pi] and {N}=100 (as applicable)
%
% IMPLOT2(...,S,'PropertyName1',PropertyValue1,...) plots as above, but
% with parameter/value pairs specifying additional line properties, as
% per syntax in PLOT.m. Type DOC LINESERIESPROPERTIES for more info.
% S is an optional 1,2 or 3-element character string specifying line
% color, marker and/or style, also as per PLOT.m syntax.
%
% Example -- plot , , :
%
% f=inline('2*sin(y)-x','x','y'); g=inline('x^2+y^2-4','x','y');
% h=inline('sin(x)*cos(y)-pi/30','x','y');
% rangexy = [-2 2 -2 2]; hold on;
% implot2(f,'LineWidth',2,'Color',);
% implot2(g,rangexy,200,'g-','LineWidth',2);
% = implot2(h,'r:'); axis equal;
%
% --- AUTHOR: Vinesh Rajpaul (UCT)
% --- VERSION: 16 May 2009
%
% See also INLINE, PLOT, CONTOUR, CONTOURC

%% Specify default parameters
ngrid=100; % default: 100*100 gridpoints
rangexy=[-2*pi,2*pi,-2*pi,2*pi]; % default range
lineSpec ='k-'; % default linestyle

%% Check number of input arguments; extract contourgroup properties

for i=1:nargin
if ischar(varargin{i})
propertyIndex = i; % index of first line property
break
end
end

if exist('propertyIndex','var')
properties = varargin(propertyIndex:nargin);
if mod(length(properties),2)~=0
lineSpec = properties{1}; % treat character string S separately
properties(1)=[];
end
nargs = propertyIndex-1;
else nargs = nargin;
end

if nargs < 1 || nargs >3
error('MATLAB:implot:InvalidInput', ...
'1 to 3 plotting arguments required. Type HELP IMPLOT2.')
end

switch nargs
case 1; % only an inline function specified
fun = varargin{1};
case 2; % as for nargin==1, with range also specified
= deal(varargin{1:nargs});
otherwise % as for nargin==2, with gridpoints also specified
=deal(varargin{1:nargs});
end

%% Set up mesh grid; do plotting

xGrid=linspace(rangexy(1),rangexy(2),ngrid);
yGrid=linspace(rangexy(3),rangexy(4),ngrid);
=meshgrid(xGrid,yGrid);

fvector=vectorize(fun); % vectorize the inline function
fvalues=feval(fvector,x,y);
cMatrix = contourc(xGrid,yGrid,fvalues,);

variables = symvar(fun); % extract variables from inline function

lineData = separateLines(cMatrix); % separate contiguous lines
lineHandle = plot(lineData(1,:),lineData(2,:),lineSpec);
xlabel(variables(1)); ylabel(variables(2)); box on;

if exist('properties','var') % apply all specified line properties
nProps = length(properties);
set(lineHandle,properties(1:2:nProps),properties(2:2:nProps));
end

end

%% Demarcate contiguous line segments by NaN tokens
% (Mainly to facilitate plotting of noncontiguous segments via PLOT.m)

function lineData = separateLines(cMatrix)
lineData = cMatrix;
index = 1; % index of line segment demarcation
while index<length(lineData)
numPoints = round(lineData(2,index)); % no. datapoints in line
lineData(:,index)= NaN; % demarcate each contiguous line by NaN
index = index + numPoints +1; % advance to next token
end
end

悠若谷 发表于 2009-6-17 10:51

谢谢楼上的O(∩_∩)O~代码已经用上了!

[ 本帖最后由 悠若谷 于 2009-6-17 11:21 编辑 ]
页: [1]
查看完整版本: 关于绘制隐函数图形