声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1002|回复: 2

[综合讨论] 关于绘制隐函数图形

[复制链接]
发表于 2009-6-16 19:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
在学习画隐函数图形的时候看到论坛的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原代码?能贴上来吗?

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 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.

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

  36. %% Specify default parameters
  37. ngrid=100; % default: 100*100 gridpoints
  38. rangexy=[-2*pi,2*pi,-2*pi,2*pi]; % default [x,y] range
  39. lineSpec ='k-'; % default linestyle

  40. %% Check number of input arguments; extract contourgroup properties

  41. for i=1:nargin
  42. if ischar(varargin{i})
  43. propertyIndex = i; % index of first line property
  44. break
  45. end
  46. end

  47. if exist('propertyIndex','var')
  48. properties = varargin(propertyIndex:nargin);
  49. if mod(length(properties),2)~=0
  50. lineSpec = properties{1}; % treat character string S separately
  51. properties(1)=[];
  52. end
  53. nargs = propertyIndex-1;
  54. else nargs = nargin;
  55. end

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

  60. switch nargs
  61. case 1; % only an inline function specified
  62. fun = varargin{1};
  63. case 2; % as for nargin==1, with range also specified
  64. [fun,rangexy] = deal(varargin{1:nargs});
  65. otherwise % as for nargin==2, with gridpoints also specified
  66. [fun,rangexy,ngrid]=deal(varargin{1:nargs});
  67. end

  68. %% Set up mesh grid; do plotting

  69. xGrid=linspace(rangexy(1),rangexy(2),ngrid);
  70. yGrid=linspace(rangexy(3),rangexy(4),ngrid);
  71. [x,y]=meshgrid(xGrid,yGrid);

  72. fvector=vectorize(fun); % vectorize the inline function
  73. fvalues=feval(fvector,x,y);
  74. cMatrix = contourc(xGrid,yGrid,fvalues,[0,0]);

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

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

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

  83. end

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

  86. function lineData = separateLines(cMatrix)
  87. lineData = cMatrix;
  88. index = 1; % index of line segment demarcation
  89. while index<length(lineData)
  90. numPoints = round(lineData(2,index)); % no. datapoints in line
  91. lineData(:,index)= NaN; % demarcate each contiguous line by NaN
  92. index = index + numPoints +1; % advance to next token
  93. end
  94. end
复制代码

评分

1

查看全部评分

 楼主| 发表于 2009-6-17 10:51 | 显示全部楼层
谢谢楼上的O(∩_∩)O~代码已经用上了!

[ 本帖最后由 悠若谷 于 2009-6-17 11:21 编辑 ]
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-6-26 20:53 , Processed in 0.060859 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表