书籍:Matlab实用教程
工具:Matlab2021a
电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz
系统类型:64位操作系统,基于X64的处理器 windows10 专业版
第5章 Matlab程序设计
5.6 利用函数句柄执行函数
5.6.1 函数句柄的创建
h_fun=@fun
h_fun=str2func('cos')
h_array=str2func({'sin','cos','tan'})
h_fun = @fun
h_fun = @cos
error: str2func: FCN_NAME must be a string
5.6.2 用feval命令执行函数
function Ex0521(z)
y=Ex05(1)
function y=Ex05(z1)
t=0:0.1:20;
h_plotxy1=str2func('plotxy1')
h_plotxy2=str2func('plotxy2')
h_plotxy3=str2func('plotxy3')
if(z1>=0)&(z1<1)
y=feval(h_plotxy1,z1,t);
elseif(z1==1)
y=feval(h_plotxy2,z1,t);
else
y=feval(h_plotxy3,z1,t);
end
function y1=plotxy1(zeta,x)
y1=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));
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)
h_plotxy1 = @plotxy1
h_plotxy2 = @plotxy2
h_plotxy3 = @plotxy3
y =
Columns 1 through 8:
0 0.0047 0.0175 0.0369 0.0616 0.0902 0.1219 0.1558
Columns 9 through 16:
0.1912 0.2275 0.2642 0.3010 0.3374 0.3732 0.4082 0.4422
Columns 17 through 24:
0.4751 0.5068 0.5372 0.5663 0.5940 0.6204 0.6454 0.6691
Columns 25 through 32:
0.6916 0.7127 0.7326 0.7513 0.7689 0.7854 0.8009 0.8153
Columns 33 through 40:
0.8288 0.8414 0.8532 0.8641 0.8743 0.8838 0.8926 0.9008
Columns 41 through 48:
0.9084 0.9155 0.9220 0.9281 0.9337 0.9389 0.9437 0.9482
Columns 49 through 56:
0.9523 0.9561 0.9596 0.9628 0.9658 0.9686 0.9711 0.9734
Columns 57 through 64:
0.9756 0.9776 0.9794 0.9811 0.9826 0.9841 0.9854 0.9866
Columns 65 through 72:
0.9877 0.9887 0.9897 0.9905 0.9913 0.9920 0.9927 0.9933
Columns 73 through 80:
0.9939 0.9944 0.9949 0.9953 0.9957 0.9961 0.9964 0.9967
Columns 81 through 88:
0.9970 0.9972 0.9975 0.9977 0.9979 0.9981 0.9982 0.9984
Columns 89 through 96:
0.9985 0.9986 0.9988 0.9989 0.9990 0.9991 0.9991 0.9992
Columns 97 through 104:
0.9993 0.9993 0.9994 0.9995 0.9995 0.9995 0.9996 0.9996
Columns 105 through 112:
0.9997 0.9997 0.9997 0.9997 0.9998 0.9998 0.9998 0.9998
Columns 113 through 120:
0.9998 0.9998 0.9999 0.9999 0.9999 0.9999 0.9999 0.9999
Columns 121 through 128:
0.9999 0.9999 0.9999 0.9999 0.9999 0.9999 1.0000 1.0000
Columns 129 through 136:
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
Columns 137 through 144:
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
Columns 145 through 152:
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
Columns 153 through 160:
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
Columns 161 through 168:
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
Columns 169 through 176:
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
Columns 177 through 184:
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
Columns 185 through 192:
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
Columns 193 through 200:
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
Column 201:
1.0000

