FindMaximum function in Mathematica

Click For Summary

Discussion Overview

The discussion revolves around the use of the FindMaximum and FindRoot functions in Mathematica for a piecewise function defined by the user. Participants explore issues related to function definitions, error messages, and methods for finding maximum values and roots of the function.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant describes a piecewise function and attempts to use FindMaximum to find its maximum value, encountering an error related to the function definition.
  • Another participant suggests that the correct syntax for FindMaximum should include the function call q[t] instead of just q.
  • There is a discussion about using FindRoot to determine when the function equals 10^-3, with concerns raised about applying it to nonzero values.
  • A participant shares their attempts to define a new function f(t) to find roots, but encounters convergence errors with their definitions.
  • One participant later resolves their issue by using the original function q and successfully finds the root using FindRoot.

Areas of Agreement / Disagreement

Participants generally agree on the need to correctly define functions for Mathematica commands, but there are differing approaches on how to apply FindRoot and the specifics of function definitions. The discussion includes both successful and unsuccessful attempts, indicating unresolved issues for some participants.

Contextual Notes

Some participants express uncertainty regarding the convergence of FindRoot and the appropriate method for defining functions to achieve desired outputs. There are also indications of missing assumptions in the function definitions that may affect the results.

Who May Find This Useful

Users of Mathematica, particularly those working with piecewise functions and seeking to understand the application of FindMaximum and FindRoot functions.

kevmac
Messages
6
Reaction score
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
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?
 
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}##.
 
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
 
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   Reactions: DrClaude

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K