How to solve undefined operator error in integral2

Hello,
I want to numerically integrate using integral2 as my function has two variables. However, I always get the error 'Undefined operator '/' for input arguments of type 'function_handle'.' I already searched for help but found nothing that helped me.
Here is my code:
a = 0.15;
v = 1.5e-5;
rho = 1.2;
u = @(x,y)(a.*x.*exp(-y.*sqrt(a./v)));
v = @(y)(sqrt(a.*v).*(exp(-y.*sqrt(a./v))-1));
dudx = @(y)(a.*exp(-y.*sqrt(a./v)));
dudy = @(y)(-a.*exp(-y.*sqrt(a./v)));
f = @(x,y)(rho.*(u(x,y).*dudx(y) + v(y).*dudy(y)));
F = integral2(f, 0, sqrt(v/a), 0, inf)

1 commentaire

Torsten
Torsten le 19 Oct 2017
Please include the integral you want to solve in a mathematical notation.
Best wishes
Torsten.

Connectez-vous pour commenter.

Réponses (1)

In your last line:
F = integral2(f, 0, sqrt(v/a), 0, inf)
You've used v twice in your code, first as a number (1.5e-5) and secondly as the anonymous function @(y)(sqrt(a.*v).*(exp(-y.*sqrt(a./v))-1)). Which did you intend to use here: the number or the function?
If you intend to define the upper limit on x using the number, you must change the name of one of those two variables so the anonymous function doesn't overwrite the number. I would recommend changing the name anyway, to avoid confusing people who read your code in the future (which could include you six months from now) but miss the fact that you redefined v. That name change may also impact the definitions of dudx and dudy depending on what you intended v to be for those functions.
If you intend to define the upper limit on x using the anonymous function, there are two problems. The main one is that by the description given in the documentation for integral2:
"q = integral2(fun,xmin,xmax,ymin,ymax) approximates the integral of the function z = fun(x,y) over the planar region xmin ≤ x ≤ xmax and ymin(x) ≤ y ≤ ymax(x)."
The limits on y are allowed to be functions of x, not the other way around. Even if you were able to switch the order of integration, you cannot divide a function handle by a number. However you could divide the numeric result of evaluating a function handle by a number.
f = @sin;
g = @(x) f(x)./2
y1 = g(pi) % works
h = @(x) f./2
y2 = h(pi) % does not work

1 commentaire

Sometimes I'm really blind...The first v should have been a greek ny, and I always thought of it as one, not noticing that I used v twice. I changed the name and it works now. Thanks a lot!

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by