FindMaximum function in Mathematica

In summary, the FindMaximum function in Mathematica is a built-in function used for finding the maximum value of an expression or function numerically. It can handle multivariate functions and requires the function or expression to be maximized, along with any necessary constraints or initial values. It differs from NMaximize in that it only searches for local maxima and has less control over the optimization process. However, it may not work well with certain types of functions and may not provide exact solutions due to its reliance on numerical methods.
  • #1
kevmac
6
2
I recently plotted a piecewise function:

Plot[Piecewise[{{1 - Exp[-.002*t],
0 <= t < 120}, {-Exp[-.002*t] + Exp[-.002*(t - 120)],
120 <= t}}], {t, 0, 5000}, PlotRange -> {0, 0.25}]

I then defined the function which I am calling q[t_] as follows:

q[t_] := Piecewise[{{1 - Exp[-.002*t],
0 <= t < 120}, {-Exp[-.002*t] + Exp[-.002*(t - 120)],
120 <= t}}];

I then wish to find the maximum value of this function, which should occur at t=120. I entered:

FindMaximum[q, t]

But received the following error:

FindMaximum::nrnum: "The function value -q is not a real number at {t} = {1.`}"

Any easy way to fix this? Am I using the FindMaximum function incorrectly, or did I do something wrong when initially defining the function?
 
Physics news on Phys.org
  • #2
Also, the following question asks that we find when this function is equal to 10^-3. My professor suggested using the FindRoot function, however I can't see how that is applicable to anything not equal to zero. Any tips on how to apply that or some other function to a nonzero value of y?
 
  • #3
kevmac said:
FindMaximum[q, t]
That should be FindMaximum[q[t], t]

kevmac said:
Also, the following question asks that we find when this function is equal to 10^-3. My professor suggested using the FindRoot function, however I can't see how that is applicable to anything not equal to zero. Any tips on how to apply that or some other function to a nonzero value of y?
You have to build a function ##f(t)## that will be equal to 0 when ##q(t) = 10^{-3}##.
 
  • #4
Thanks for fixing my error for the FindMaximum function. Included q[t] and it worked fine.

In terms of building a function f(t), I did this a few ways, but keep receiving the same error. First, I started with the same piecewise function q(t) as defined previously, and subtracted 10^-3 from it. Not sure if that would work, I took only the second leg of the piecewise function, and subtracted 10^-3 from that (we are only looking for the second root in this case). Both times, I entered it as such and received the following error:

(a)
f[t_] := Piecewise[{{1 - Exp[-.002*t], 0 <= t < 120}, {-Exp[-.002*t] + Exp[-.002*(t - 120)], 120 <= t}}] - 10^(-3);
FindRoot[f[t], {t,120}]
FindRoot::cvmit: Failed to converge to the requested accuracy or precision within 100 iterations
{t -> 50120.}

(b)
f[t_] := -Exp[-.002*t] + Exp[-.002*(t - 120)] - 10^(-3)
FindRoot[f[t], {t, 120}]
FindRoot::cvmit: Failed to converge to the requested accuracy or precision within 100 iterations.
{t -> 50120.}

Once again, am I entering this incorrectly? The correct output should be ~2801.5
 
  • #5
Please ignore my last response, I figured it out.

Rather than define a new function f, I used my same function q and entered:

FindRoot[q[t] - 10^(-3), {t, 120}]

And found my output. Thanks for your feedback!
 
  • Like
Likes DrClaude

1. What is the FindMaximum function in Mathematica?

The FindMaximum function in Mathematica is a built-in function that helps find the maximum value of an expression or function numerically. It uses a combination of local and global optimization algorithms to search for the maximum value.

2. How do I use the FindMaximum function in Mathematica?

To use the FindMaximum function, you need to provide the function or expression you want to maximize, along with any necessary constraints or initial values. For example, FindMaximum[f[x], {x, x0}] will search for the maximum value of f[x] starting from the initial value x0.

3. Can the FindMaximum function handle multivariate functions?

Yes, the FindMaximum function can handle multivariate functions. You can specify multiple variables and their initial values in the second argument of the function, such as FindMaximum[f[x,y], {x,x0, y,y0}].

4. What is the difference between FindMaximum and NMaximize?

The main difference between FindMaximum and NMaximize is that FindMaximum only searches for a local maximum, while NMaximize can search for both local and global maxima. NMaximize also allows for more control over the optimization process, such as specifying the method or convergence criteria.

5. Are there any limitations to using the FindMaximum function?

While the FindMaximum function is powerful and versatile, there are some limitations to be aware of. It may not work well with highly discontinuous functions or functions with many local maxima. It also relies on numerical methods, so it may not provide exact solutions for all functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
270
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
228
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top