Showing results for 
Search instead for 
Did you mean: 

help with downhill optimization

Go to solution

Hi all,


I am trying to solve this one equation with one unknown using optimization.  It's a non-linear equation and I was searching around for something that works like excel's solver.  The downhill optimization seems close to what I want to do, but it is not optimizing.  Would anyone mind looking at this code and try to determine what I'm doing wrong?


FYI, I've checked the answer by putting 0.02 as the X in my function and it is giving the right value.  The function is set up to be like:  Calc(X) - Actual = 0.  The "=0" portion should be implied (maybe this is what is wrong?)







Download All
0 Kudos
Message 1 of 6

The problem seemed to be the initial value of your function and the type of your function. If you try to plot it, you will notice the discontinuity around 21.049 as shown below:


If you start from the left (like 21) then the minimization function will only be able to converge. If you start on the right (like 22), then you will find the X = 21.049 as minimum.


Here is the code for it:



Hope this helps

Barp - Control, Simulation, RTT and HIL - National Instruments
Message 2 of 6
Accepted by topic author Matt-UHart

I just noticed that... If you are trying to get the Func - Measured, then you are trying to detect zero crossing of it instead of minimum. In this case, then you have to make the f(x) present the minimum when crossing zero. You can use the minimization function but you have to change f(x) to be abs(f(x)). Then, finding the minimum means detecting where the function crosses zero.


If this also doesn't help, maybe it would be good to have your original problem posed and, if possible, which function you were trying to use in Excel.

Barp - Control, Simulation, RTT and HIL - National Instruments
Message 3 of 6

ahh thank you!  I will try the absolute value when I get in the office on Monday.  In the meantime, I coded a simple Newton Rahpson method to work with my function that seems to do the job as well.


Thanks again!


0 Kudos
Message 4 of 6

oh also I do not expect solutions above 21%... in fact, all answers should be greater than zero but less than 5%.  You must be correct that I need to do the absolute value and give it a better first approximation.




0 Kudos
Message 5 of 6

Thank you!  The absolute value did the trick!


Thanks again!


0 Kudos
Message 6 of 6