1
$\begingroup$
F[y_?NumericQ] := Eigenvectors@( {
     {1, y},
     {y, 1}
    } );
G[x_?NumericQ] := NIntegrate[F[y][[1]].F[y][[1]], {y, 0, 2}];
G[4]

The program simply doesn't work in this way. Changing y_?NumericQ to y_ doesn't help since then the Eigenvectors would be evaluated in symbolic method (which I don't want). How can it be solved?

$\endgroup$
2
  • 2
    $\begingroup$ Your function for G[x] does not have x on the right hand side. I doubt this was intended. $\endgroup$
    – dan7geo
    Oct 17, 2016 at 4:35
  • $\begingroup$ The point is the following: Sometimes, F is a complicated function that ends with a list of, say, 3 numbers (and that ?NumericQ is a must for some function (say function involving ordering)). Then G evaluate the integral of that function. But the mathematica simply doesn't work for ?NumericQ trick when your function ends with several numbers, so you must do it separately and ends with multiple evaluation times :P $\endgroup$
    – Ting Fung
    Oct 25, 2016 at 4:44

1 Answer 1

1
$\begingroup$

Adding an intermediate function works:

F[y_?NumericQ] := Eigenvectors@({{1, y}, {y, 1}});
H[y_?NumericQ] := F[y][[1]].F[y][[1]];
G[x_?NumericQ] := NIntegrate[H[y], {y, 0, 2}]; 
G[4]
(*2*)

The problem with your previous code was that NIntegrate tried to evaluate its argument symbolically first. So it tried evaluating:

F[y][[1]].F[y][[1]]
(*y.y*)

This returns y.y because F[y] had no definitions when y is a symbol. So F[y][[1]] evaluates to y (which is the first part of the expression F[y])

(By the way, you should avoid starting your function names with capital letters)

$\endgroup$
3
  • $\begingroup$ Just a side note, now ?NumericQ is no longer needed in F :) $\endgroup$
    – xzczd
    Oct 17, 2016 at 6:49
  • $\begingroup$ That function isn't really needed :) H[y_?NumericQ] := #.# &@Eigenvectors[{{1, y}, {y, 1}}][[1]] $\endgroup$
    – dan7geo
    Oct 17, 2016 at 17:08
  • $\begingroup$ ic, thanks for the answer~ $\endgroup$
    – Ting Fung
    Oct 25, 2016 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.