function Ex0521(z)
h_Ex0521=str2func('Ex05')
y=feval(h_Ex0521,1.5)
function y=Ex05(z1)
t=0:0.1:20;
h_plotxy1=str2func('plotxy1')
h_plotxy2=str2func('plotxy2')
h_plotxy3=str2func('plotxy3')
if(z1>=0)&(z1<1)
y=feval(h_plotxy1,z1,t);
elseif(z1==1)
y=feval(h_plotxy2,z1,t);
else
y=feval(h_plotxy3,z1,t);
end
function y1=plotxy1(zeta,x)
y1=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));
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)
h_Ex0521 = @Ex05
h_plotxy1 = @plotxy1
h_plotxy2 = @plotxy2
h_plotxy3 = @plotxy3
y =
Columns 1 through 6:
-2.2204e-16 4.5317e-03 1.6482e-02 3.3825e-02 5.5013e-02 7.8867e-02
Columns 7 through 12:
1.0449e-01 1.3120e-01 1.5849e-01 1.8597e-01 2.1335e-01 2.4043e-01
Columns 13 through 18:
2.6705e-01 2.9309e-01 3.1849e-01 3.4319e-01 3.6715e-01 3.9037e-01
Columns 19 through 24:
4.1283e-01 4.3454e-01 4.5550e-01 4.7573e-01 4.9525e-01 5.1406e-01
Columns 25 through 30:
5.3219e-01 5.4966e-01 5.6649e-01 5.8270e-01 5.9831e-01 6.1334e-01
Columns 31 through 36:
6.2782e-01 6.4175e-01 6.5517e-01 6.6808e-01 6.8052e-01 6.9249e-01
Columns 37 through 42:
7.0401e-01 7.1510e-01 7.2577e-01 7.3605e-01 7.4594e-01 7.5546e-01
Columns 43 through 48:
7.6462e-01 7.7344e-01 7.8193e-01 7.9011e-01 7.9797e-01 8.0554e-01
Columns 49 through 54:
8.1283e-01 8.1984e-01 8.2660e-01 8.3309e-01 8.3935e-01 8.4537e-01
Columns 55 through 60:
8.5116e-01 8.5674e-01 8.6211e-01 8.6728e-01 8.7225e-01 8.7704e-01
Columns 61 through 66:
8.8165e-01 8.8608e-01 8.9035e-01 8.9446e-01 8.9842e-01 9.0222e-01
Columns 67 through 72:
9.0589e-01 9.0942e-01 9.1281e-01 9.1608e-01 9.1922e-01 9.2225e-01
Columns 73 through 78:
9.2516e-01 9.2797e-01 9.3067e-01 9.3327e-01 9.3577e-01 9.3817e-01
Columns 79 through 84:
9.4049e-01 9.4272e-01 9.4487e-01 9.4693e-01 9.4892e-01 9.5084e-01
Columns 85 through 90:
9.5268e-01 9.5445e-01 9.5616e-01 9.5780e-01 9.5938e-01 9.6091e-01
Columns 91 through 96:
9.6237e-01 9.6378e-01 9.6514e-01 9.6645e-01 9.6770e-01 9.6891e-01
Columns 97 through 102:
9.7008e-01 9.7120e-01 9.7228e-01 9.7332e-01 9.7432e-01 9.7528e-01
Columns 103 through 108:
9.7621e-01 9.7710e-01 9.7796e-01 9.7878e-01 9.7958e-01 9.8034e-01
Columns 109 through 114:
9.8108e-01 9.8179e-01 9.8247e-01 9.8313e-01 9.8376e-01 9.8437e-01
Columns 115 through 120:
9.8495e-01 9.8552e-01 9.8606e-01 9.8658e-01 9.8709e-01 9.8757e-01
Columns 121 through 126:
9.8804e-01 9.8848e-01 9.8892e-01 9.8933e-01 9.8973e-01 9.9012e-01
Columns 127 through 132:
9.9049e-01 9.9084e-01 9.9119e-01 9.9152e-01 9.9183e-01 9.9214e-01
Columns 133 through 138:
9.9244e-01 9.9272e-01 9.9299e-01 9.9325e-01 9.9351e-01 9.9375e-01
Columns 139 through 144:
9.9398e-01 9.9421e-01 9.9443e-01 9.9464e-01 9.9484e-01 9.9503e-01
Columns 145 through 150:
9.9522e-01 9.9540e-01 9.9557e-01 9.9573e-01 9.9589e-01 9.9605e-01
Columns 151 through 156:
9.9620e-01 9.9634e-01 9.9648e-01 9.9661e-01 9.9674e-01 9.9686e-01
Columns 157 through 162:
9.9698e-01 9.9709e-01 9.9720e-01 9.9730e-01 9.9740e-01 9.9750e-01
Columns 163 through 168:
9.9759e-01 9.9768e-01 9.9777e-01 9.9786e-01 9.9794e-01 9.9801e-01
Columns 169 through 174:
9.9809e-01 9.9816e-01 9.9823e-01 9.9829e-01 9.9836e-01 9.9842e-01
Columns 175 through 180:
9.9848e-01 9.9854e-01 9.9859e-01 9.9864e-01 9.9869e-01 9.9874e-01
Columns 181 through 186:
9.9879e-01 9.9884e-01 9.9888e-01 9.9892e-01 9.9896e-01 9.9900e-01
Columns 187 through 192:
9.9904e-01 9.9907e-01 9.9911e-01 9.9914e-01 9.9917e-01 9.9921e-01
Columns 193 through 198:
9.9924e-01 9.9926e-01 9.9929e-01 9.9932e-01 9.9934e-01 9.9937e-01
Columns 199 through 201:
9.9939e-01 9.9941e-01 9.9944e-01

