新疆大学重点课程·电子教案 33 页 共 41 页
fill(real(x),imag(x),s) end axis('square') 课堂练习:
1. 分析解释程序。注意学习 nargin 和 nargout 的用法
2. 将该函数命名为 circle 存入指定目录,并在该目录下运行它 3. 解释 fill(real(x),imag(x),s)的含义
4. 适当改变函数中的语句使 circle 函数也可以画椭圆(留作练习) 例 68.运动的小球 function f=movingball(K,ki)
t1=(0:1000)/1000*10*pi; x1=cos(t1); y1=sin(t1); z1=-t1; t2=(0:10)/10; x2=x1(end)*(1-t2); y2=y1(end)*(1-t2); z2=z1(end)*ones(size(x2));
t3=t2; z3=(1-t3)*z1(end); x3=zeros(size(z3)); y3=x3; t4=t2; x4=t4; y4=zeros(size(x4)); z4=y4; x=[x1,x2,x3,x4]; y=[y1,y2,y3,y4]; z=[z1,z2,z3,z4]; plot3(x,y,z,'b'), axis off
h=line('Color',[1 0 0],'Marker','.','MarkerSize',40,'EraseMode','xor'); n=length(x); i=1; j=1; while 1
set(h,'xdata',x(i),'ydata',y(i),'zdata',z(i)); drawnow; pause(0.0005) i=i+1;
if nargin==2 & nargout==1
if(i==ki & j==1); f=getframe(gcf); end end
新疆大学重点课程·电子教案 34 页 共 41 页
if i>n i=1; j=j+1; if j>K ; break; end end end 课堂练习:
1. 分析解释程序。注意学习动画的制作方法 2. 注意学习 set(),pause(),getframe()函数的使用方法
研究题:编写一个函数,当输入任意一个封闭的 3 维曲线时,小球 沿该曲线作周期运动。 例 70.分析 peaks 函数
function [xz,y,z] = peaks(arg1,arg2); %PEAKS A sample function of two variables.
% PEAKS is a function of two variables, obtained by translating and % scaling Gaussian distributions, which is useful for demonstrating % MESH, SURF, PCOLOR, CONTOUR, etc. % There are several variants of the calling sequence: %
% Z = PEAKS; % Z = PEAKS(N); % Z = PEAKS(V); % Z = PEAKS(X,Y); %
% PEAKS; % PEAKS(N); % PEAKS(V); % PEAKS(X,Y); %
% [X,Y,Z] = PEAKS;
新疆大学重点课程·电子教案 35 页 共 41 页
% [X,Y,Z] = PEAKS(N); % [X,Y,Z] = PEAKS(V); %
% The first variant produces a 49-by-49 matrix. % The second variant produces an N-by-N matrix.
% The third variant produces an N-by-N matrix where N = length(V). % The fourth variant evaluates the function at the given X and Y, % which must be the same size. The resulting Z is also that size. %
% The next four variants, with no output arguments, do a SURF % plot of the result. %
% The last three variants also produce two matrices, X and Y, for % use in commands such as PCOLOR(X,Y,Z) or SURF(X,Y,Z,DEL2(Z)). %
% If not given as input, the underlying matrices X and Y are % [X,Y] = MESHGRID(V,V)
% where V is a given vector, or V is a vector of length N with % elements equally spaced from -3 to 3. If no input argument is % given, the default N is 49. % CBM, 2-1-92, 8-11-92, 4-30-94.
% Copyright 1984-2002 The MathWorks, Inc. % $Revision: 5.10 $ $Date: 2002/04/08 20:04:50 $ (1) if nargin == 0 (2) dx = 1/8;
(3) [x,y] = meshgrid(-3:dx:3); (4) elseif nargin == 1 (5) if length(arg1) == 1
新疆大学重点课程·电子教案 36 页 共 41 页
(6) [x,y] = meshgrid(-3:6/(arg1-1):3); (7) else
(8) [x,y] = meshgrid(arg1,arg1); (9) end (10) else
(11) x = arg1; y = arg2; (12) end
(13) z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2); (14) if nargout > 1 (15) xz = x;
(16) elseif nargout == 1 (17) xz = z; (18) else
% Self demonstration (19) disp(' ')
(20) disp('z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... ') (21) disp(' - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... ') (22) disp(' - 1/3*exp(-(x+1).^2 - y.^2) ') (23) disp(' ') (24) surf(x,y,z)
(25) axis([min(min(x)) max(max(x)) min(min(y)) max(max(y)) ... (26) min(min(z)) max(max(z))]) (27) xlabel('x'), ylabel('y'), title('Peaks') (28) end 程序流程图
相关推荐: