Matlab技巧記錄

1. 獲取程序的時(shí)間消耗

//在程序的開(kāi)頭加入
profile clear
profile off
profile on
//在末尾加入
profile viewer
profile off

借助這profile這個(gè)工具可以查看具體的程序每個(gè)函數(shù)消耗的時(shí)間,從而根據(jù)時(shí)間消耗來(lái)對(duì)程序的運(yùn)行時(shí)間進(jìn)行優(yōu)化。

time.png

很明顯上圖中state_solver中調(diào)用mupadmex和dsolve的次數(shù)特別多,消耗的時(shí)間也最大,所以需要搞清楚兩者的使用關(guān)系,運(yùn)行流程!

2. 給符號(hào)表達(dá)式賦值

syms x
f=3*x^3;
g=diff(f,x);
x=2;
% 執(zhí)行f和g對(duì)應(yīng)的表達(dá)式
ansf=eval(f)
ansg=eval(g)

該方法用于符號(hào)運(yùn)算及其方便。

3. 加快程序的速度

  • 用乘法,別用除法
  • 用向量運(yùn)算,別用標(biāo)量運(yùn)算
  • 使用profile探明時(shí)間消耗原因
  • 能夠獲取解析解進(jìn)行計(jì)算時(shí),盡量用解析解計(jì)算或者直接迭代計(jì)算,而不是采用符號(hào)函數(shù)計(jì)算,解析符號(hào)函數(shù)十分耗費(fèi)時(shí)間

4. 局部圖像放大,作圖中圖

首先將測(cè)試程序test.m和插件magnify.m放到同一文件夾下

function magnify(f1)
%magnify是個(gè)動(dòng)態(tài)放大鏡,固化后可以用tools>edit plot移動(dòng)小圖,能選取多個(gè)局部圖
%用法:打開(kāi)figure圖,輸入magnify,左鍵動(dòng)態(tài)選取查看,ctrl+左鍵固化,也可右鍵固化,‘<’和‘>’縮放方法范圍,‘+’和‘-’縮放放大比例
%magnify(f1)
%
%  Figure creates a magnification box when under the mouse
%  position when a button is pressed.  Press '+'/'-' while
%  button pressed to increase/decrease magnification. Press
%  '>'/'<' while button pressed to increase/decrease box size.
%  Hold 'Ctrl' while clicking to leave magnification on figure.
%
%  Example:
%     plot(1:100,randn(1,100),(1:300)/3,rand(1,300)), grid on,
%     magnify;

% Rick Hindman - 7/29/04

if (nargin == 0), f1 = gcf; end;
set(f1, ...
   'WindowButtonDownFcn',  @ButtonDownCallback, ...
   'WindowButtonUpFcn', @ButtonUpCallback, ...
   'WindowButtonMotionFcn', @ButtonMotionCallback, ...
   'KeyPressFcn', @KeyPressCallback);
return;

function ButtonDownCallback(src,eventdata)
   f1 = src;
   a1 = get(f1,'CurrentAxes');
   a2 = copyobj(a1,f1);

   set(f1, ...
      'UserData',[f1,a1,a2], ...
      'Pointer','fullcrosshair', ...
      'CurrentAxes',a2);
   set(a2, ...
      'UserData',[2,0.2], ...  %magnification, frame size
      'Color',get(a1,'Color'), ...
      'Box','on');
   xlabel(''); ylabel(''); zlabel(''); title('');
   set(get(a2,'Children'), ...
      'LineWidth', 2);
   set(a1, ...
      'Color',get(a1,'Color')*0.95);
   set(f1, ...
      'CurrentAxes',a1);
   ButtonMotionCallback(src);
return;

function ButtonUpCallback(src,eventdata)
   H = get(src,'UserData');
   f1 = H(1); a1 = H(2); a2 = H(3);
   set(a1, ...
      'Color',get(a2,'Color'));
   set(f1, ...
      'UserData',[], ...
      'Pointer','arrow', ...
      'CurrentAxes',a1);
   if ~strcmp(get(f1,'SelectionType'),'alt'),
      delete(a2);
   end;
return;

function ButtonMotionCallback(src,eventdata)
   H = get(src,'UserData');
   if ~isempty(H)
      f1 = H(1); a1 = H(2); a2 = H(3);
      a2_param = get(a2,'UserData');
      f_pos = get(f1,'Position');
      a1_pos = get(a1,'Position');

      [f_cp, a1_cp] = pointer2d(f1,a1);

      set(a2,'Position',[(f_cp./f_pos(3:4)) 0 0]+a2_param(2)*a1_pos(3)*[-1 -1 2 2]);
      a2_pos = get(a2,'Position');

   set(a2,'XLim',a1_cp(1)+(1/a2_param(1))*(a2_pos(3)/a1_pos(3))*diff(get(a1,'XLim'))*[-0.5 0.5]);
   set(a2,'YLim',a1_cp(2)+(1/a2_param(1))*(a2_pos(4)/a1_pos(4))*diff(get(a1,'YLim'))*[-0.5 0.5]);
   end;
return;

function KeyPressCallback(src,eventdata)
   H = get(gcf,'UserData');
   if ~isempty(H)
      f1 = H(1); a1 = H(2); a2 = H(3);
      a2_param = get(a2,'UserData');
      if (strcmp(get(f1,'CurrentCharacter'),'+') | strcmp(get(f1,'CurrentCharacter'),'='))
         a2_param(1) = a2_param(1)*1.2;
      elseif (strcmp(get(f1,'CurrentCharacter'),'-') | strcmp(get(f1,'CurrentCharacter'),'_'))
         a2_param(1) = a2_param(1)/1.2;
      elseif (strcmp(get(f1,'CurrentCharacter'),'<') | strcmp(get(f1,'CurrentCharacter'),','))
         a2_param(2) = a2_param(2)/1.2;
      elseif (strcmp(get(f1,'CurrentCharacter'),'>') | strcmp(get(f1,'CurrentCharacter'),'.'))
         a2_param(2) = a2_param(2)*1.2;
      end;
      set(a2,'UserData',a2_param);
   ButtonMotionCallback(src);
   end;
return;

% Included for completeness (usually in own file)
function [fig_pointer_pos, axes_pointer_val] = pointer2d(fig_hndl,axes_hndl)
%
%pointer2d(fig_hndl,axes_hndl)
%
% Returns the coordinates of the pointer (in pixels)
% in the desired figure (fig_hndl) and the coordinates
%       in the desired axis (axes coordinates)
%
% Example:
%  figure(1),
%  hold on,
%  for i = 1:1000,
%     [figp,axp]=pointer2d;
%     plot(axp(1),axp(2),'.','EraseMode','none');
%     drawnow;
%  end;
%  hold off

% Rick Hindman - 4/18/01

if (nargin == 0), fig_hndl = gcf; axes_hndl = gca; end;
if (nargin == 1), axes_hndl = get(fig_hndl,'CurrentAxes'); end;

set(fig_hndl,'Units','pixels');

pointer_pos = get(0,'PointerLocation'); %pixels {0,0} lower left
fig_pos = get(fig_hndl,'Position'); %pixels {l,b,w,h}

fig_pointer_pos = pointer_pos - fig_pos([1,2]);
set(fig_hndl,'CurrentPoint',fig_pointer_pos);

if (isempty(axes_hndl)),
axes_pointer_val = [];
elseif (nargout == 2),
axes_pointer_line = get(axes_hndl,'CurrentPoint');
axes_pointer_val = sum(axes_pointer_line)/2;
end;

test.m文件內(nèi)容如下:

x=-1:0.1:1;
y1=sin(x);
y2=tan(x);
y3=x;
plot(x,y1,x,y2,x,y3);
title('對(duì)數(shù)函數(shù)圖像');
xlabel('x');
ylabel('y');
grid on;

運(yùn)行test.m,在命令窗口輸入magnify,然后右鍵選中想要放大的區(qū)域(按著右鍵不要?jiǎng)?/strong>),然后可以使用<>縮放方法范圍,+-縮放放大比例,看著放大的小圖滿意后松開(kāi)右鍵即可。(這一過(guò)程網(wǎng)上都管他們叫固化),見(jiàn)詳細(xì)教程MATLAB中使用magnify做圖中圖
操作流程如下:

error1.gif

注意在進(jìn)行目標(biāo)區(qū)域的固化時(shí),一定要在圖像是不可編輯的狀態(tài)下進(jìn)行的,也就是小鼠標(biāo)未選中的狀態(tài)。

5. 繪制三維球體

繪制三維球體

6. 繪制三維立方體

畫(huà)立方體的幾種方法

7. 繪制圓柱體

使用matlab軟件繪制圓柱體

8. 將兩個(gè)曲線放在一個(gè)圖里面

在顯示的圖像窗口中,在可編輯狀態(tài)下,選中曲線復(fù)制,然后到另外一個(gè)圖像窗口下,選擇粘貼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近在看一篇論文,覺(jué)得文章的數(shù)據(jù)處理效果十分的驚艷!所以想著如何用matlab將類(lèi)似的效果實(shí)現(xiàn)出來(lái),但最近有一個(gè)任...
    酵母小木閱讀 927評(píng)論 0 1
  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,929評(píng)論 2 59
  • 轉(zhuǎn)自 http://www.kylen314.com/archives/412 不顯示坐標(biāo)刻度: set(gca,...
    天之道天知道閱讀 2,207評(píng)論 0 2
  • abandon, desert, forsake, leave, give up abandon :強(qiáng)調(diào)永遠(yuǎn)或完全...
    sunxiaohang閱讀 3,729評(píng)論 0 3
  • 一、Unity簡(jiǎn)介 1. Unity界面 Shift + Space : 放大界面 Scene界面按鈕渲染模式2D...
    MYves閱讀 8,662評(píng)論 0 22

友情鏈接更多精彩內(nèi)容