Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7088 > unrolled thread
| Started by | "Doug Tinkham" <dtinkham@live.ca> |
|---|---|
| First post | 2011-12-10 12:30 +0000 |
| Last post | 2011-12-11 09:01 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
NMinimize problem: fct minimized uses FindRoot "Doug Tinkham" <dtinkham@live.ca> - 2011-12-10 12:30 +0000
Re: NMinimize problem: fct minimized uses FindRoot "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-12-11 09:01 +0000
| From | "Doug Tinkham" <dtinkham@live.ca> |
|---|---|
| Date | 2011-12-10 12:30 +0000 |
| Subject | NMinimize problem: fct minimized uses FindRoot |
| Message-ID | <jbvjcm$it3$1@smc.vnet.net> |
Hello
I'm using NMinimize and FindMinimum to minimize a function that uses
FindRoot when calculating it's value. The problem is that the equation
that FindRoot is used on uses the variable that is being optimized, and
Mathematica appears to be forcing the variable that is being optimized
to remain symbolic in the FindRoot call, and this leads to recursion and
a recursion limit error.
Rather than post my actual functions that are quite long, I've reduced
my problem to the code below that shows my issue. As you will see,
FindRoot keeps optvar in symbolic form when executing FindRoot. Is there
a way to force Mathematica to use all numerical calculations using
NMinimize or FindMinimum? Is the issue with calculation of the
gradient, which Mathematica wants to do symbolically?
Many thanks.
MyNumFct[optvar_] := Module[{inteq, n},
inteq[x_] := (Sin[x] + 1/2*Cos[x])/optvar;
n = n /. FindRoot[inteq[n], {n, 0.1}];
Return[n + Sin[optvar]];
]
NMinimize[{MyNumFct[var], 0 <= var <= 6}, {var, 4.1}]
[toc] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2011-12-11 09:01 +0000 |
| Message-ID | <jc1rhb$t5v$1@smc.vnet.net> |
| In reply to | #7088 |
On Sat, 10 Dec 2011 12:30:14 -0000, Doug Tinkham <dtinkham@live.ca> wrote:
> Hello
>
> I'm using NMinimize and FindMinimum to minimize a function that uses
> FindRoot when calculating it's value. The problem is that the equation
> that FindRoot is used on uses the variable that is being optimized, and
> Mathematica appears to be forcing the variable that is being optimized
> to remain symbolic in the FindRoot call, and this leads to recursion and
> a recursion limit error.
>
> Rather than post my actual functions that are quite long, I've reduced
> my problem to the code below that shows my issue. As you will see,
> FindRoot keeps optvar in symbolic form when executing FindRoot. Is there
> a way to force Mathematica to use all numerical calculations using
> NMinimize or FindMinimum? Is the issue with calculation of the
> gradient, which Mathematica wants to do symbolically?
>
> Many thanks.
>
>
>
> MyNumFct[optvar_] := Module[{inteq, n},
> inteq[x_] := (Sin[x] + 1/2*Cos[x])/optvar;
> n = n /. FindRoot[inteq[n], {n, 0.1}];
> Return[n + Sin[optvar]];
> ]
> NMinimize[{MyNumFct[var], 0 <= var <= 6}, {var, 4.1}]
>
An easy fix: just don't allow MyNumFct to evaluate for symbolic arguments.
Define it with a condition that the argument has to be numeric:
MyNumFct[optvar_?NumericQ] := Module[{inteq, n},
inteq[x_] := (Sin[x] + 1/2*Cos[x])/optvar;
n = n /. FindRoot[inteq[n], {n, 0.1}];
Return[n + Sin[optvar]];
]
Your syntax for NMinimize is also erroneous; if you want to specify
initial values, you have to specify a starting region (with upper and
lower bounds), not just a starting point:
NMinimize[{MyNumFct[var], 0 <= var <= 6}, {var, 4, 5}]
giving:
{-1.46365, {var -> 4.71239}}
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web