Subtracting two derivatives then store in a new function

I want to calculate a double integral using polar coordinates.
xFun = @(x, y) 3.*x.*y;
dx = @(x, y) diff(xFun, x);
yFun = @(x, y) y.^2;
dy = @(x, y) diff(yFun, y);
fun = @(x, y) minus(dx, dy);
polarfun = @(theta,r) fun(r.*cos(theta),r.*sin(theta)).*r;
res = integral2(polarfun,0,pi,1,2);
disp(res);
I have a xFun and its derivative, same as yFun. Then I subtract these two and store the result function in fun. I transform fun to polarfun(using polar coordinates method). But then i cannot find the double integral of polarfun. It says:
Operator '-' is not supported for operands of type 'function_handle'.
Error in untitled>@(x,y)minus(dx,dy) (line 5)
fun = @(x, y) minus(dx, dy);
Can you please point out my mistake and show me a way to solve this problem?
Thanks alot!

1 Comment

You have function handles, symbolic operations, and numeric operations all mixed together. You need to decide whether you want a numeric, symbolic, or functional result.

Sign in to comment.

Answers (3)

One possibility - symbolic differntiation then numeric integration:
syms x y r theta
xFun = 3.*x*y;
dx = diff(xFun,x);
yFun = y.^2;
dy = diff(yFun,y);
fun (x,y)= dx-dy;
polarfun (theta, r)= fun(r.*cos(theta),r.*sin(theta)).*r;
pfun=matlabFunction(polarfun);
res = integral2(pfun,0,pi,1,2)
res = 4.6667
Another possibility- symbolic differntiation then symbolic integration:
syms x y r theta
xFun = 3.*x*y;
dx = diff(xFun,x);
yFun = y.^2;
dy = diff(yFun,y);
fun (x,y)= dx-dy;
polarfun (r ,theta)= fun(r.*cos(theta),r.*sin(theta)).*r;
res = double( int( int(polarfun,theta,0,pi),r,1,2) )
res = 4.6667
Another possibility - numeric differntiation and integration.
xFun = @(x,y) 3.*x.*y;
dxFun = dfunx(xFun);
yFun = @(x,y) y.^2;
dyFun = dfuny(yFun);
fun = @(x,y) dxFun(x,y)-dyFun(x,y);
polarfun = @(theta,r) fun(r.*cos(theta),r.*sin(theta)).*r;
res = integral2(polarfun,0,pi,1,2)
res = 4.6667
function df=dfunx(f)
h=1e-8;
df=@(x,y) (f(x+h,y)-f(x,y))/h;
end
function df=dfuny(f)
h=1e-8;
df=@(x,y) (f(x,y+h)-f(x,y))/h;
end

Tags

Asked:

on 4 Sep 2022

Edited:

on 4 Sep 2022

Community Treasure Hunt

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

Start Hunting!