Why in the name of god and little green apples do you need to write your own code?
NEVER write your own code, when code written by professionals is available. This is especially true when you will write poor code.
ezsurf(h,[-3,3])
Rotate the plot around, and it will be obvious the maximum lies near (1,1). It will also be clear there will be multiple solutions. As well, it looks like there might be two solutions with the same maximum, mirror images across the origin.
fminsearch(@(xy) -h(xy(1),xy(2)),[1 1])
ans =
1.062 0.61742
Or, you could have used the optimization toolbox, which will probably give you a few more correct digits for the time required.
format long g
fminunc(@(xy) -h(xy(1),xy(2)),[1 1])
Local minimum found.
Optimization completed because the size of the gradient is less than
the default value of the optimality tolerance.
<stopping criteria details>
ans =
1.06206524800884 0.617438162285868
Next, is it true that the two solutions are the mirrored across the origin?
[xy1,fval] = fminsearch(@(xy) -h(xy(1),xy(2)),[-1 -1])
xy1 =
-1.06203291314256 -0.61741902622501
fval =
-0.604829999447586
[xy1,fval] = fminsearch(@(xy) -h(xy(1),xy(2)),[1 1])
xy1 =
1.06203291314256 0.61741902622501
fval =
-0.604829999447586
Looks like it.
Or, you could use the symbolic TB.
syms x y
H = ((x.^3.*y) + ((5*x.^2).*(y.^2)))./(exp(1).^((x.^2) + 3*y.^4));
Hgrad = gradient(H);
xysol = solve(Hgrad,x,y)
xysol =
struct with fields:
x: [13×1 sym]
y: [13×1 sym]
So 13 solutions, many of which are complex. Which one givex the max?
vpa([xysol.x,xysol.y,h(xysol.x,xysol.y)],8)
ans =
[ 0.90244248, -0.66672465, 0.32319386]
[ -1.0620653, -0.61743817, 0.60483]
[ 1.4133748, -0.14100149, -0.027034788]
[ -1.4133748, 0.14100149, -0.027034788]
[ 1.0620653, 0.61743817, 0.60483]
[ -0.90244248, 0.66672465, 0.32319386]
[ 1.0188998 + 0.076070383i, - 0.025239699 - 0.63521774i, - 0.44731804 - 0.14437493i]
[ 1.0188998 - 0.076070383i, - 0.025239699 + 0.63521774i, - 0.44731804 + 0.14437493i]
[ 10.20644i, -2.0509926i, 1.5918468e23]
[ -10.20644i, 2.0509926i, 1.5918468e23]
[ - 1.0188998 + 0.076070383i, 0.025239699 - 0.63521774i, - 0.44731804 + 0.14437493i]
[ - 1.0188998 - 0.076070383i, 0.025239699 + 0.63521774i, - 0.44731804 - 0.14437493i]
[ 0, 0, 0]
Of the real solutions found, it looks like the two we found before are it.