1
$\begingroup$

Let me define the following function:

f[x_] = x

I have to solve the numerical integral:

NIntegrate[NIntegrate[f[x], {x, 0, y}], {y, 0, 1}] (*1*)

and obviously this gives me an error.

I know there is a correct way to do it and the its form is:

NIntegrate[f[x], {y, 0, 1}, {x, 0, y}]

but for several reasons I need to evaluate the integral in the form (1). I cannot use the 'Integrate' built-in function for the symbolic intgeral beacuse the function I have to integrate is very complicated and the time to solve it symbolically is huge.

Is there a way to bypass the error that appears in (1) ?

$\endgroup$
3
$\begingroup$

You could define a second function with the inner integral and give the restriction that it is only evaluated if the argument is numeric. E.g.:

f[x_] = x;
f1[y_?NumericQ] := NIntegrate[f[x], {x, 0, y}]
NIntegrate[f1[y], {y, 0, 1}]
(* 0.166667 *)
$\endgroup$
2
  • $\begingroup$ Thanks, I need a little more... Is there a way to define, for example, the variable 'L[1]' as a blank variable like 'f[L[1]_]:=L[1]^2'? $\endgroup$ Jan 14 at 11:01
  • $\begingroup$ That will not work. MMA interprets this as L[1] times underscore. However this is superfluous. In "f[x_]", "x_" means any expression and your "L[1]_" would mean the same thing. $\endgroup$ Jan 14 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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