第四章 MATLAB的数值计算功能(2)(6)

2019-04-09 19:42

[x,fual,exitflag,output]=fminsearch(fun,x0,options): 单纯形法求多元函数极值点指令的最完整格式。

[x,fual,exitflag,output,grad,hessian]=fminunc(fun,x0,options): 拟牛顿法求多元函数极值点指令的最完整格式。

其中x1,x2分别表示被研究区间的左、右边界。 fminsearch和fminunc中的x0是一个向量,表示极值点位置的初步推测。输入变量options的默认值可以用options=optimset('FunFun_Name')获得。 例1,求解函数的极值 %求最小值

fn='2*exp(-x)*sin(x)'; %定义fn函数

xmin=fminbnd(fn,2,5) %在区间(2,5) 寻找最小值 emin=5*pi/4-xmin %求最小值的误差 x=xmin; %令x为最小值点

ymin=eval(fn) %计算最小值点的函数值 %求最大值

fx='-(2*exp(-x)+sin(x))'; % 为求最大值,函数值取负 xmax=fminbnd(fx,0,3) %在区间(0,3)寻找最大值 emax=pi/4-xmax %求最大值的误差

ymax=eval(fx) %求最大值点的函数值

x=fminsearch(f,[1;2;3]) %用单纯法求解,从点[1,2,3]开始搜索

4、数值积分: 数值积分函数

quad('fname',a,b,tol,trace,p1,p2,…) 低阶法--自适应递推辛普生法 quadl('fname',a,b,tol,trace,p1,p2,…) 高阶法--自适应递推牛顿-柯西法 trapz(x,y) 计算变量为x,函数y的积分 fname是被积函数表达式字符串或函数文件名,a,b分别为积分的上、下限,tol用来控制积分精度,默认为tol=0.001,trace 取1,表示用图形表示积分结果,取0则无图形,p1,p2…是向函数传递的参数,可取默认值。 例1:用trapz在区间-1<x<3上计算y=humps(x)下面的面积 x=-1:0.12:3;

y=humps(x);% Y = HUMPS(X) is a function with strong maxima near

% x = .3 and x = .9.

plot(x,y)

area=trapz(x,y) %以更好的精度计算 x=-1:0.02:3; y=humps(x); area=trapz(x,y)

%用quad和quadl在区间-1<x<3上计算y=humps(x)下面的面积 format long

area=quad('humps',-1,3) area=quadl('humps',-1,3)

5. 数值微分与梯度 (Difference and approximate derivative ,gradient). 数值微分函数diff(x) 例1:按列求微分。

x=[1,10,20;2,12,23;3,14,26;3,16,29] d=diff(x) figure(1) plot(x) figure(2)

plot(d) %求一阶微分 x =

1 10 20 2 12 23 3 14 26 3 16 29 d =

1 2 3 1 2 3

0 2 3

数值微分比较困难,函数的微小变化都容易产生相邻斜率的大的变化。因而应尽可能避免数值微分。特别是对实验数据进行微分的时候,最好用最小二次曲线拟合数据,然后对所得的多项式进行微分。

x=0:0.1:1;

y=[-0.443 1.783 3.231 6.21 7.08 7.73 7.98 8.99 9.23 9.93 10.99 ]; n=2;

p=polyfit(x,y,n) %多项式拟合 xi=linspace(0,1,100); z=polyval(p,xi); figure(1) hold on

plot(x,y,'o',x,y,xi,z,':') xlabel('x') ylabel('y')

title('二次曲线拟合')

pd=polyder(p) %多项式微分 w=polyval(pd,xi); plot(xi,w,'*') xlabel('x') ylabel('y')

title('曲线微分')

%函数diff是对一些描述某函数的数据进行粗略计算的微 %分函数,用于计算数组中元素间的差分。f=f(x) 的微分近 %似为 dy/dx=f(x+h)-f(x)/(x+h)-x dy=diff(y)./diff(x); xd=x(1:max(size(x))-1); figure(2) plot(xd,dy)

title('用diff进行近似差分') ylabel('dy/dx轴')

xlabel('x轴')

结果可见用有限差分近似微分导致很差的结果。 例2: 对于(u=x2+y2和Δ2=4)求5点差分。

[x,y]=meshgrid(-4:4,-3:3) u=x.^2+y.^2

v4=4*del2(u) %求m×n阶矩阵U的五点差分矩阵 u =

25 18 13 10 9 10 13 18 25 20 13 8 5 4 5 8 13 20 17 10 5 2 1 2 5 10 17 16 9 4 1 0 1 4 9 16 17 10 5 2 1 2 5 10 17 20 13 8 5 4 5 8 13 20 25 18 13 10 9 10 13 18 25 v4 =

4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

MESHGRID X and Y arrays for 3-D plots.

[X,Y] = MESHGRID(x,y) transforms the domain specified by vectors

x and y into arrays X and Y that can be used for the evaluation of functions of two variables and 3-D surface plots.

The rows of the output array X are copies of the vector x and the columns of the output array Y are copies of the vector y.

[X,Y] = MESHGRID(x) is an abbreviation for [X,Y] = MESHGRID(x,x).

[X,Y,Z] = MESHGRID(x,y,z) produces 3-D arrays that can be used to

evaluate functions of three variables and 3-D volumetric plots.

DEL2 Discrete Laplacian.

L = DEL2(U) when U is a matrix, is an discrete approximation of

0.25*del^2 u = (d^2u/dx^2 + d^2/dy^2)/4. The matrix L is the same size as U with each element equal to the difference between an element of U and the average of its four neighbors.

L = DEL2(U) when U is an N-D array, returns an approximation of

(del^2 u)/2/n where n is ndims(u).

L = DEL2(U,H), where H is a scalar, uses H as the spacing between

points in each direction (H=1 by default).

L = DEL2(U,HX,HY) when U is 2-D, uses the spacing specified by HX

and HY. If HX is a scalar, it gives the spacing between points in the x-direction. If HX is a vector, it must be of length SIZE(U,2) and specifies the x-coordinates of the points. Similarly, if HY is a scalar, it gives the spacing between points in the

y-direction. If HY is a vector, it must be of length SIZE(U,1) and specifies the y-coordinates of the points.

L = DEL2(U,HX,HY,HZ,...) when U is N-D, uses the spacing given by

HX, HY, HZ, etc.

例3:产生一个二元函数偏导数和梯度。

x=-2:0.2:2; y=-2:0.2:2;

[xx,yy]=meshgrid(x,y); z=xx.*exp(-xx.^2-yy.^2);

[Gx,Gy]=gradient(z,0.2,0.2); %Gx,Gy分别是二元函数的偏导 contour(x,y,z,'k'),hold on,

quiver(xx,yy,Gx,Gy,'r'),hold off


第四章 MATLAB的数值计算功能(2)(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:机关卫生检查评比办法

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: