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

PCB聯(lián)盟網(wǎng)

搜索
查看: 15|回復: 0
收起左側(cè)

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

[復制鏈接]

238

主題

238

帖子

1400

積分

三級會員

Rank: 3Rank: 3

積分
1400
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2021-10-23 09:00:00 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
點擊上方藍字和“好玩的MATLAB”一起快樂玩耍吧!
( L, y9 K0 J. ]
, w( V  _! Y6 n; T5 G % u' n8 M) `& U- k7 E4 |: Y
好玩的matlab# O" n! [* n& W- j
帶你喜歡不一樣的matlab新玩法
6 h9 j+ T" p& t5 H5 }$ o2 g$ b: ?' e
6 M. Y) I( \% S+ p; ]/ G今天有空整理一下抖音“好玩的matlab”的視頻源碼,分享一下供大家學習參考。6 f. O* V- I5 u! {7 P- n5 _2 z' x
01; Q( Q8 o5 t+ H' w! J; ^
視頻效果
% n' d; P4 S( h. v% [; a9 E! [
01' l3 D$ S! T; g8 V7 A# M2 X' _
源碼如下7 Q. W  f( T6 v. B
  • clear ;clc;close all;Np = 100;            % 空間點數(shù)dx = 2*pi/Np;        % 步長x  = 0:dx:(6*pi);    %  x 范圍
    * s. Q2 R0 N6 S/ a1 zf1sin = sin(x);        f1cos = cos(x);" D1 _. ]3 l8 W+ ]7 L
    Nt = 100;                % 圓上的點數(shù)dtheta = 2*pi/Nt;        % 圓上的點步長theta  = 0:dtheta:2*pi;  % 旋轉(zhuǎn)2pi
    ! ?; ~3 C: t: ~% 畫圓x1=cos(theta);y1=sin(theta);
    , M: r. ]" L9 }+ X; sLx=length(x);Lw=2; Fs=12;f1=figure('color',[0,0,0]);
    , o* H7 q$ K) y7 x% k/ z. y8 _5 _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# C$ l4 Y2 b
    6 {) q  A6 _5 s4 u( `" W
  • 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
    * L) U8 p/ h: I& s, n' ...  'position vector or 2 equal length (x,y) vectors.'];
    + ^- F4 y! B: K1 J% 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)  endend8 n* ^' X& O" N' F+ Y
    %% 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));
    ) L0 u  s6 m" y5 _1 A! N, \& h%% 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
    ( H) r0 q! D4 ^( H/ c& V! j%% Restore axes unitsset(hAx,'Units',axun)/ E4 S' r( {) a: h0 q( O: U
    往期精彩回顧
    ! q# h& I/ p/ _, U6 G % \* k' d% L. o
    推薦 | 源碼分享5 I5 E' r" G3 R2 X+ Z4 V
    推薦| 【高級繪圖】MATLAB怎么將圖形局部放大  {9 M; y: ~) J  v. U8 q
    3 t5 T( n9 ^- D

    , V/ |" b! q* k% ]9 H
  • 回復

    使用道具 舉報

    發(fā)表回復

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

    本版積分規(guī)則


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