MATLAB练习例子八(函数处理)
返回
function y = humps(x)
y = 1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2 + 0.04) - 6;
》 fh = @humps;
??? fh = @
|
変数、または関数が見つかりません
》 f = inline('1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2 + 0.04) - 6');
》 f(2.0)
ans =
-4.8552
》 f = inline('y*sin(x)+x*cos(y)','x','y')
f =
Inline function:
f(x,y) = y*sin(x)+x*cos(y)
》 f(pi,2*pi)
ans =
3.1416
》 f = inline('1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2 + 0.04) - 6');
》 fplot(f,[-5 5])
》 grid on
》 fplot(inline('2*sin(x+3)'),[-1 1])
》 type humps.m
function y = humps(x)
y = 1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2 + 0.04) - 6;
》 fplot('humps',[-5 5])
》 fh = 'humps';
》 feval(fh,2.0)
ans =
-4.8552
》
》 fplot(inline('[2*sin(x+3), humps(x)]'),[-5 5])
求函数的最小参数值
》 x = fminbnd('humps',0.3,1)
Optimization terminated successfully:
the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-004
x =
0.6370
》 x = fminbnd('humps',0.3,1,optimset('Display','iter'))
Func-count x f(x) Procedure
1 0.567376 12.9098 initial
2 0.732624 13.7746 golden
3 0.465248 25.1714 golden
4 0.644416 11.2693 parabolic
5 0.6413 11.2583 parabolic
6 0.637618 11.2529 parabolic
7 0.636985 11.2528 parabolic
8 0.637019 11.2528 parabolic
9 0.637052 11.2528 parabolic
Optimization terminated successfully:
the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-004
x =
0.6370
function b = three_var(v)
x = v(1);
y = v(2);
z = v(3);
b = x.^2 + 2.5*sin(y) - z^2*x^2*y^2;
》 v = [-0.6 -1.2 0.135];
》 a = fminsearch('three_var',v)
Optimization terminated successfully:
the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-004
and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-004
a =
0.0000 -1.5708 0.1803
找零点
》 a = fzero('humps',-0.2)
Zero found in the interval: [-0.10949, -0.264].
a =
-0.1316
》 humps(a)
ans =
8.8818e-016
》 humps(1)
ans =
16
》 humps(-1)
ans =
-5.1378
》 options = optimset('Display','iter');
》 a = fzero('humps',[-1 1],options)
Func-count x f(x) Procedure
1 -1 -5.13779 initial
2 1 16 initial
3 -0.513876 -4.02235 interpolation
4 0.243062 71.6382 bisection
5 -0.473635 -3.83767 interpolation
6 -0.115287 0.414441 bisection
7 -0.150214 -0.423446 interpolation
8 -0.132562 -0.0226907 interpolation
9 -0.131666 -0.0011492 interpolation
10 -0.131618 1.88371e-007 interpolation
11 -0.131618 -2.7935e-011 interpolation
12 -0.131618 8.88178e-016 interpolation
13 -0.131618 -9.76996e-015 interpolation
Zero found in the interval: [-1, 1].
a =
-0.1316
数值积分
》 q = quad('humps',0,1)
q =
29.8583
》 t = 0:0.1:3*pi;
》 plot3(sin(2*t),cos(t),t)
function f = hcurve(t)
f = sqrt(4*cos(2*t).^2 + sin(t).^2 + 1);
》 len = quad('hcurve',0,3*pi)
len =
17.2220
function out = integrnd(x,y)
out = y*sin(t) + x*cos(y);
》 xmin = pi;
》 xmax = 2*pi;
》 ymin = 0;
》 ymax = pi;
》 result = dblquad('integrnd',xmin,xmax,ymin,ymax)
??? 't' は未定義の関数、または変数です.
function out = integrnd(x,y)
out = y*sin(x) + x*cos(y);
》 result = dblquad('integrnd',xmin,xmax,ymin,ymax)
result =
-9.8698
返回