Message Boards Message Boards

Passing NDSolve through NIntegrate?

Posted 3 years ago

Hello everyone,

I was wondering: is it possible to solve a differential equation over a certain domain using NDSolve and pass that solution through NIntegrate? Any suggestions would be greatly appreciated!

Alex

POSTED BY: Alex L
7 Replies
Posted 3 years ago

Hello again,

I was told I should not ask this question via a new discussion, so I was wondering: do you know how I could avoid receiving an error in the attached code and how I might make the code run faster? I am trying to pass one NIntegrate solution through another and subsequently pass that resulting function through NIntegrate. The code is not only slow but also gives me an answer that I think is wrong.

Alex

Attachments:
POSTED BY: Alex L
Posted 3 years ago

WOW I really did not think this would be possible. Thank you SO MUCH!!

POSTED BY: Alex L
Posted 3 years ago

Hi Alex,

Thanks for changing the filename this time. The code in testt.nb 16-06-35-484.nb works fine for me. Make sure you start from a clean kernel. The problem is with the plotting because NIntegrate will be called hundreds of times. You can speed up the integration like this

B[u_?NumericQ] := 
 NIntegrate[(1 - c*g)*h[t]/K + g*(1 - c*g)*(1 - h[t]/K) - (h[t])/K, {t, A, u}, 
  Method -> {Automatic, "SymbolicProcessing" -> 0}]

Then this is pretty fast

Plot[Exp[B[u]], {u, 0, 250}]

enter image description here

Looks like the integral starts diverging around x = 225. Please verify that the change to B to speed it up is not impacting accuracy.

POSTED BY: Rohit Namjoshi
Posted 3 years ago

I updated it again; I am so sorry for the confusion! I am trying to solve h'[t] numerically using NDSolve, plug the resulting h[t] values into b(1-cg)h[t]/K + b(1-cg)g(1 - h[t]/K) - dh[t]/K and numerically integrate the resulting curve. Is this possible?

Alex

POSTED BY: Alex L
Posted 3 years ago

OK, I evaluated the updated notebook and there are no errors and there is no NIntegrate. So, I am confused. What are you trying to integrate?

POSTED BY: Rohit Namjoshi
Posted 3 years ago

Sorry about that! I updated the notebook.

POSTED BY: Alex L
Posted 3 years ago

Some problems with the code

h is a list, not a function. Need

h = h /. slv // First

And in the definition of B, h needs to be h[t].

What is g? It is used in the definition of B but is not defined. Easy to spot since it is colored blue.

POSTED BY: Rohit Namjoshi
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