书籍:Matlab实用教程
工具:Matlab2021a
电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz
系统类型:64位操作系统,基于X64的处理器 windows10 专业版
第5章 Matlab程序设计
5.3 函数调用好人参互传递
5.1 子函数和私有函数
function Ex0501()
z1=0.3;
Ex0502(z1)
hold on
z1=0.5;
Ex0502(z1)
hold on
z1=0.707;
Ex0502(z1)
function Ex0502(zeta)
x=0:0.1:20;
y=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));
plot(x,y)

5.3.2 局部变量和全局变量
function Ex0512()
global X
X=0:0.1:20;
z1=0.3;
Ex0502(z1)
hold on
z1=0.5;
Ex0502(z1)
hold on
z1=0.707;
Ex0502(z1)
function Ex0502(zeta)
global X
y=1-1/sqrt(1-zeta^2)*exp(-zeta*X).*sin(sqrt(1-zeta^2)*X+acos(zeta));
plot(X,y)

5.3.3 函数的参数
function Ex0513()
z1=0.3;
[x1,y1]=Ex0502(z1)
plot(x1,y1)
hold on
z1=0.5;
[x2,y2]=Ex0502(z1)
plot(x2,y2)
hold on
z1=0.707;
[x3,y3]=Ex0502(z1)
plot(x3,y3)
function [x,y]=Ex0502(zeta)
x=0:0.1:20;
y=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));

function Ex0514()
y=Ex0502(2,3)
y=Ex0502(2)
y=Ex0502()
function sum=Ex0502(x,y)
if nargin==1
sum=x;
elseif nargin==0
sum=0;
else
sum=x+y;
End
y = 5
y = 2
y = 0
function Ex0515()
[a,b]=Ex0502(1,2,3,4)
[a,b]=Ex0502(2)
[a,b]=Ex0502()
function [y,n]=Ex0502(varargin)
if nargin==0
disp('No input variables.');
y=0;
elseif nargin==1
y=varargin{1};
else
n=nargin;
y=0;
for m=1:n
y=varargin{m}+y;
end
end
n=nargin;
a = 10
b = 4
a = 2
b = 1
No input variables.
a = 0
b = 0
function Ex0515()
y=Ex0502(0.3)
hold on
y=Ex0502(0.707)
y=Ex0502(1)
y=Ex0502(2)
function y=Ex0502(z1)
t=0:0.1:20;
if (z1>=0)&(z1<1)
y=plotxy1(z1,t);
elseif z1==1
y=plotxy2(z1,t);
else
y=plotxy3(z1,t);
end
function y1=plotxy1(zeta,x)
y1=1-1/sqrt(1-0.3^2)*exp(-0.3*x).*sin(sqrt(1-0.3^2)*x+acos(0.3));
plot(x,y1)
function y2=plotxy2(zeta,x)
y2=1-exp(-x).*(1+x);
plot(x,y2)
function y3=plotxy3(zeta,x)
y3=1-1/(2*sqrt(zeta^2-1))*(exp(-((zeta-sqrt(zeta^2-1))*x))./(zeta-sqrt(zeta^2-1))-exp(-((zeta+sqrt(zeta^2-1))*x))./(zeta+sqrt(zeta^2-1)));
plot(x,y3)

function Ex0517()
y=Ex0502(6)
function y=Ex0502(m)
y=0;
m=m-1;
for n=1:m
y=[0,y];
end
for n=1:m
a=[1:1:n];
b=a;
for k=m:-1:n
b=[0,b];
end
y=[b;y];
n=n+1;
End
y =
0 1 2 3 4 5
0 0 1 2 3 4
0 0 0 1 2 3
0 0 0 0 1 2
0 0 0 0 0 1
0 0 0 0 0 0
function Ex0517()
y=Ex0502(11)
function y=Ex0502(m)
y=0;
m=m-1;
for n=1:m
y=[0,y];
end
for n=1:m
a=[1:1:n];
b=a;
for k=m:-1:n
b=[0,b];
end
y=[b;y];
n=n+1;
End
y =
0 1 2 3 4 5 6 7 8 9 10
0 0 1 2 3 4 5 6 7 8 9
0 0 0 1 2 3 4 5 6 7 8
0 0 0 0 1 2 3 4 5 6 7
0 0 0 0 0 1 2 3 4 5 6
0 0 0 0 0 0 1 2 3 4 5
0 0 0 0 0 0 0 1 2 3 4
0 0 0 0 0 0 0 0 1 2 3
0 0 0 0 0 0 0 0 0 1 2
0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0
5.4 M文件性能的优化和加速
5.4.1 P码文件
5.4.2 M文件性能优化
function Ex0518()
y=Ex0502(21)
function y=Ex0502(m)
m=m-1;
y=zeros(m);
for n=1:m-1
a=1:m-n;
y(n,n+1:m)=a;
end
Y
y =
Columns 1 through 16:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6
0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5
0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4
0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 17 through 20:
16 17 18 19
15 16 17 18
14 15 16 17
13 14 15 16
12 13 14 15
11 12 13 14
10 11 12 13
9 10 11 12
8 9 10 11
7 8 9 10
6 7 8 9
5 6 7 8
4 5 6 7
3 4 5 6
2 3 4 5
1 2 3 4
0 1 2 3
0 0 1 2
0 0 0 1
0 0 0 0
5.4.3 JIT和加速器
5...5 内联函数
f=inline('sin(x)*exp(-z*x)','x','z')
y=f(5,0.3)
y = -0.2140
f=inline('sin(x)*exp(-z*x)','x','z')
y=f(5,0.3)
char(f)
class(f)
argnames(f)
y = -0.2140
ans = sin(x)*exp(-z*x)
ans = inline
ans =
{
[1,1] = x
[2,1] = z
}
f=inline('sin(x)*exp(-z*x)','x','z')
y=f(5,0.3);
char(f);
class(f);
argnames(f);
ff=vectorize(f)
x=0:0.1:20;
y=ff(x,0.3)
y =
Columns 1 through 8:
0 0.0969 0.1871 0.2701 0.3454 0.4126 0.4716 0.5222
Columns 9 through 16:
0.5643 0.5980 0.6234 0.6407 0.6503 0.6524 0.6475 0.6360
Columns 17 through 24:
0.6185 0.5955 0.5675 0.5352 0.4990 0.4597 0.4179 0.3740
Columns 25 through 32:
0.3288 0.2827 0.2363 0.1901 0.1446 0.1002 0.0574 0.0164
Columns 33 through 40:
-0.0224 -0.0586 -0.0921 -0.1228 -0.1503 -0.1746 -0.1957 -0.2135
Columns 41 through 48:
-0.2279 -0.2392 -0.2472 -0.2522 -0.2542 -0.2534 -0.2500 -0.2441
Columns 49 through 56:
-0.2360 -0.2259 -0.2140 -0.2005 -0.1856 -0.1697 -0.1529 -0.1355
Columns 57 through 64:
-0.1177 -0.0996 -0.0815 -0.0637 -0.0462 -0.0292 -0.0129 0.0025
Columns 65 through 72:
0.0171 0.0306 0.0430 0.0542 0.0642 0.0730 0.0805 0.0866
Columns 73 through 80:
0.0915 0.0952 0.0976 0.0989 0.0990 0.0981 0.0962 0.0934
Columns 81 through 88:
0.0898 0.0854 0.0804 0.0748 0.0688 0.0623 0.0556 0.0488
Columns 89 through 96:
0.0417 0.0347 0.0277 0.0208 0.0141 0.0076 0.0015 -0.0043
Columns 97 through 104:
-0.0098 -0.0148 -0.0194 -0.0235 -0.0271 -0.0302 -0.0328 -0.0349
Columns 105 through 112:
-0.0366 -0.0377 -0.0384 -0.0386 -0.0384 -0.0378 -0.0369 -0.0356
Columns 113 through 120:
-0.0340 -0.0322 -0.0301 -0.0278 -0.0253 -0.0228 -0.0201 -0.0174
Columns 121 through 128:
-0.0147 -0.0119 -0.0092 -0.0066 -0.0040 -0.0016 0.0008 0.0030
Columns 129 through 136:
0.0050 0.0068 0.0085 0.0100 0.0113 0.0124 0.0133 0.0140
Columns 137 through 144:
0.0145 0.0149 0.0150 0.0150 0.0149 0.0145 0.0141 0.0135
Columns 145 through 152:
0.0128 0.0121 0.0112 0.0103 0.0093 0.0083 0.0072 0.0062
Columns 153 through 160:
0.0051 0.0040 0.0030 0.0020 0.0010 0.0001 -0.0008 -0.0016
Columns 161 through 168:
-0.0024 -0.0031 -0.0037 -0.0042 -0.0047 -0.0050 -0.0054 -0.0056
Columns 169 through 176:
-0.0057 -0.0058 -0.0059 -0.0058 -0.0057 -0.0056 -0.0054 -0.0051
Columns 177 through 184:
-0.0048 -0.0045 -0.0042 -0.0038 -0.0034 -0.0030 -0.0026 -0.0022
Columns 185 through 192:
-0.0017 -0.0013 -0.0009 -0.0005 -0.0002 0.0002 0.0005 0.0008
Columns 193 through 200:
0.0011 0.0013 0.0016 0.0017 0.0019 0.0020 0.0021 0.0022
Column 201:
0.0023
f=inline('sin(x)*exp(-z*x)','x','z')
y=f(5,0.3);
char(f);
class(f);
argnames(f);
ff=vectorize(f)
x=0:0.1:20;
y=ff(x,0.3);
z=0:0.05:10;
y0=feval(ff,x,z)
y0 =
Columns 1 through 8:
0 0.0993 0.1947 0.2825 0.3595 0.4231 0.4716 0.5042
Columns 9 through 16:
0.5209 0.5225 0.5104 0.4867 0.4537 0.4139 0.3699 0.3238
Columns 17 through 24:
0.2779 0.2338 0.1927 0.1556 0.1231 0.0952 0.0719 0.0529
Columns 25 through 32:
0.0379 0.0263 0.0176 0.0112 0.0066 0.0036 0.0016 0.0003
Columns 33 through 40:
-0.0003 -0.0007 -0.0008 -0.0008 -0.0007 -0.0006 -0.0004 -0.0003
Columns 41 through 48:
-0.0003 -0.0002 -0.0001 -0.0001 -0.0001 -0.0000 -0.0000 -0.0000
Columns 49 through 56:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
Columns 57 through 64:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000
Columns 65 through 72:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 73 through 80:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 81 through 88:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 89 through 96:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -0.0000
Columns 97 through 104:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
Columns 105 through 112:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
Columns 113 through 120:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
Columns 121 through 128:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000
Columns 129 through 136:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 137 through 144:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 145 through 152:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 153 through 160:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000
Columns 161 through 168:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
Columns 169 through 176:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
Columns 177 through 184:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
Columns 185 through 192:
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 0.0000
Columns 193 through 200:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Column 201:
0.0000