0
$\begingroup$

I am trying to do some calculations on very long and difficult equations. Ultimately, I want to create a function to NIntegrate my result, but I want to set it as SetDelay (to be evaluated after placing the variables).

The problem can be showed at this simple example:

A = {{a, b}, {c, d}};

EVals = Eigenvalues[A];

EV1 = EVals[[1]];
EV2 = EVals[[2]];

DEV1 = D[EV1, a];
DEV2 = D[EV2, a];

F[b_, c_, d_] := NIntegrate[DEV1 + DEV2, {a, 0, 1}];

But after evaluation, e.g., F[2,3,4], I get the symbolic expression.

I know, that in this example I can just copy-paste DEV1 and DEV2 directly to the F, but I am working with more difficult formulas, where I cannot do this.

$\endgroup$
6
  • $\begingroup$ The error message is NIntegrate::itraw: Raw object 1 cannot be used as an iterator.. As you see in your code, you set a to 1, and then you want to integrate over this, e.g. $\int_0^1 (DEV1+DEV2) \; \rm d 1$. This does not make much sense, right? :) So what do you want to do with this a? $\endgroup$
    – Domen
    Commented Mar 11 at 10:43
  • $\begingroup$ @Domen, yes you are right, but if I change to F[b_,c_,d_] := NIntegrate[DEV1 + DEV2, {a, 0, 1}] it still doesn't work. :( I corrected my question. $\endgroup$
    – blahblah
    Commented Mar 11 at 10:46
  • $\begingroup$ Off-topic, but: the answer will be 1 irrespective of the parameters $(b,c,d)$. $\endgroup$
    – Roman
    Commented Mar 11 at 10:55
  • $\begingroup$ @xzczd, the question is with the same problem, but the answer doesn't work in my case $\endgroup$
    – blahblah
    Commented Mar 11 at 12:00
  • $\begingroup$ Why do you think so? How do you apply the methods therein? $\endgroup$
    – xzczd
    Commented Mar 11 at 12:01

1 Answer 1

0
$\begingroup$

Due to ,localization, your parameters b,c,d are not the same variables as those in DEV1 nd DEV2 and therefore get not replaced. You can see this e.g. by:

tmp = {b, c, d};
F[b_, c_, d_] := Print[tmp];
F[2, 3, 4]

{b,c,d}

However, we can replace them explicitly, like e.g.;

tmp = {b, c, d};
F[b0_, c0_, d0_] := Print[tmp /. {b -> b0, c -> c0, d -> d0}];
F[2, 3, 4]

{2,3,4}

This applied to your example:

F[b0_, c0_, d0_] := 
  NIntegrate[DEV1 + DEV2 /. {b -> b0, c -> c0, d -> d0} , {a, 0, 1}];
F[2, 3, 4]

1.
$\endgroup$

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