Function inside the function

조회 수: 2 (최근 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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2010b

Community Treasure Hunt

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

Start Hunting!

Translated by