# Function inside the function

4 views (last 30 days)
Nikola Segedin on 18 May 2022
Commented: Nikola Segedin on 18 May 2022
Hi,
I have three functions:
r=@(s,z) sqrt((s+s0).^2+(z+z0).^2);
Ds=@(r,s)(c0x+A1x.*erf((r+a1x)./b1x)+A2x.*erf((r+a2x)./b2x)+A3x.*erf((r+a3x)./b3x)+A4x.*erf((r+a4x)./b4x).*(s+s0)./r).^2;
Dz=@(r,z)(c0z+A1z.*erf((r+a1z)./b1z)+A2z.*erf((r+a2z)./b2z)+A3z.*erf((r+a3z)./b3z)+A4z.*erf((r+a4z)./b4z).*(z+z0)./r).^2;
and I want to make a fourth one which contains all three mentioned functions. I triyed:
D=@(Ds,s,r,Dz,z) sqrt((Ds.*(s-s0)./r).^2+(Dz.*(z*z0)./r).^2), but is's not working. Can anyone help me out?

Torsten on 18 May 2022
r=@(s,z) sqrt((s+s0).^2+(z+z0).^2);
Ds=@(s,z)(c0x+A1x.*erf((r(s,z)+a1x)./b1x)+A2x.*erf((r(s,z)+a2x)./b2x)+A3x.*erf((r(s,z)+a3x)./b3x)+A4x.*erf((r(s,z)+a4x)./b4x).*(s+s0)./r(s,z)).^2;
Dz=@(s,z)(c0z+A1z.*erf((r(s,z)+a1z)./b1z)+A2z.*erf((r(s,z)+a2z)./b2z)+A3z.*erf((r(s,z)+a3z)./b3z)+A4z.*erf((r(s,z)+a4z)./b4z).*(z+z0)./r(s,z)).^2;
D=@(s,z) sqrt((Ds(s,z).*(s-s0)./r(s,z)).^2+(Dz(s,z).*(z*z0)./r(s,z)).^2)
Nikola Segedin on 18 May 2022
Thanks, you are a life savier <3

Steven Lord on 18 May 2022
r=@(s,z) sqrt((s+s0).^2+(z+z0).^2);
I'm assuming you've defined s0 and z0 before you run this line to define r.
Ds=@(r,s)(c0x+A1x.*erf((r+a1x)./b1x)+A2x.*erf((r+a2x)./b2x)+A3x.*erf((r+a3x)./b3x)+A4x.*erf((r+a4x)./b4x).*(s+s0)./r).^2;
Is r supposed to be a variable here, or are you hoping to call the anonymous function r you defined above here?
Dz=@(r,z)(c0z+A1z.*erf((r+a1z)./b1z)+A2z.*erf((r+a2z)./b2z)+A3z.*erf((r+a3z)./b3z)+A4z.*erf((r+a4z)./b4z).*(z+z0)./r).^2;
If Ds and Dz should call r(s, z), they should be functions of s and z instead of r and s or z. I'm assuming any variables other than r, s, and z in this expression have already been defined. I broke the expression onto multiple lines so you can see each term without scrolling.
Ds=@(s,z) (c0x+A1x.*erf((r(s, z)+a1x)./b1x)+ ...
A2x.*erf((r(s, z)+a2x)./b2x)+ ...
A3x.*erf((r(s, z)+a3x)./b3x)+ ...
A4x.*erf((r(s, z)+a4x)./b4x).*(s+s0)./r(s, z)).^2;
If you do the same thing for Dz, then your D only needs to be a function of s and z.
D= @(s,z) sqrt((Ds(s, z).*(s-s0)./r(s, z)).^2+ ...
(Dz(s, z).*(z*z0)./r(s, z)).^2);

R2010b

### Community Treasure Hunt

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

Start Hunting!

Translated by