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

33 次查看(过去 30 天)
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

采纳的回答

Teja Muppirala
Teja Muppirala 2013-2-14
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.
  3 个评论
ARNAB PAL
ARNAB PAL 2019-9-19
Dear sir,
I have a question that if the function is a matrix function like,
f=@(x,y) K_2*exp(A*(x(i)-y))*B*u;
where K_2 is a (3*6) matrix,A is a (6*6) matrix ,B is (6*3) and u is (3*1) matrix. I want to find g=@(x) integral(@(y) f(x,y),0,x(i)) but when I use this it is not giving the output as a (3*1) vector?
Finally I want to find w=u-(K_2*exp(A*x(i))*X) where X is (6*1).
Walter Roberson
Walter Roberson 2019-9-19
When you call integral(), it is required to return an array the same size as x, which will be a vector of varying sizes. However, there is the 'vectorvalued' option for integral(), and when set then the function will be passed scalars and can return multidimensional outputs as needed.

请先登录,再进行评论。

更多回答(3 个)

Walter Roberson
Walter Roberson 2013-2-13
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.
  2 个评论
Walter Roberson
Walter Roberson 2021-6-5
f = @(x,y) (x.^2-y.^2).*cos(x./(1+y.^2));
% Some limits of integration
a = 0;
b = 3;
syms x y
% Define g as the integral of f(x,y) dy from a to b
g(x) = int(f(x,y), y, a, b)
g(x) = 
x0 = -100;
vpasolve(g, x0)
ans = 
G = matlabFunction(g)
G = function_handle with value:
@(x)integral(@(y)cos(x./(y.^2+1.0)).*(x.^2-y.^2),0.0,3.0)
fsolve(G, x0)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = -99.0803
In some cases, the int() step would be able to calculate a closed form expression, so G will not always end up with an integral() in it.

请先登录,再进行评论。


Youssef  Khmou
Youssef Khmou 2013-2-13
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 个评论
Walter Roberson
Walter Roberson 2013-2-13
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
Youssef Khmou 2013-2-13
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" .

请先登录,再进行评论。


Yang Zhang
Yang Zhang 2015-3-15
Use Symbolic Math Toolbox to your problem.

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by