Message Boards Message Boards

0
|
6530 Views
|
12 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Some hard numerical integrations Specific example provided

Posted 9 years ago

Hi everyone, Let's suppose that there is a function, R(x), with variable x, and R(x)= ? P(x,t) dt. P(x,t) is a function of x and t, but it is a very complicated integral, so no analytic solution is available. Another function is H(x, y)= h(y)* ? R(x)*f(x) dx. Here, f(x) and h(y) are functions of x and y respectively. R(x) is calculated before. I need to plot the relation between H(x, y) and y. The bounds of both integrals are known. Does anyone know how to do it numerically in Mathematica? I hope that I make my question clear enough. Thanks for any help that you can provide!! Cheers, Stven

POSTED BY: Stven Maths
12 Replies
Posted 9 years ago

Thanks everyone for the informative reply. Very appreciated!!!

POSTED BY: Stven Maths

Begin by using ?NumericQ with NIntegrate. See (http://support.wolfram.com/kb/12502)

r[x_?NumericQ] := NIntegrate[p[x,t], {t,a,b}]

Where a and b are the limits of the numerical Integration.

POSTED BY: Sean Clarke

If you're looking for something maybe more efficient and more clever, you can turn R(x)= ? P(x,t) dt from an integral equation into a differential equation.

Now that it's a differential equation you can use NDSolve or ParametricNDSolve:

https://reference.wolfram.com/language/ref/NDSolve.html

https://reference.wolfram.com/language/ref/ParametricNDSolve.html

POSTED BY: Sean Clarke
Posted 9 years ago

Hi Sean,

First, thanks for your awesome reply. It seems to be a promising way. The only thing that worries me is that my equation is slightly different than the examples in NDSolve and ParametricNDSolve. I can convert my equation into a differential equation, but my differential equation is in the form of R'(x)= P(x,t), {t, t0, t1}. I think this is different from the form that can be calculated by NDSolve and PatametricNDSolve.

Regards,

Stven

POSTED BY: Updating Name

Use NIntegrate to compute r[x] (here I use Mathematica notation and avoid capitalizing user-defined function names). Compute the integral of r[x]*f[x] also with NIntegrate (possibly it should just be done as a double integral). At that point you are pretty much done.

I should remark that the subject header seems to be incorrect; I have not found a specific example anywhere in this.

POSTED BY: Daniel Lichtblau
Posted 9 years ago

Hi Daniel,

I apologize for the inappropriate subject header. Thanks for the reply!

Best regards,

Stven

POSTED BY: Stven Maths

Hi Stven,

you say you need the relation between H(x,y) and y. But H does not depend on x, because x is only a dummy variable of integration. Then you simply have H(y) = h(y)*const, with: $$\mbox{const} := \int\mbox{d}x \; f(x) \int \mbox{d}t \; P(x,t)$$ Regards -- Henrik

POSTED BY: Henrik Schachner
Posted 9 years ago

Hi Henrik,

Thanks a lot for the reply. Sorry if I didn't make my question clear. I think that my key point is how to compute R(x)= ? P(x,t) dt with known bounds numerically? As P(x,t) it too complicated with x and t correlated, there is no way to find an analytic solution. It will be greatly appreciated if you can provide me some helps on coding this. Thanks again.

Best regards,

Stven

POSTED BY: Stven Maths
Posted 9 years ago

Hi Stven,

The only thing I can think of is a rather brute-force and decidedly unelegant method: 1. Make a "sufficiently dense" (*) grid of points {x0, y0} in the (x,y}-plane. 2. Calculate your function H(x,y) on this grid. (3. This step is optional: Perhaps wrap an Interpolation command over the H(x,y} so as to get smooth connections between the grid points.) 4. Choose your fixed point x0 and plot H(x0, y) versus y.

This is how I would try it. Almost certainly there are more efficient methods, but this is simple and I don't see why it wouldn't work.

(*) Footnote re point 1: The "sufficiently dense" is the stumbling block of course. If you know next to nothing about H(x,y), you'll have to proceed by trial and error. The more you know about H, the smarter you can choose your grid: make the grid sparse in regions where H doesn't change much and dense in regions with dramatic gradients.

Hope this works for you.

René Samson

POSTED BY: Rene Samson
Posted 9 years ago

Hi Rene, Thanks a million for your reply. I am very new to Mathematica, also I have never used any other maths software like Matlab before. Would you mind to write down some relevant codes for this calculation? I will write down the steps that I don't know how to code. It will be greatly appreciated if you can provide any help.

-----Let's suppose that there is a function, R(x), with variable x, and R(x)= ? P(x,t) dt. P(x,t) is a function of x and t-----

How to define P as a function of x and t? Will mathematica be able to compute this integral given a small region like {t, t+0.1)

----- Make a "sufficiently dense" () grid of points {x0, y0} in the (x,y}-plane----*

How to define a grid? Can you show me the code for it? Will the grid in Mathematica be able to store an equation?

When I successfully calculate the integral for each interval, {t, t+0.1}, how do I sum it to get R(x)? I may have a million equations stored in the grid and the sum of them, which gives me R(x), will be horrendous.

Thanks again for any help if you can provide!!

Best regards,

Stven

POSTED BY: Stven Maths
Posted 9 years ago

Stven,

Point 1: In reply to your question how to make a grid in Mathematica I have attached a simple Notebook example for you. The most important ingredient is the Table-function. That is THE way for making vectors or matrices or objects of even higher dimensionalities than matrices. If you've never used the Table-function before, I highly recommend you spend some time reading up on it in the Wolfram documentation. I find the Table-function an indispensible tool. I couldn't live a day without it (Mathematica-wise, that is).

In the attached Notebook I show you the following example. I make a grid in the (x,z)-plane. x goes from 0 to 2 and I take linear steps in the x-direction of 0.1. The z-coordinate is a bit more interesting. I wanted uneven spacings in the z-direction. Z goes from 1 to 2.8 but the spacing gets wider as z gets larger.

In the Notebook you'll see that I created an object called "gridff". If you pay good attention to the output you'll notice that there are multiple levels of brackets (like {...,...} ). In many situations this might turn out to be a pain in the butt. (Mathematica can be very picky sometimes). Usually (at least how I use these things mostly) you want all the (x,z) pairs inside the Table to be treated on an equal footing. If that is your aim, you have to add one additional line of code and that is the Flatten-command. (Again, if you have no idea what that is, do read up on it in the documentation. It is a most useful thing to know about.) After you "flatten" the original grid, you're left with only two levels of hierarchy within the Table: like this: { { }, { }, { }, { },.......{ } }. It is also always very useful to know how many XZ-pairs you generated. That's what the Length-command tells you.

Point 2: You ask: Will the grid in Mathematica be able to store an equation? The answer is: once you have a grid, you can then use it to define a function on each grid point that you generated. I also illustrate how to do this in the Notebook. I define (external to the Table) a simple function f(x,z) = x^2+z^2. Then I show inside a new Table command how one-by-one I extract every (x,z)-pair in grid, calculate f for that pair and add it as a third member to the list {x,z,f}. I hope the principle is clear.

Point 3: You ask how to define functions in Mathematica. There are many ways of doing this, but the way that is shown in my Notebook is one of the most common ways. Once you have defined your function you should be able to integrate it over the domain on which it is defined, using e.g. the function NIntegrate (the prefix N indicates that it is a numerical integration procedure). I have done that for the simple example in my note book; scroll down in the Notebook to find the command and the result.

I hope this is useful.

Good luck, René

Attachments:
POSTED BY: Rene Samson
Posted 9 years ago

Hi Rene, Thanks so much for you very detailed reply!! Very appreciated. I am really busy recently, so I will follow your steps to do the calculation late today. Hope I won't get stuck. Thanks again! Best regards, Stven

POSTED BY: Stven Maths
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