TransWikia.com

How to write a program on gradients and the path for steepest ascent?

Mathematica Asked by imarben007 on January 17, 2021

I’m given two functions

f[x ,y ]=Cos[x]*Cos[y]/(6+x^2+y^2)

and

df[x ,y ]=Grad[F[x,y],{x,y}]

I was given a note that
F[1.0,2.0] and dF[1.0,2.0] both evaluate but when g={1.0,2.0} then neither F[g] nor dF[g] evaluate properly and the solution to that is using the command Apply like this: Apply[F,g] or Apply[dF,g].

I need to modify this set of code (and if it doesn’t work for some reason I’ll go back and make sure I didn’t miss anything)

F[x_, y_] := Sin[x]*Sin[y]

F[x_] := Sin[x[[1]]]*Sin[x[[2]]]
F[{0.1, 0.2}]
dF[x_] := {Cos[x[[1]]]*Sin[x[[2]]], Sin[x[[1]]]*Cos[x[[2]]]}
dF[{0.1, 0.2}]
x = {-1.4, 1.3}
X = {x};
threshold = 0.001;
h = 0.1;
While[Sqrt[dF[x].dF[x]] > threshold,
 x = x + h*dF[x] ;
 X = Append[X, x]
 ]
Print[x]
Print[dF[x]]
X

and make a module argMax[f ,df ,z ]:=Module[{} ]
where f is the function to maximize, df is the gradient of this function,
and z is the initial point from which to begin a steepest ascent algorithm.
This function should implement the steepest ascent algorithm as developed
above and it should return the terminal point of this process. In other
words, it should return the approximation of the point at which f attains its
maximum. The step size should be h=0.1, and the process should terminate
when the norm of the gradient is the threshold value of 0.001. Additionally,
the argMax function should define a global variable X which is a list of the
points generated in the steepest ascent algorithm.

I’ve tried messing with this in a lot of different ways but I can’t figure out how to piece it together.

It may help to have the early steps of the assignment so I’ll post it all below, assuming it’s right. One of my struggles is that the example code for the gradient is different from the gradient function I’m using and I don’t know how to manipulate my version.

f[x_, y_] := Cos[x]*Cos[y]/(6 + x^2 + y^2)

df[x_, y_] := Grad[f[x, y], {x, y}]

f[1.0, 2.0]
df[1.0, 2.0]

g = {1.0, 2.0}
f[g]
df[g]


Apply[f, g]
Apply[df, g]


Plot3D[f[x, y], {x, -4, 4}, {y, -4, 4}, Evaluated -> True]

A = ContourPlot[f[x, y], {x, -4, 4}, {y, -4, 4}]

B = StreamPlot[{df[x, y]}, {x, -4, 4}, {y, -4, 4}, Evaluated -> True, 
  StreamStyle -> Black]

Show[A, B]
```

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP