1
$\begingroup$

Here's a similar question as the the preceding question with the same title, but this time with vectors, which I need to use. I followed the suggestion in the answer the the previous question, but it yields the same error. I just don't understand why functions in Mathematica behave the way they do.

    aVec = {a[1], a[2]};  
    bVec = {b[1]};  
    y[aVec_, bVec_] = bVec[[1]]*x^2 - aVec[[1]] - aVec[[2]]  
    g[aVec_, bVec_] := x /. FindRoot[y[aVec, bVec] == 0, {x, 1}]

    g[{1, 2}, {1}]

During evaluation of In[5]:= FindRoot::nlnum: The function value {-1. a[1.]-1. a[2.]+1. b[1.]} is not a list of numbers with dimensions {1} at {x} = {1.}. >>

During evaluation of In[5]:= ReplaceAll::reps: {FindRoot[y[{1,2},{1}]==0,{x,1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

(* x /. FindRoot[y[{1, 2}, {1}] == 0, {x, 1}] *)
$\endgroup$
3
  • 1
    $\begingroup$ Please use standard formatting, along the lines of my editing of your first question. Thanks. $\endgroup$
    – bbgodfrey
    Jan 22, 2015 at 1:39
  • $\begingroup$ How do you do the formatting? What are the commands? $\endgroup$
    – Dan
    Jan 22, 2015 at 2:25
  • $\begingroup$ Whenever you post a Question or an Answer, you will see a string of icons just above the box for typing. Hover over each to see its meaning, or click the ? at the right, followed by advanced help. The same is true for editing. Also, be sure to read Tour under help at the top of the page. $\endgroup$
    – bbgodfrey
    Jan 22, 2015 at 2:38

1 Answer 1

1
$\begingroup$

Try this instead:

y[aVec_, bVec_] := bVec[[1]]*x^2 - aVec[[1]] - aVec[[2]]
g[aVec_, bVec_] := x /. FindRoot[y[aVec, bVec] == 0, {x, 1}]

g[{1, 2}, {1}]

1.73205

By using Set to define y instead of SetDelayed the function ignored the two parameters and used the already-defined aVec and bVec, which were non-numerical and led to the error.

That is, your definition evaluated to this:

y[_, _] = b[1]*x^2 - a[1] - a[2]
$\endgroup$
0

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.