Function inside the function

11 vues (au cours des 30 derniers jours)
Nikola Segedin
Nikola Segedin le 18 Mai 2022
Commenté : Nikola Segedin le 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 :)

Réponse acceptée

Torsten
Torsten le 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)
  1 commentaire
Nikola Segedin
Nikola Segedin le 18 Mai 2022
Thanks, you are a life savier <3

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 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);

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by