I am trying to make a function that works as follows: Integrate 1 for all t in the interval $ [0,1] $ such that the first element of function vstar is positive. vstar is a function that outputs a pair of real numbers. Unfortunately the test vstar[t, a1, a2, f1, f2, σ]][[1]] > 0 does not work as expected (this is waht I've tried):

D1[f1_, a1_, f2_, a2_,σ_] := NIntegrate [If[vstar[t, a1, a2, f1, f2,σ][[1]] > 0, 1, 0] f[t], {t, 0, tup}, AccuracyGoal -> 8];

The way it is, Mathematica tests if the first slot of vstar is greater than zero (that is $ t>0 $) rather than testing if the first part of the output of vstar is greater than zero. vstar is defined so that it evaluates only for numerical values. I think I need to evaluate the function vstar before the test is performed, I just don't know how to do it - could someone explain how?

p.s. For further reference, here's the code of vstar:

vstar[tt_?NumericQ, aa1_?NumericQ, aa2_?NumericQ, 
  ff1_?NumericQ, ff2_?NumericQ, σσ_?NumericQ] := 
  list = {objm, objs1, objs2}  /. {t -> tt, 
     a1 -> aa1, a2 -> aa2, f1 -> ff1, 
     f2 -> ff2, σ -> σσ}; 
  If[list[[1]] < 0 && list[[2]] < 0 && list[[3]] < 0, {0, 0},
   Which[Ordering[list , -1] == {1}, {v1, v2} /. vm, 
     Ordering[list , -1] == {2}, {v1 /. v1s, 0}, 
     Ordering[list, -1] == {3}, {0, 
      v2 /. v2s}]  /. {t ->tt, a1 -> aa1, 
     a2 -> aa2, f1 -> ff1, f2 -> ff2, σ -> σσ}]]
  • $\begingroup$ I edited your question but your code still has issues so I can't really help much more. At first sight, there are wrong square brackets inserted in your If clause so I suggest you edit the code to amend this (I have not touched your code other than formatting) and also provide the function vstar. May I also suggest you read the relevant section on formatting for future posts. $\endgroup$
    – gpap
    Sep 17, 2013 at 11:26
  • $\begingroup$ @gpap Thanks, I spotted the typo and fixed it. $\endgroup$ Sep 17, 2013 at 11:37
  • 1
    $\begingroup$ Ok, so, this isn't much help now - it depends on other bits of code. The idea is to have a self-containted, minimally working example for people to test, without having to see part sof your code that are irrelevant to the problem. So, if the output of vstar always gives out a pair {a,b} where, say, 0<a,b<1, it will be easier to write vstar[args__]:=RandomReal[{0,1},{2,1}]. Also, the page about formatting that I linked tells you that a code block is formatted easily by selecting the code a pressing ctrl + K. I have done that now, but you should be able to do it yourself in the future $\endgroup$
    – gpap
    Sep 17, 2013 at 12:04
  • $\begingroup$ Possible duplicate: Prevent Part[] from trying to extract parts of symbolic expressions $\endgroup$
    – Mr.Wizard
    Sep 18, 2013 at 21:02

1 Answer 1


What happens now since vstar only evaluates on numerical arguments Part sees the unevaluated form and extracts the first argument since it doesn't know any better:

vstar[t, a1, a2, f1, f2, s][[1]]
(* t *) 

To get around this you can make a function that extracts the first part only on numerical input:

numericFirst[l_?(VectorQ[#, NumericQ] &)] := l[[1]]

numericFirst[vstar[t, a1, a2, f1, f2, s]]
(* vstar[t, a1, a2, f1, f2, s] *)

(* 0.1 *) 

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.