1

I am using the following code to determine the maximum of a function:

f[x_] = Log[3/10, 3 - 2 x - x^2]
xr = FunctionDomain[f[x], x]
Maximize[{f[x], xr}, x]

As the maximum value does not exist, a warning message is returned:

Maximize::natt: The maximum is not attained at any point satisfying the given constraints.

I want to modify the code such that it doesn't give a warning message, but prints

The function does not have a maximum value in this interval

when the maximum does not exist, and give the maximum otherwise.

Domen
  • 23,608
  • 1
  • 27
  • 45
csn899
  • 3,953
  • 6
  • 13
  • 1
    {Limit[f[x], x -> -3], Limit[f[x], x -> -1]} – cvgmt Aug 14 '23 at 13:43
  • Maybe this would help: https://reference.wolfram.com/language/ref/frontendobject/RemoveFromEvaluationQueue.html – Michael E2 Aug 14 '23 at 13:44
  • @cvgmt There is a big problem with this, as the maximum or minimum value of a function may not necessarily be obtained at the endpoint of the interval – csn899 Aug 14 '23 at 13:47
  • 1
    Once again, please put more effort in understanding the code you've found/obtained, don't blindly use them, at least check the document of the functions you don't know. Check already appears in following questions of yours: https://mathematica.stackexchange.com/q/282329/1871 https://mathematica.stackexchange.com/q/284016/1871 https://mathematica.stackexchange.com/q/282327/1871 https://mathematica.stackexchange.com/q/282362/1871. – xzczd Aug 15 '23 at 00:26

1 Answers1

3

Using Check:

Clear["Global`*"]
f[x_] = Log[3/10, 3 - 2 x - x^2]
xr = FunctionDomain[f[x], x]
FunctionRange[f[x], x, y]
Quiet@Check[Maximize[{f[x], xr}, x], 
  "The function does not have a maximum value over this interval", 
  Maximize::natt]

You can also specify a list of error messages.

Syed
  • 52,495
  • 4
  • 30
  • 85
  • 1
    Clear["Global`*"] f[x_] = Log[3/10, 3 + 2 x + x^2] xr = FunctionDomain[f[x], x] FunctionRange[f[x], x, y] Quiet@Check[Maximize[{f[x], xr}, x], "The function does not have a maximum value over this interval", Maximize::natt] Quiet@Check[Minimize[{f[x], xr}, x], "The function does not have a minimum value over this interval", Minimize::natt] Plot[f[x], {x, -5, 5}] – csn899 Aug 14 '23 at 14:16