Nested functions using handler
Mostra commenti meno recenti
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.
Risposte (1)
Steven Lord
il 24 Mag 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)
Categorie
Scopri di più su Data Structures in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!