0
$\begingroup$

I'm new to mathematica I want to find the positive maximum value of a function, but so far I always ended up with either a negative value or the value doesn't even show up.

Clear["Global`*"]
Z = 500;
W = 100000;
G = 250;
H = 100;
K = 0.5;
T = 30;
L = 4000;
P = 5;
S = 2.5;
Y = 1;
A = 0.1;
V = 2.5;
J = 8000;
f[x_] := 1/
   x {(J*Z*x*(2*Y - x))/(
     2*Y) - ((W + T*G) + ((L + T*P)*2*Z*Y*(1 - ((Y - x)/Y)^1.5))/
       3 + (H + T*S + A*L)*((
         2*Z*Y*2*Y - 2*Z*Y*2*Y*((Y - x)/Y)^2.5 - 
          2*Z*Y*5*x*((Y - x)/Y)^1.5)/15))};
Plot[f[x], {x, -2, 2}]
FindMaximum[f[x], x] 
$\endgroup$
3
  • $\begingroup$ In the definition of f you should replace { and } by normal parentheses ( and ). Then beware that FindMaximum makes a local max search, and usually you should only use this with a starting point such as FindMaximum[f[x],{x,0.2}] where here 0.2 would be your starting point. There is also NMaximize which attempts a global max search. $\endgroup$
    – user293787
    Jul 14, 2022 at 8:43
  • $\begingroup$ The maximum value of a function, if there is a positive one, will be positive automatically simply because a positive value is greater than a negative one. So just find the max and see if it's positive. If you mean you want to find the maximum value of $f(x)$ for $x>0$, please edit the question to state more precisely what you want. $\endgroup$
    – Michael E2
    Jul 14, 2022 at 13:17

3 Answers 3

3
$\begingroup$

Since f[x] is complex value, we use Re@f[x] to restrict on reals.

Clear[f];
f[x_] = 1/
    x ((J*Z*x*(2*Y - x))/(2*
        Y) - ((W + T*G) + ((L + T*P)*2*Z*Y*(1 - ((Y - x)/Y)^1.5))/
        3 + (H + T*S + 
          A*L)*((2*Z*Y*2*Y - 2*Z*Y*2*Y*((Y - x)/Y)^2.5 - 
            2*Z*Y*5*x*((Y - x)/Y)^1.5)/15)));
Plot[Re@f[x], {x, -2, 2}]
FindMaximum[{Re@f[x], x > 0}, x]

{1.09904*10^6, {x -> 0.263896}}

$\endgroup$
4
  • $\begingroup$ Or f[x_]:=Rationalize[ 1/ x ((J*Z*x*(2*Y - x))/(2* Y) - ((W + T*G) + ((L + T*P)*2*Z*Y*(1 - ((Y - x)/Y)^1.5))/ 3 + (H + T*S + A*L)*((2*Z*Y*2*Y - 2*Z*Y*2*Y*((Y - x)/Y)^2.5 - 2*Z*Y*5*x*((Y - x)/Y)^1.5)/15))),0] $\endgroup$
    – user64494
    Jul 14, 2022 at 8:53
  • $\begingroup$ When i tried it in my mathematica why the result is like this -->During evaluation of In[791]:= FindMaximum::nrnum: The function value {-432503.} is not a real number at {x} = {0.999994}. During evaluation of In[791]:= FindMaximum::grad: Evaluation of the gradient of function Experimental`NumericalFunction[{Hold[-{Re[(-107500+Times[<<2>>]+Times[<<3>>]+Times[<<2>>])/x]}],Block},{0,{{{},1,0,Hold[x],0,0}}},<<3>>,{None,None,None}] failed at {1.}. Out[807]= FindMaximum[{Re[f[x]], x > 0}, x] $\endgroup$ Jul 14, 2022 at 9:13
  • 1
    $\begingroup$ @ShafaKeysaRinjaniHananta Please Clear[f]. $\endgroup$
    – cvgmt
    Jul 14, 2022 at 9:14
  • $\begingroup$ Ah i see, thank you so much! It's working now $\endgroup$ Jul 14, 2022 at 9:22
3
$\begingroup$
$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

Z = 500;
W = 100000;
G = 250;
H = 100;
K = 0.5;
T = 30;
L = 4000;
P = 5;
S = 2.5;
Y = 1;
A = 0.1;
V = 2.5;
J = 8000;

Do not use list brackets in place of parentheses. See The Four Kinds of Bracketing in the Wolfram Language

f[x_] := 1/
    x ((J*Z*x*(2*Y - x))/(2*
        Y) - ((W + T*G) + ((L + T*P)*2*Z*Y*(1 - ((Y - x)/Y)^1.5))/
        3 + (H + T*S + 
          A*L)*((2*Z*Y*2*Y - 2*Z*Y*2*Y*((Y - x)/Y)^2.5 - 
            2*Z*Y*5*x*((Y - x)/Y)^1.5)/15)));

The function is real when

FunctionDomain[f[x], x]

(* x < 0 || 0 < x <= 1 *)

Use the domain of interest as a constraint

{max, arg} = Maximize[{f[x], 0 < x <= 1}, x]

(* {1.09904*10^6, {x -> 0.263896}} *)

Plot[f[x], {x, 0, 1},
 Epilog -> {Red, AbsolutePointSize[4],
   Point[{x, f[x]} /. arg]}]

enter image description here

$\endgroup$
2
$\begingroup$

Another approach is as follows.

f[x_] := 1/x ((J*Z*x*(2*Y - x))/(2*
     Y) - ((W + T*G) + ((L + T*P)*2*Z*Y*(1 - ((Y - x)/Y)^1.5))/
     3 + (H + T*S +  A*L)*((2*Z*Y*2*Y - 2*Z*Y*2*Y*((Y - x)/Y)^2.5 - 
     2*Z*Y*5*x*((Y - x)/Y)^1.5)/15)));
NMaximize[{f[x], x > 0 && x < 1}, x]

{1.09904*10^6, {x -> 0.263896}}

$\endgroup$

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.