I am not able to integrate heaviside(y-f) 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?
Show older comments
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)
4 Comments
Walter Roberson
on 13 Aug 2020
Formally speaking, Heaviside is undefined when the expression value is exactly 0. There are multiple conventions about what value is to be substituted in that case. Which convention do you want to use?
CHINTALAPUDI GNANA SURYA NARAYANA SAI
on 14 Aug 2020
Edited: CHINTALAPUDI GNANA SURYA NARAYANA SAI
on 14 Aug 2020
Walter Roberson
on 14 Aug 2020
For your purposes, is heaviside(0) equal to 0, or is it equal to 1, or is it equal to 1/2, or is it undefined ?
CHINTALAPUDI GNANA SURYA NARAYANA SAI
on 14 Aug 2020
Edited: CHINTALAPUDI GNANA SURYA NARAYANA SAI
on 14 Aug 2020
Accepted Answer
More Answers (1)
hosein Javan
on 13 Aug 2020
if you need numerical integration, some functions cannot be easily evaluated by symbolic. instead use "integral". in here we have a double integral. the integral calculation method is 'iterated' which means on each iteration it is integrating on one dimension and then the next. it takes a few seconds to accomplish though.
fun = @(x,y) double( y > sin(x) )
q = integral2(fun, 0, pi/2, 0, pi/2, 'Method', 'iterated', 'Reltol', 1e-8)
% answer in command window:
q =
1.4674
here's your function plot. blue part is zero. yellow part equals one.

3 Comments
hosein Javan
on 13 Aug 2020
notice that because one side is totally zero (where y<sin(x)), we can perform the integral by defining the lower limit of the integral in terms of a variable. this time notice that it has become much faster and accurate.
while before it took about 1.2 seconds.
fun = @(x,y) ones(size(x))
tic
q = integral2(fun, 0, pi/2, @(x) sin(x), pi/2, 'RelTol',eps)
toc
q =
1.4674
Elapsed time is 0.005605 seconds.
CHINTALAPUDI GNANA SURYA NARAYANA SAI
on 14 Aug 2020
hosein Javan
on 14 Aug 2020
you're welcome.
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!