電子產業(yè)一站式賦能平臺

PCB聯(lián)盟網

搜索
查看: 18|回復: 0
收起左側

【好玩的源碼】MATLAB 繪制動態(tài)正弦函數

[復制鏈接]

238

主題

238

帖子

1400

積分

三級會員

Rank: 3Rank: 3

積分
1400
跳轉到指定樓層
樓主
發(fā)表于 2021-10-23 09:00:00 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
點擊上方藍字和“好玩的MATLAB”一起快樂玩耍吧!
/ A6 J' t* ~% g0 s* b: M/ L. _6 t7 p. q

1 s; T% i5 j, V5 R好玩的matlab
& A+ m& i' ]0 i" q% I+ z  E9 R; N帶你喜歡不一樣的matlab新玩法4 g/ U) f. k5 J9 F) [
3 E* Q8 C1 i* C! l( D" f) v( W+ N
今天有空整理一下抖音“好玩的matlab”的視頻源碼,分享一下供大家學習參考。$ Z+ O5 J  v# _1 W0 g, [) L  \
019 M5 G+ L2 C9 X# D' ^
視頻效果
$ t) r8 C$ t5 g: G+ i$ m& n/ I$ a, Y! y: d& A" A5 u# u" P5 A
01
/ d/ X  p5 I1 |- g6 J/ e4 N; `. }8 v源碼如下- }& c8 k0 c& i' Q/ I
  • clear ;clc;close all;Np = 100;            % 空間點數dx = 2*pi/Np;        % 步長x  = 0:dx:(6*pi);    %  x 范圍
    : C/ k" T% y8 w$ o8 of1sin = sin(x);        f1cos = cos(x);
    0 q  ^0 C& n1 `% }Nt = 100;                % 圓上的點數dtheta = 2*pi/Nt;        % 圓上的點步長theta  = 0:dtheta:2*pi;  % 旋轉2pi
    ) R5 m' s; a% i: D$ E0 K% 畫圓x1=cos(theta);y1=sin(theta);
    : P( n* h! o! q3 I" oLx=length(x);Lw=2; Fs=12;f1=figure('color',[0,0,0]);' `: L( {. s* A+ w0 L
    pausefor 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% X$ U- `& @7 J, x( z
    : E! V/ T* c; _7 _
  • function varargout = ds2nfu(varargin)%% Process inputsnarginchk(1, 3);% Determine if axes handle is specifiedif 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 3 e6 V( O/ C$ L5 \$ e- N
    ' ...  'position vector or 2 equal length (x,y) vectors.'];
      ^/ C$ g9 q+ b% O$ @% Proceed with remaining inputsif 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)  endend
    7 _6 \& B$ G7 A( `. z3 ^) f%% Get limitsaxun = get(hAx,'Units');set(hAx,'Units','normalized');axpos = get(hAx,'Position');axlim = axis(hAx);axwidth = diff(axlim(1:2));axheight = diff(axlim(3:4));
    ) b! B  X0 B9 X2 ]8 A. @( d' q2 O%% Transform dataif 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& E) [2 t$ A) l# w
    %% Restore axes unitsset(hAx,'Units',axun)- j. G) M9 W9 g& [% K6 T, p
    往期精彩回顧
    - a, ~5 H- U9 ~% X8 |2 n
    & Y9 K1 y- V$ ~6 W4 r$ F推薦 | 源碼分享
    * j* e, L( k4 \推薦| 【高級繪圖】MATLAB怎么將圖形局部放大
    4 b) c. [1 n( ~1 O8 o! }: W0 Z
    8 B6 u3 c% W4 F) ^
    1 N( o% X2 K: V
  • 回復

    使用道具 舉報

    發(fā)表回復

    您需要登錄后才可以回帖 登錄 | 立即注冊

    本版積分規(guī)則


    聯(lián)系客服 關注微信 下載APP 返回頂部 返回列表