简体   繁体   中英

How to integrate heaviside(y-f) in Matlab?

I am not able to integrate heaviside(yf) for x= 0 to pi/2 and y = 0 to pi/2 (only syms integration "int"). After running the code, output is shown in below and I am not able to get the numerical answer. Can you give some ideas on how to proceed?

code:

syms x y
f = sin(x);
integ = int( int(heaviside(y-f), x, 0, pi/2),y, 0, pi/2)

Result:

integ =
 
int(int(heaviside(y - sin(x)), x, 0, pi/2), y, 0, pi/2)

Avoid symbolic calculation whenever possible.

Notice the heaviside function can be defined as:

hvsd = @(x) (x > 0) + 0.5*(x == 0); % another name to do not overshadow heaviside

You can use either integral2 or trapz to evaluate the numerical integral.

heaviside = @(x) (x > 0) + 0.5*(x == 0);
fun = @(x,y) heaviside(y-sin(x));
% using integral2
I1 = integral2(fun,0,pi/2,0,pi/2)

nx = 100; ny = 100;
x = linspace(0,pi/2,nx);
y = linspace(0,pi/2,ny);
[X,Y] = meshgrid(x,y);
Z = fun(X,Y);
% using trapz
I2 = trapz(y,trapz(x,Z.*(Z>0),2))
 I1 = 1.4674 I2 = 1.4695

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM