Re: Find max of a Find root function
- To: mathgroup at smc.vnet.net
- Subject: [mg113761] Re: Find max of a Find root function
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Thu, 11 Nov 2010 06:09:16 -0500 (EST)
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:
=============
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:image002.png at 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.}}