Matlab: Numerical integration of a multivariable symbolic function

In summary, RebekahFTR is trying to numerically integrate a symbolic function of three variables with a single variable, but is having difficulty. She has tried several different approaches, but all have had problems. She has also created a bare-bones example that might be useful for image processing.
  • #1
rsc42
6
0
I have a symbolic function of three variables which I'm trying to numerically integrate wrt a single variable. Consider (syms x y a) and the function f(x,y,z). Here are some things I've tried, without success:

1. >>int(f(x,y,z),x,a,b)
which analytically integrates f wrt x from a to b but, with 3+ hours runtime, Matlab hasn't been able to solve it.

2. >>double(int(f(x,y,z),x,a,b))
but this requires that Matlab first try to solve it analytically before solving it numerically. And Matlab thinks it can solve it analytically so it never gets around to a numerical solution.

3. Variations on >>quad(inline(f),a,b)
but quad only accepts single variable functions. quad2d, dblquad and higher order quads can handle multivariable functions but only if you're integrating over all variables.

4. I've also tried expanding individual within my integral so as to soften it up for int(f(x,y,z)...) but with no luck since this requires I limit the region within which the resulting expression is valid.

I'd appreciate any help you could give me. Thanks!

Rebekah
 
Physics news on Phys.org
  • #2
FTR, I did figure out a very simple way to do it but at the cost of keeping y and z as symbolics. I created nested for-loops which let's me calculate the integral with quad for each pair of y and z values I'm interested in. This happens to work for me now but if anyone comes up with a better solution, I'd love to hear it.

A bare-bones example looks like:

for y = yi : increment : yf ;
for z = zi : increment : zf ;
I = quad( inline( f(x,y,z) ), a , b );
end
end
 
  • #3
I have code that computes:
[tex]\int_{a}^{b}\int_{c}^{d}f(x,y)dxdy[/tex]
if you're interested.
 
  • #4
I'm interested! Do you have it on the file exchange?
 
  • #6
I don't have it on this system but I will post the actual code here tomorrow (it's very simple, I just took the trapezium rule and applied it to each variable. The idea of the code is that you input a matrix of the function at equally spaced points and the increments of the function dx and dy and then you apply this function.
 
  • #7
That sounds like it can be handy in particular situations in image processing?
 
  • #8
It might be, I have never done any image processing. I checked the process with some simple integrands which I knew the answers and they seem to work quite well with small enough spacing.
 
  • #9
This is the code I wrote for it:

function y=trap_2d(A,dx,dy)
N=length(A(:,1));
M=length(A(1,:));

a=A(1,1)+A(1,M)+A(N,1)+A(N,M);
b=sum(A(1,:))+sum(A(N,:));
c=sum(A(:,1))+sum(A(:,M));
u=zeros(1,N);
for i=1:N
u(i)=sum(A(i,:));
end

d=sum(u);

y=(d-0.5*c-0.5*b-0.25*a)*dx*dy;
 

Related to Matlab: Numerical integration of a multivariable symbolic function

1. What is Matlab?

Matlab is a high-level programming language and interactive computing environment commonly used in scientific and engineering applications. It allows for numerical computation, data analysis, and visualization.

2. What is numerical integration?

Numerical integration is a method for approximating the definite integral of a function by dividing the interval into smaller subintervals and calculating the area under the curve using a numerical algorithm.

3. How do I perform numerical integration in Matlab?

To perform numerical integration in Matlab, you can use the "integral" function. This function takes in a symbolic expression, the limits of integration, and any additional parameters and returns the approximate value of the integral.

4. Can Matlab perform numerical integration for multivariable symbolic functions?

Yes, Matlab has the ability to integrate multivariable symbolic functions using the "integral2" or "integral3" functions, depending on the number of variables. These functions take in the symbolic expression, the limits of integration for each variable, and any additional parameters.

5. How accurate is the numerical integration in Matlab?

The accuracy of numerical integration in Matlab depends on the number of subintervals used and the complexity of the function being integrated. Generally, using a higher number of subintervals will result in a more accurate approximation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top