Message Boards Message Boards

GROUPS:

Plotting a numerical integral?

Posted 4 months ago
676 Views
|
4 Replies
|
3 Total Likes
|

Hello,

I am trying to solve a numerical integral that is a function of two other numerical integrals and subsequently plot the result as a function of a parameter. When I try this, I do not get an explicit error, but the kernel quits and the plot does not appear. Any help would be greatly appreciated!

Also, moderators please note: this is not a duplicate of my previous question on this forum. My previous question pertains to the "Solve" function and this question pertains to a plotting issue. So please do not remove this post!

Thank you,

Alex

Attachments:
POSTED BY: Alex L
Answer
4 Replies

Hello Alex, You will tend to get a quicker response if you post a bit of code instead of a notebook.

In any case, I took a quick look at your code. You had values of g inside the inner Numerical integrate. You need to make that explicit, with something like B[T,g] instead of B[T]. If it inside a numerical function, then it is better to do this:

B[T_,?NumericQ, g_?NumericQ]:= B[J_, g_] :=
 (2/(1 + nIntegrate[ Exp[-B[J]]*(b*h/G*(1 - c*g) + b*(1 - h/G)*(1 - c*g)*g + d*x/K), {J, T, 505 - T}]))

I put a dummy function in there "nIntegrate" so you can make sure that the integrand evaluates to a numeric quantify for values of J. If it does, replace the nIntegrate with NIntegrate.

Also, note that capital K is a symbol already defined in the WL.

Your plot will take a long time as you have to do nested numerical integrals.

Posted 4 months ago

Hello Craig, thank you for your response! I tried the code you suggested but it is giving me the error seen in the attached notebook. Based on what you said about making g explicit, I tried this code:

B[J_?NumericQ, g_?NumericQ] := 
 NIntegrate[
  b*(1 - c*g)*H/G + b*g*(1 - c*g)*(1 - H/G) - d*X/K, {t, J, 500}]

h = Exp[-q*J]*G

x = (b E^((b h J)/G) h K)/(-d G + d E^((b h J)/G) G + b h)

B[T_?NumericQ, 
  g_?NumericQ] := (2/(1 + 
     NIntegrate[
      Exp[-B[J, g]]*(b*h/G*(1 - c*g) + b*(1 - h/G)*(1 - c*g)*g + 
         d*x/K), {J, T, 500}]))

w = Exp[-q*T]*G

W = (b E^((b w T)/G) w K)/(-d G + d E^((b w T)/G) G + b w)

Plot[NIntegrate[W*B[T, g], {T, 0, 330}], {g, 0, 1}]

But this code again results in the Kernel automatically quitting. Any further help would be greatly appreciated but I completely understand if it can not be given!

Alex

Attachments:
POSTED BY: Alex L
Answer

Hello Alex, Here is a working version. Some parts of your numerical integral could be done symbolically which speeds up the computation considerably. You will want to check my steps: I didn't do a proof read.

I find it easier to put only one or two expressions in a cell--that way you can take a look at intermediate results easier.

I recommend not using upper case letters for your own symbols. I redefined K as k.

Because you are working with Exps of Exps, rescaling your problem may make things better behaved. In any case, something does plot in the attached.

Attachments:
Posted 4 months ago

Thank you so much!! I just learned so much from this.

Alex

POSTED BY: Alex L
Answer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract