Message Boards Message Boards

0
|
5207 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Perform gradient descend for the function of two variables?

Posted 5 years ago

Hello. I need to perform Gradient Descent in VBA. Before that I wanna do the same thing in mathematica. So that I've tried to use this code. I've already made it for one variable and it works well. But for two it dosent.

    ...
Do[
     cx = nx;
     cy = ny;
     Subscript[p, 0] = cx;
     Subscript[p, 1] = cy;
     nx = cx - alpha*dx;
     (*Print[nx];*)
     ny = cy - alpha*dy;
     i += 1;
     (*Print[ny];*)
     step = nx - cx;
     tmp = {nx, ny};
     If[ Abs[step] <= precision &&  Abs[step] > 0, Break[]], 500]
    i
    Subscript[p, 0] = tmp[[1]]
    Subscript[p, 1] = tmp[[2]]
    z = Y/100*z

it dosent find a correct solution. I've found partial derivatives in the file. Please, take a look at the attachment for the whole code. Are there other effective numerical methods for this function to make a code the problem in VB/VBA? So I cant use any optimization libraries there. And I need to write a complete code of that stuff. To do that I need to make it in mathematica first.

Attachments:
POSTED BY: Alex Graham
3 Replies
Posted 5 years ago

Well. I've found the problem. it is that dx, dy dosent change. I've coded in mathematica z(p0) problem which works in vba excel and gives correct result. and have the same problem in mathematica. When I start mathematica for the first time(after closing it) dx changes. For the second, third Shift + Enter....... it stays constant. I use Woflram Mathematica 12.0. What is that? Bug of the while loop? Please, take a look at my file. what's wrong with that. EDIT 1 fixed it by clear[subscript], but seems for two variables dosent find a correct minimum.

EDIT 2. Solved the problem. Using fullsimplify distort the result of dx dy

POSTED BY: Alex Graham
Posted 5 years ago

Hi Alex,

Not sure why you have to implement it in Mathematica first. A functional Mathematica implementation is not going to be easy to translate to VB/VBA. It has been about 20 years since I wrote any VB/VBA code and I really don't want to re-live that experience :-).

A Google search revealed this, which might help you with the VB implementation.

POSTED BY: Rohit Namjoshi
Posted 5 years ago

Thank you for your reply, I don't think that it would be super hard to solve this problem in vba. It only demands some while loop. I've already solved f(p0) problem, but can't solve f(p0,p1). Maybe it needs different alphas for p0,p1. but as far as know gradient descend method, alpha should be the same for both variables. So I need to minimize with gradient descend method exactly in mathematica. To be solved it there I can solve it everywhere. So I need some help with Gradient decent there.

POSTED BY: Alex Graham
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract