Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find max of a Find root function

247 views
Skip to first unread message

Van Peursem, Dan

unread,
Nov 10, 2010, 6:29:04 AM11/10/10
to
Can someone please assist me with the following sample problem. Bottom line is that I'd like to use the FindRoot function to numerically solve an equation, that involves a parameter. I'd then like to use the Findmax function that would yield the largest root for a specified range of the parameter. Any help would be appreciated. Thanks

Dan
I first define the function
f[a_?NumberQ]:=x/.Last[FindRoot[xSCos[a x],{x,.4}]]

and it evaluates nicely.

f[3]
0.39004

It even graphs nicely.

Plot[f[x],{x,2,3}]
[cid:image0...@01CB8019.CA736910]

But it doesn't like me asking for a max...


FindMaximum[{f[a],2=A3a=A33},{a,3}]

The errors I get are the following

FindRoot::nlnum : The function value {1. -1. Cos[1. a]} is not a list of numbers with dimensions {1} at {x} = {1.}. ?
ReplaceAll::reps : {FindRoot[xSCos[a x],{x,1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. ?
FindRoot::nlnum : The function value {1. -1. Cos[1. a]} is not a list of numbers with dimensions {1} at {x} = {1.}. ?
ReplaceAll::reps : {FindRoot[xSCos[a x],{x,1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. ?
FindRoot::nlnum : The function value {1. -1. Cos[1. a]} is not a list of numbers with dimensions {1} at {x} = {1.}. ?
General::stop : Further output of FindRoot::nlnum will be suppressed during this calculation. ?
ReplaceAll::reps : {FindRoot[xSCos[a x],{x,1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. ?
General::stop : Further output of ReplaceAll::reps will be suppressed during this calculation. ?
{-0.887727,{a=AE3.}}

Bob Hanlon

unread,
Nov 11, 2010, 6:09:12 AM11/11/10
to

Your e-mail appears to have been garbled, but everything works after fixing that. Start with a fresh kernel.

f[a_?NumberQ] := x /.
Last[FindRoot[x == Cos[a x],
{x, .4}]]

f[3]

0.39004

Plot[f[x], {x, 2, 3}]

FindMaximum[{f[a], 2 <= a <= 3}, {a, 3}]

{0.514933, {a -> 2.}}

FindMaximum[{f[a], 2 <= a <= 3}, a]

{0.514933, {a -> 2.}}

NMaximize[{f[a], 2 <= a <= 3}, a]

{0.514933, {a -> 2.}}

I recommend that you use NumericQ rather than NumberQ

NumberQ /@ {Pi, E, GoldenRatio}

{False, False, False}

NumericQ /@ {Pi, E, GoldenRatio}

{True, True, True}

Bob Hanlon

---- "Van Peursem wrote:

=============

0 new messages