Matlab Numerical Integration - Syntax/Style Query

In summary: Insert your name here]In summary, the conversation discusses using MATLAB for solving a mathematical problem involving numerical integration with wide bounds. The approach taken by the person posting the question was to use the quad command, which yielded the expected answers but was considered crude. The responder suggests that while numerical integration is a valid approach, it may not be the most efficient or accurate method. They also suggest a more elegant approach using a defined integrand function and the quad function. This would make the code more modular and easier to modify in the future.
  • #1
pete j
1
0
Hi,

Pretext: I have no formal background in MATLAB or maths in general, so apologies if any of the following doesn't make sense. I am also new to this forum, so apologies if this post is incorrect in any way. [n.b. I also posted this thread on mathhelpforum.com]

Context: Ok, so I wanted to find the response value for various parameter values, using the following equation:

http://www.nitric.net/~pete/pictures/pj.integral.GIF

[If you can't see the image, it is an integration with infinite bounds over an equation consisting of a Gaussian probability density function multiplied by the the square of a Gaussian cumulative density function plus one minus the square of another Gaussian cumulative density function]

I wasn't sure whether it was possible to do this using the MATLAB symbolic toolkit (syms), so I thought I'd take a crack at it using numerical integration, using the quad command. After much effort and confusion, I ended up with the following code.

Code:
mu=0;
sigma=1;
s=0;
alpha=0.4;
 
mu=sprintf('%f', mu);
sigma=sprintf('%f', sigma);
s=sprintf('%f', s);
alpha=sprintf('%f', alpha);
 
 
f=strcat('normpdf(x+',s,',',mu,',sqrt(2)*',sigma,').*(normcdf(x,',mu,',sqrt(2)*',alpha,'*',sigma,').^2 + (1 - normcdf(x,',mu,',sqrt(2)*',alpha,'*',sigma,')).^2)');

quad(f,-1000,1000)

This code *DOES* appear to work (i.e. yields the expected answers). But it strikes me as being more than a little crude. My questions therefore are as follows:

  1. From a mathematical point of view, was using numerical integration with suitably wide bounds the right way to go about this problem?
  2. From a programming point of view, is there a more elegant way to execute the above numerical integration? (i.e. a better approach than wrapping everything up as one big string which is then passed to the quad function)

Many thanks for your time,

pete
 
Last edited by a moderator:
Physics news on Phys.org
  • #2


Hi Pete,

Thanks for posting your question on the forum. It's great to see someone with a non-math background interested in using MATLAB for their work. I'll try my best to address your questions.

Firstly, from a mathematical perspective, numerical integration with wide bounds is a valid approach for solving this type of problem. However, it may not be the most efficient or accurate method. Since you are integrating over an infinite range, you will need to choose a large enough range to ensure that your solution is accurate. This can be computationally expensive and may not be feasible for more complex equations. In such cases, it may be better to use symbolic integration or other specialized numerical integration techniques.

Secondly, from a programming perspective, there are definitely more elegant ways to execute the numerical integration in MATLAB. One approach would be to define your integrand as a function and then use the quad function to integrate it. This would involve breaking down your equation into smaller, more manageable parts and then combining them in the integrand function. This would also make your code more modular and easier to debug and modify in the future.

Here's an example of how you could implement this approach:

% Define your parameters
mu = 0;
sigma = 1;
s = 0;
alpha = 0.4;

% Define your integrand function
function y = my_integrand(x)
y = normpdf(x+s,mu,sqrt(2)*sigma).*(normcdf(x,mu,sqrt(2)*alpha*sigma).^2 + (1 - normcdf(x,mu,sqrt(2)*alpha*sigma)).^2);
end

% Use the quad function to integrate over a wide range
quad(@my_integrand, -1000, 1000)

I hope this helps. Let me know if you have any further questions or need clarification on anything.
 
  • #3


Hello Pete,

From a mathematical point of view, using numerical integration with wide bounds is a valid approach for solving this problem. However, there may be other methods such as using a change of variables or applying a numerical integration algorithm specifically designed for Gaussian functions that could potentially yield more accurate results.

From a programming point of view, there may be more elegant ways to execute the numerical integration. Instead of creating a long string, you could break the equation into smaller parts and use the quad function on each part separately, then combine the results. This may be more efficient and easier to read in the code. Additionally, you could also consider using the integral function in MATLAB, which can handle symbolic expressions and may provide a more straightforward solution.

I hope this helps. Keep exploring and learning, and don't hesitate to ask for help when needed. Best of luck!
 

1. What is the syntax for performing numerical integration in Matlab?

The syntax for performing numerical integration in Matlab is:
integral(fun,xmin,xmax)
where "fun" is the function to be integrated and "xmin" and "xmax" are the lower and upper limits of integration, respectively.

2. How do I specify a custom integrand function in Matlab?

To specify a custom integrand function in Matlab, you can use the "fun" argument in the integral function. For example:
integral(@(x) x.^2,0,1)
This will integrate the function "x^2" from 0 to 1.

3. Can I perform multiple integrations at once in Matlab?

Yes, you can perform multiple integrations at once in Matlab by using nested integral functions. For example:
integral(@(x) integral(@(y) x*y,0,1),0,1)
This will integrate the function "x*y" first with respect to y from 0 to 1, and then with respect to x from 0 to 1.

4. Is there a way to specify the integration method in Matlab?

Yes, you can specify the integration method in Matlab by using the "method" argument in the integral function. Some available methods include "quad" (quadrature methods), "trapz" (trapezoidal rule), and "simpson" (Simpson's rule).

5. How can I improve the speed and accuracy of my numerical integrations in Matlab?

To improve the speed and accuracy of your numerical integrations in Matlab, you can adjust the tolerance and/or use adaptive methods. For example, you can use the "RelTol" and "AbsTol" arguments in the integral function to specify the relative and absolute tolerances for the integration. Additionally, you can use adaptive methods such as "quadgk" (Gauss-Kronrod quadrature) to automatically adjust the integration step size for better accuracy.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
742
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
993
  • MATLAB, Maple, Mathematica, LaTeX
Replies
27
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top