[xx,yy]=ginput(1) xx= 1.5054e-008 yy= 3.1416
图 4.1-6 函数极值点附近的局部放大和交互式取值
【例4.1-8】求
f(x,y)?100(y?x2)2?(1?x)2的极小值点。它即是著名的Rosenbrock's
\测试函数,它的理论极小值是x(1)
(2)
?1,y?1。
ff=@(x)(100*(x(2)-x(1).^2)^2+(1-x(1))^2);
x0=[-5,-2,2,5;-5,-2,2,5];
[sx,sfval,sexit,soutput]=fminsearch(ff,x0) sx =
1.0000 -0.6897 0.4151 8.0886 1.0000 -1.9168 4.9643 7.8004 sfval =
2.4112e-010 sexit = 1
soutput =
iterations: 384 funcCount: 615
algorithm: [1x33 char] message: [1x196 char]
(3)检查目标函数值
format short e
disp([ff(sx(:,1)),ff(sx(:,2)),ff(sx(:,3)),ff(sx(:,4))]) Columns 1 through 3
2.4112e-010 5.7525e+002 2.2967e+003 Column 4
3.3211e+005
4.1.5
常微分方程的数值解
6
d2x2dx??(1?x)?x?0,??2,在初始条件【例 4.1-9】求微分方程2dtdtdx(0)x(0)?1,?0情况下的解,并图示。(见图4.1-7和4.1-8)
dt(1) (2) [DyDt.m]
function ydot=DyDt(t,y) mu=2;
ydot=[y(2);mu*(1-y(1)^2)*y(2)-y(1)];
(3)解算微分方程
tspan=[0,30]; y0=[1;0];
[tt,yy]=ode45(@DyDt,tspan,y0); plot(tt,yy(:,1))
xlabel('t'),title('x(t)')
图 4.1-7 微分方程解
(4)
plot(yy(:,1),yy(:,2))
xlabel('位移'),ylabel('速度')
图 4.1-8 平面相轨迹
7
4.2
4.2.1 一
矩阵和代数方程
矩阵运算和特征参数 矩阵运算
【例 4.2-1】已知矩阵A2?4,B4?3,采用三种不同的编程求这两个矩阵的乘积
C2?3?A2?4B4?3。
(1)
clear
rand('twister',12)
A=rand(2,4);B=rand(4,3); %------------------
C1=zeros(size(A,1),size(B,2)); for ii=1:size(A,1) for jj=1:size(B,2) for k=1:size(A,2) C1(ii,jj)=C1(ii,jj)+A(ii,k)*B(k,jj); end end end C1 C1 =
0.7337 0.8396 0.3689 1.0624 1.1734 1.3787
(2)
%------------------
C2=zeros(size(A,1),size(B,2)); for jj=1:size(B,2) for k=1:size(B,1) C2(:,jj)=C2(:,jj)+A(:,k)*B(k,jj); end end C2 C2 =
0.7337 0.8396 0.3689 1.0624 1.1734 1.3787
(3)
C3=A*B, C3 =
0.7337 0.8396 0.3689 1.0624 1.1734 1.3787
(3)
C3_C3=norm(C3-C1,'fro') C3_C2=norm(C3-C2,'fro') C3_C3 = 0 C3_C2 = 0
【例 4.2-2】观察矩阵的转置操作和数组转置操作的差别。
format rat A=magic(2)+j*pascal(2) A =
Column 1
8
1 + 1i 4 + 1i Column 2
3 + 1i 2 + 2i %
A1=A' A2=A.' A1 =
Column 1
1 - 1i 3 - 1i Column 2
4 - 1i 2 - 2i A2 =
Column 1
1 + 1i 3 + 1i Column 2
4 + 1i 2 + 2i
B1=A*A' B2=A.*A' C1=A*A.' C2=A.*A.' B1 =
Column 1
12 13 + 1i Column 2
13 - 1i 25 B2 =
Column 1
2 13 - 1i Column 2
13 + 1i 8 C1 =
Column 1
8 + 8i 7 + 13i Column 2
7 + 13i 15 + 16i C2 =
Column 1
0 + 2i 11 + 7i Column 2
11 + 7i 0 + 8i
二 矩阵的标量特征参数
【例4.2-3】矩阵标量特征参数计算示例。A=reshape(1:9,3,3) r=rank(A)
9
d3=det(A) d2=det(A(1:2,1:2)) t=trace(A) A =
Columns 1 through 2
1 4 2 5 3 6 Column 3
7 8 9 r =
2 d3 =
0 d2 =
-3 t =
15
【例4.2-4】矩阵标量特征参数的性质。
format short
rand('twister',0) A=rand(3,3); B=rand(3,3); C=rand(3,4); D=rand(4,3);
tAB=trace(A*B) tBA=trace(B*A)
tCD=trace(C*D) tDC=trace(D*C) tAB =
2.6030 tBA =
2.6030 tCD =
4.1191 tDC =
4.1191
d_A_B=det(A)*det(B) dAB=det(A*B) dBA=det(B*A) d_A_B = 0.0094 dAB =
0.0094 dBA =
0.0094
dCD=det(C*D)
dDC=det(D*C) dCD =
0.0424 dDC =
-2.6800e-018
10