1
$\begingroup$

I have function f[x,y] where i need to integrate f[x,y] with x first and then later i need to integrate that f[x,y]dx with y.
I will be getting f[x,y]dx from another notebook after using Get["ffile"] function.
Is there a way to Integrate this function separately. I tried Direct double integration it worked properly.I'm able to get plot but unable to integrate separately .

ClearAll[Evaluate[StringJoin[
Context[], "*"]]]
Needs["Utilities`CleanSlate`"]; 
CleanSlate[]; 
ClearInOut[];  

f[x_, (y_)?NumericQ] := 
   Sin[x*y]*Cos[x + y];  

g[y_] := NIntegrate[f[x, y], 
   {x, 0, Pi}]  

avggy = NIntegrate[g[y], {y, 0, Pi}]  

Plot[g[y], {y, 0, 2*Pi}]

I'm getting this error
"The integrand f[x,y] has evaluated to non-numerical values for all \ sampling points in the region with boundaries".
Is there a way to Integrate this? any suggests are highly appreciated.
Thanking you.

I'm Sorry for editing after getting response to above doubt.

This is what i'm trying to do

f123[(y_)?NumericQ, 
   (z_)?NumericQ] := 
  NIntegrate[
   Sin[x*y]*Cos[x + y] + z, 
   {x, 0, Pi}]  

Save["ffucntion", 
  testFfunction]

I will call this function from another notebook

ClearAll[Evaluate[StringJoin[
Context[], "*"]]]
Needs[
   "Utilities`CleanSlate`"]; 
CleanSlate[]; 
ClearInOut[];  

Get["ffucntion"]; 
g[(y_)?NumericQ] := 
  f123[y, y^2]  

avgg = NIntegrate[g[y], 
   {y, 0, 2*Pi}]

I'm getting this error ."The integrand g[y] has evaluated to non-numerical values for all \ sampling points in the region with boundaries"

$\endgroup$
4
$\begingroup$

EDIT 1:

Looking at the code from your edit, I think the problem is that you are saving the wrong function (testFfunction instead of f123). Try:

Save["ffucntion", f123]

in your first notebook. And then run

Get["ffucntion"]; 
g[(y_)?NumericQ] := f123[y, y^2]  

avgg = NIntegrate[g[y], {y, 0, 2*Pi}]

in your other notebook. It seems to work for me without any errors and yields 257.566.

Original:

Your ?NumericQ is in the wrong spot.

f[x_, y_] := Sin[x*y]*Cos[x + y];

g[y_?NumericQ] := NIntegrate[f[x, y], {x, 0, Pi}]

avggy = NIntegrate[g[y], {y, 0, Pi}]

Plot[g[y], {y, 0, 2*Pi}]

Plot of integrated function.

We want to prevent NIntegrate from doing any symbolic manipulation, so we check to make sure that g[y] only evaluates once y has taken on a numeric value.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Hi, thanks for the solution. I'm extremely sorry for asking again. I edited my code . Would you help me out. Thanking you. $\endgroup$ – Gummala Navneeth Dec 3 '19 at 7:53
  • 1
    $\begingroup$ @GummalaNavneeth I've edited my answer. Hopefully this will work for you. $\endgroup$ – MassDefect Dec 3 '19 at 19:59
  • $\begingroup$ Thanks a lot for your help $\endgroup$ – Gummala Navneeth Dec 4 '19 at 3:16

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.