MATLAB 绘制动态正弦函数

一、动态正弦函数

动态正弦函数

二、MATLAB 绘制动态正弦函数代码

clear ;
clc;
close all;
Np = 100;            % 空间点数
dx = 2*pi/Np;        % 步长
x  = 0:dx:(6*pi);    %  x 范围
f1sin = sin(x);        f1cos = cos(x);
Nt = 100;                % 圆上的点数
dtheta = 2*pi/Nt;        % 圆上的点步长
theta  = 0:dtheta:2*pi;  % 旋转2pi
% 画圆
x1=cos(theta);
y1=sin(theta);
Lx=length(x);
Lw=2; Fs=12;
f1=figure('color',[0,0,0]);
for i=1:length(x)
    %disp([num2str(i) ' of ' num2str(Lx)])
    clf;
    sp1=subplot(1,2,1);
    %  -- 1st harmonic ---
    plot(x1,y1,'-.m','LineWidth',1); hold on; grid on;
    line([0 f1cos(i)],[0 f1sin(i)],'Color','w','LineWidth',1,'LineSmoothing','on');
    set(sp1,'Position',[0.0400    0.1800    0.4    0.677],'color',[0,0,0],'XColor',[1 1 1],'YColor',[1 1 1])
    xlim([-2.5 2.5]); ylim([-2.5 2.5])
%         axis equal    
    [xf1, yf1] = ds2nfu(sp1,f1cos(i),f1sin(i));     % Convert axes coordinates to figure coordinates for 1st axes    line(f1cos(i),f1sin(i),10,'LineStyle','-.','MarkerSize',8,'MarkerFaceColor','b','color','b')    
    sp2=subplot(1,2,2);
    %  -- 1st harmonic ---
    plot(x(1:i),f1sin(1:i),'LineWidth',Lw,'Color','b'); hold on; grid on;    
    ylim([-2.5 2.5]); xlim([0 19])
    set(sp2,'Position',[0.48    0.178200    0.49    0.680],'color',[0,0,0],'XColor',[1 1 1],'YColor',[1 1 1]);    
    % Convert axes coordinates to figure coordinates for 1st axes
    [xg1, yg1] = ds2nfu(x(i),f1sin(i));
    annotation('line',[xf1 xg1],[yf1 yg1],'color','g','LineStyle',':','LineWidth',2);
    pause(0.00001);
end

function varargout = ds2nfu(varargin)
%% Process inputs
narginchk(1, 3);
% Determine if axes handle is specified
if length(varargin{1})== 1 && ishandle(varargin{1}) && strcmp(get(varargin{1},'type'),'axes')  
  hAx = varargin{1};
  varargin = varargin(2:end);
else
  hAx = gca;
end;
errmsg = ['Invalid input.  Coordinates must be specified as 1 four-element \n' ...
  'position vector or 2 equal length (x,y) vectors.'];

% Proceed with remaining inputs
if length(varargin)==1  % Must be 4 elt POS vector
  pos = varargin{1};
  if length(pos) ~=4, 
    error(errmsg);
  end;
else
  [x,y] = deal(varargin{:});
  if length(x) ~= length(y)
    error(errmsg)
  end
end
%% Get limits
axun = get(hAx,'Units');
set(hAx,'Units','normalized');
axpos = get(hAx,'Position');
axlim = axis(hAx);
axwidth = diff(axlim(1:2));
axheight = diff(axlim(3:4));
%% Transform data
if exist('x','var')
  varargout{1} = (x-axlim(1))*axpos(3)/axwidth + axpos(1);
  varargout{2} = (y-axlim(3))*axpos(4)/axheight + axpos(2);
else
  pos(1) = (pos(1)-axlim(1))/axwidth*axpos(3) + axpos(1);
  pos(2) = (pos(2)-axlim(3))/axheight*axpos(4) + axpos(2);
  pos(3) = pos(3)*axpos(3)/axwidth;
  pos(4) = pos(4)*axpos(4)/axheight;
  varargout{1} = pos;
end
%% Restore axes units
set(hAx,'Units',axun)
end

猜你喜欢

转载自blog.csdn.net/qq_45823589/article/details/130773863