Inequalities using function handles

3 views (last 30 days)
Hi,
I need to integrate a function h(x,y) over a region, but the value of h depends on two other functions f(x,y) and g(x,y) in the following manner:
if f(x,y)<=g(x,y), then h(x,y)=h1(x,y)
else h(x,y)=h2(x,y)
I have created function handles for f and g, and would like to implement a condition like 'if f(x,y)<= g(x,y)', so that I can define the appropriate function handle for h in this regime. Any ideas on how this can be done? Thanks.

Accepted Answer

Steven Lord
Steven Lord on 30 Jan 2017
Assuming x and y are the same size, that all the functions involved are vectorized and in scope:
function z = h(x, y)
fxy = f(x, y);
gxy = g(x, y);
z = NaN(size(x));
condition1 = fxy <= gxy;
z(condition1) = h1(x(condition1), y(condition1));
z(~condition1) = h2(x(~condition1), y(~condition1));
If you've define f, g, h1, and h2 as anonymous functions in the workspace from which you're calling h, you should pass them into h as input.
function z = h(x, y, f, g, h1, h2)

More Answers (0)

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!