1. 获取程序的时间消耗
//在程序的开头加入
profile clear
profile off
profile on
//在末尾加入
profile viewer
profile off
借助这profile
这个工具可以查看具体的程序每个函数消耗的时间,从而根据时间消耗来对程序的运行时间进行优化。
很明显上图中state_solver中调用mupadmex和dsolve的次数特别多,消耗的时间也最大,所以需要搞清楚两者的使用关系,运行流程!
2. 给符号表达式赋值
syms x
f=3*x^3;
g=diff(f,x);
x=2;
% 执行f和g对应的表达式
ansf=eval(f)
ansg=eval(g)
该方法用于符号运算及其方便。
3. 加快程序的速度
- 用乘法,别用除法
- 用向量运算,别用标量运算
- 使用
profile
探明时间消耗原因 - 能够获取解析解进行计算时,尽量用解析解计算或者直接迭代计算,而不是采用符号函数计算,解析符号函数十分耗费时间
4. 局部图像放大,作图中图
首先将测试程序test.m
和插件magnify.m
放到同一文件夹下
function magnify(f1)
%magnify是个动态放大镜,固化后可以用tools>edit plot移动小图,能选取多个局部图
%用法:打开figure图,输入magnify,左键动态选取查看,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
文件内容如下:
x=-1:0.1:1;
y1=sin(x);
y2=tan(x);
y3=x;
plot(x,y1,x,y2,x,y3);
title('对数函数图像');
xlabel('x');
ylabel('y');
grid on;
运行test.m
,在命令窗口输入magnify
,然后右键选中想要放大的区域(按着右键不要动),然后可以使用<
和>
缩放方法范围,+
和-
缩放放大比例,看着放大的小图满意后松开右键即可。(这一过程网上都管他们叫固化),见详细教程MATLAB中使用magnify做图中图
操作流程如下:
注意在进行目标区域的固化时,一定要在图像是不可编辑的状态下进行的,也就是小鼠标未选中的状态。
5. 绘制三维球体
6. 绘制三维立方体
7. 绘制圆柱体
8. 将两个曲线放在一个图里面
在显示的图像窗口中,在可编辑状态
下,选中曲线复制
,然后到另外一个图像窗口下,选择粘贴