1
$\begingroup$

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.

$\endgroup$
4

1 Answer 1

3
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ 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}] $\endgroup$
    – csn899
    Commented Aug 14, 2023 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.