Function inside the function

9 ビュー (過去 30 日間)
Nikola Segedin
Nikola Segedin 2022 年 5 月 18 日
コメント済み: Nikola Segedin 2022 年 5 月 18 日
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 :)

採用された回答

Torsten
Torsten 2022 年 5 月 18 日
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 件のコメント
Nikola Segedin
Nikola Segedin 2022 年 5 月 18 日
Thanks, you are a life savier <3

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 5 月 18 日
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);

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by