Function inside the function

2 Ansichten (letzte 30 Tage)
Nikola Segedin
Nikola Segedin am 18 Mai 2022
Kommentiert: Nikola Segedin am 18 Mai 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?
Thank you in advance :)

Akzeptierte Antwort

Torsten
Torsten am 18 Mai 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)

Weitere Antworten (1)

Steven Lord
Steven Lord am 18 Mai 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;
Same question about r here.
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);

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by