Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Integrating a multivariate function w.r.t. a single variable

Asked by Alexandru on 13 Feb 2013

Hello,

I am defining a function using

f = @(x,y) (expression in x and y)

This definition I believe it is correct as I can call f(0,0) for example and I get the numerical value.

What I need to do next is integrate f(x,y) with respect to y between a and b and call this g(x). I need then to be able to pass g(x) to fsolve in order to compute the roots. How do I do this? I tried dblquad, but it integrates w.r.t. both variables at once. quad gives me and error as I am not sure what's the correct syntax for this.

Thanks, Alex

0 Comments

Alexandru

Products

No products are associated with this question.

3 Answers

Answer by Teja Muppirala on 14 Feb 2013
Accepted answer

Define another function handle to be the integral over y. Like this:

% Just making some random 2d function
f = @(x,y) (x.^2-y.^2).*cos(x./(1+y.^2));
% Some limits of integration
a = 0;
b = 3;
% Define g as the integral of f(x,y) dy from a to b
g = @(x) integral(@(y) f(x,y) , a,b);
% Plot it, and find a zero
ezplot(g)
fzero(g,0)

If your version of MATLAB doesn't have the INTEGRAL function, you could use QUAD instead.

0 Comments

Teja Muppirala
Answer by Walter Roberson on 13 Feb 2013

You cannot do this with numeric integration.

If you have the symbolic toolbox, then expression the function symbolically and do symbolic integration with int(). Then if you need, you can use matlabFunction to turn the symbolic result into a function handle of a numeric function.

0 Comments

Walter Roberson
Answer by Youssef KHMOU on 13 Feb 2013

Hi, try this :

 syms x y
 h=exp(-x^2-y^2)
 F1=int(h,x)
 F2=int(h,y)

Based on F1 and F2 you make function handle :

 Fx=@(x) 1/2/exp(x^2)*pi^(1/2)    %  truncated ERF(Y)
 Y=fsolve(Fx,0.1)

2 Comments

Walter Roberson on 13 Feb 2013

How do you get from the "F1 and F2" to the Fx ? And where do the limits of integration over y come in?

Youssef KHMOU on 13 Feb 2013

Hi,i used the undefined integration or "primitive" , for F1 you get :

F1 =
 1/2/exp(y^2)*pi^(1/2)*erf(x)

you have an analytic integration w.r.t. x, so its function of y , now manually you set a function handle Fy :

 Fy=@(x) 1/2/exp(y^2)*pi^(1/2)

and then solve it with "fslove" as Alexandru said he wants to use "fslove" .

Youssef  KHMOU

Contact us