Nested functions using handler

Hi, Sorry for the ambiguous title, I have three functions
syms a
a = 52.9
f1 = @(r,t,p) (1/sqrt(2*pi*a)).*exp(-r./a)
f2 = @(r,t,p) (1/sqrt(15*pi*a)) .* exp(-r./(3*a))
f3 = @(r,t,p) f1 .* f2 .* (r.^3) .* cos(t) .* sin(t)
integral3(f3,0,Inf,0,pi,0,2*pi)
If I run this I would get following error:
Error using @(f1,f2)@(r,t,p)f1*f2.*(r.^3).*cos(t).*sin(t)
Too many input arguments.
But if I incorporate the f1 and f2 functions in f3 function, it works perfectly fine:
f3 = @(r,t,p) (1/sqrt(2*pi*a)).*exp(-r./a) * (1/sqrt(15*pi*a)) .* exp(-r./(3*a)) .* (r.^3) .* cos(t) .* sin(t)
integral3(f3,0,Inf,0,pi,0,2*pi)
ans =
-7.7376e-11
For the type of usage that I intend to use, I have to have it defined as three separate functions. I appreciate it if you can help me figure out the problem.

Respuestas (1)

Steven Lord
Steven Lord el 24 de Mayo de 2017
You don't need the syms call, since you immediately overwrite the symbolic variable with a numeric value.
You can't multiply two function handles together. You can multiply the values you receive by evaluating two function handles together.
f1 = @(x) sin(x);
f2 = @(x) cos(x);
willWork = @(x) f1(x).*f2(x);
willWork(1:10)
willNOTWork = @(x) f1.*f2;
willNOTWork(1:10)

Etiquetas

Preguntada:

el 24 de Mayo de 2017

Respondida:

el 24 de Mayo de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by