(* 二変数関数 f の停留点を求める (よう努力する) *)
teiryuuten[f_]:=
Module[
{fx,fy},
fx=Simplify[D[f[x,y],x]];
fy=Simplify[D[f[x,y],y]];
Solve[{fx==0,fy==0},{x,y}]
]
(* 二変数関数 f とその停留点のリスト s を分析し、極値の判定をする *)
bunseki[s_,f_]:=
Module[
{ff,HesseXY,aSolution,restSolutions,valf,l1,l2},
ff=f[x,y];
HesseXY = {{D[ff,x,x],D[ff,x,y]},
{D[ff,y,x],D[ff,y,y]}};
restSolutions = s;
While [(restSolutions != {}),
aSolution = First[restSolutions];
restSolutions = Rest[restSolutions];
valf = ff /. aSolution;
{l1,l2} = Eigenvalues[HesseXY /. aSolution];
If [l1 > 0 && l2 > 0,
Print[aSolution, ", 極小 f(x,y)=", valf]];
If [l1 < 0 && l2 < 0,
Print[aSolution, ", 極大 f(x,y)=", valf]];
If [(l1 l2 < 0),
Print[aSolution, ", 極値でない"]];
If [(l1 l2 == 0),
Print[aSolution, ", 極値であるかどうか分からない。"]];
]
]
|
| 使用例 |
oyabun% math
Mathematica 4.0 for Solaris
Copyright 1988-1999 Wolfram Research, Inc.
-- Motif graphics initialized --
In[1]:= << /home/syori2/kyokuchi.m
In[2]:= f[x_,y_]:=x y(x^2+y^2-4)
In[3]:= s=teiryuuten[f]
Out[3]= {{x -> -2, y -> 0}, {x -> -1, y -> -1}, {x -> -1, y -> 1},
> {x -> 0, y -> 0}, {x -> 1, y -> -1}, {x -> 1, y -> 1}, {x -> 2, y -> 0},
> {y -> -2, x -> 0}, {y -> 2, x -> 0}}
In[4]:= bunseki[s,f]
{x -> -2, y -> 0}, 極値でない
{x -> -1, y -> -1}, 極小 f(x,y)=-2
{x -> -1, y -> 1}, 極大 f(x,y)=2
{x -> 0, y -> 0}, 極値でない
{x -> 1, y -> -1}, 極大 f(x,y)=2
{x -> 1, y -> 1}, 極小 f(x,y)=-2
{x -> 2, y -> 0}, 極値でない
{y -> -2, x -> 0}, 極値でない
{y -> 2, x -> 0}, 極値でない
In[5]:=
|