function Ex0521(z)
y=feval('Ex05',0.5)
function y=Ex05(z1)
t=0:0.1:20;
h_plotxy1=str2func('plotxy1')
h_plotxy2=str2func('plotxy2')
h_plotxy3=str2func('plotxy3')
if(z1>=0)&(z1<1)
y=feval(h_plotxy1,z1,t);
elseif(z1==1)
y=feval(h_plotxy2,z1,t);
else
y=feval(h_plotxy3,z1,t);
end
function y1=plotxy1(zeta,x)
y1=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));
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)
h_plotxy1 = @plotxy1
h_plotxy2 = @plotxy2
h_plotxy3 = @plotxy3
y =
Columns 1 through 6:
-2.2204e-16 4.8334e-03 1.8669e-02 4.0519e-02 6.9413e-02 1.0441e-01
Columns 7 through 12:
1.4458e-01 1.8907e-01 2.3704e-01 2.8769e-01 3.4030e-01 3.9417e-01
Columns 13 through 18:
4.4868e-01 5.0324e-01 5.5734e-01 6.1049e-01 6.6229e-01 7.1237e-01
Columns 19 through 24:
7.6043e-01 8.0618e-01 8.4943e-01 8.8999e-01 9.2773e-01 9.6258e-01
Columns 25 through 30:
9.9446e-01 1.0234e+00 1.0493e+00 1.0723e+00 1.0924e+00 1.1097e+00
Columns 31 through 36:
1.1244e+00 1.1364e+00 1.1460e+00 1.1533e+00 1.1585e+00 1.1616e+00
Columns 37 through 42:
1.1630e+00 1.1626e+00 1.1607e+00 1.1575e+00 1.1531e+00 1.1477e+00
Columns 43 through 48:
1.1413e+00 1.1343e+00 1.1266e+00 1.1184e+00 1.1099e+00 1.1012e+00
Columns 49 through 54:
1.0923e+00 1.0834e+00 1.0746e+00 1.0659e+00 1.0574e+00 1.0491e+00
Columns 55 through 60:
1.0412e+00 1.0336e+00 1.0265e+00 1.0197e+00 1.0134e+00 1.0076e+00
Columns 61 through 66:
1.0023e+00 9.9744e-01 9.9308e-01 9.8920e-01 9.8580e-01 9.8285e-01
Columns 67 through 72:
9.8034e-01 9.7826e-01 9.7659e-01 9.7529e-01 9.7436e-01 9.7376e-01
Columns 73 through 78:
9.7346e-01 9.7345e-01 9.7369e-01 9.7415e-01 9.7482e-01 9.7566e-01
Columns 79 through 84:
9.7666e-01 9.7778e-01 9.7901e-01 9.8032e-01 9.8169e-01 9.8310e-01
Columns 85 through 90:
9.8455e-01 9.8600e-01 9.8744e-01 9.8887e-01 9.9027e-01 9.9163e-01
Columns 91 through 96:
9.9293e-01 9.9418e-01 9.9537e-01 9.9649e-01 9.9753e-01 9.9850e-01
Columns 97 through 102:
9.9939e-01 1.0002e+00 1.0009e+00 1.0016e+00 1.0022e+00 1.0027e+00
Columns 103 through 108:
1.0031e+00 1.0035e+00 1.0037e+00 1.0040e+00 1.0041e+00 1.0043e+00
Columns 109 through 114:
1.0043e+00 1.0043e+00 1.0043e+00 1.0042e+00 1.0041e+00 1.0040e+00
Columns 115 through 120:
1.0039e+00 1.0037e+00 1.0035e+00 1.0033e+00 1.0030e+00 1.0028e+00
Columns 121 through 126:
1.0026e+00 1.0023e+00 1.0021e+00 1.0019e+00 1.0016e+00 1.0014e+00
Columns 127 through 132:
1.0012e+00 1.0010e+00 1.0008e+00 1.0006e+00 1.0004e+00 1.0003e+00
Columns 133 through 138:
1.0001e+00 1.0000e+00 9.9988e-01 9.9977e-01 9.9967e-01 9.9959e-01
Columns 139 through 144:
9.9951e-01 9.9945e-01 9.9940e-01 9.9936e-01 9.9933e-01 9.9931e-01
Columns 145 through 150:
9.9930e-01 9.9929e-01 9.9930e-01 9.9931e-01 9.9932e-01 9.9934e-01
Columns 151 through 156:
9.9936e-01 9.9939e-01 9.9942e-01 9.9946e-01 9.9949e-01 9.9953e-01
Columns 157 through 162:
9.9957e-01 9.9961e-01 9.9965e-01 9.9968e-01 9.9972e-01 9.9976e-01
Columns 163 through 168:
9.9979e-01 9.9983e-01 9.9986e-01 9.9989e-01 9.9992e-01 9.9995e-01
Columns 169 through 174:
9.9997e-01 9.9999e-01 1.0000e+00 1.0000e+00 1.0000e+00 1.0001e+00
Columns 175 through 180:
1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00
Columns 181 through 186:
1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00
Columns 187 through 192:
1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00
Columns 193 through 198:
1.0001e+00 1.0001e+00 1.0001e+00 1.0001e+00 1.0000e+00 1.0000e+00
Columns 199 through 201:
1.0000e+00 1.0000e+00 1.0000e+00

5.7 利用泛函命令进行数值分析
5.7.1 求极小值
[x,y]=fminbnd(@humps,0.5,0.8)
x = 0.6370
y = 11.253
x=0:0.01:2;
y=humps(x);
plot(x,y)

fn=inline('100*(x(2)-x(1)^2)^2+(1-x(1))^2','x')
y=fminsearch(fn,[0.5,-1])
y =
1.0000 1.0000
5.7.2 求过零点
xzero=fzero(@humps,1)
xzero=fzero(@humps,[0.5,1.5])
xzero=fzero(@humps,[0.5,1])
xzero = 1.2995
xzero = 1.2995
error: fzero: not a valid initial bracketing
error: called from
fzero at line 228 column 5
main at line 3 column 6
/opt/run_user_code.m at line 1 column 1
5.7.4 微分方程的数值解
function [t,y]=Ex0526(z)
tspan=[0,30];
y0=[1;0];
[t,y]=ode45(@vdpol,tspan,y0)
function yprime=vdpol(t,y)
y=0:0.01:1;
yprime=[y(2);2*(1-y(1)^2)*y(2)-y(1)]
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
yprime =
0.010000
0.020000
t =
0
0.1468
0.3669
0.6972
1.1926
1.9357
3.0503
4.7222
7.2301
10.2301
13.2301
16.2301
19.2301
22.2301
25.2301
28.2301
30.0000
y =
1.0000 0
1.0015 0.0029
1.0037 0.0073
1.0070 0.0139
1.0119 0.0239
1.0194 0.0387
1.0305 0.0610
1.0472 0.0944
1.0723 0.1446
1.1023 0.2046
1.1323 0.2646
1.1623 0.3246
1.1923 0.3846
1.2223 0.4446
1.2523 0.5046
1.2823 0.5646
1.3000 0.6000
function [t,y]=Ex0526(z)
tspan=[0,30];
y0=[1;0];
[t,y]=ode45(@vdpol,tspan,y0);
y1=y(:,1);
y2=y(:,2);
figure(1)
plot(t,y1,':b',t,y2,'-r')
function yprime=vdpol(t,y)
y;
yprime=[y(2);2*(1-y(1)^2)*y(2)-y(1)];

function [t,y]=Ex0526(z)
tspan=[0,30];
y0=[1;0];
[t,y]=ode45(@vdpol,tspan,y0);
y1=y(:,1);
y2=y(:,2);
figure(2)
plot(y1,y2)
function yprime=vdpol(t,y)
y;
yprime=[y(2);2*(1-y(1)^2)*y(2)-y(1)];
