How can I evaluate a function that is a function of other functions but all relate to the same parameter such as x
3 views (last 30 days)
Show older comments
I have a bunch of functions that are related to each other through one parameter which is in my case altitude. I am having trouble understanding how MATlab is interpreting what I am writing.
func1=@(x) Some_Constant*x^2
func2=Some_Other_Constant*func1(x)
func3=Yet_Another_Constant*func2*func1
I would like to plot func3 vs x, how is Matlab interpreting what I wrote above and what is the right way to write the above code? Is there a good help page to answer my question? I have not been able to find one thus far.
0 Comments
Answers (1)
dpb
on 9 Dec 2015
Just write it like any other Matlab expression...
f1=@(x) C1*x.^2;
f2=@(x) C2*f1(x);
f3=@(x) C3*f1(x).*f2(x);
This way, of course, the values of the constants are embedded in the function definitions and are insensitive to subsequent changes in those values; if need to redefine C then define the functions as functions of (x,C) and pass the constants.
3 Comments
Stephen23
on 9 Dec 2015
Edited: Stephen23
on 9 Dec 2015
Nothing. There is no difference. Note that dpb's code does not use parentheses around the function calls. When I remove the parentheses from around the two rho_h calls in your code:
CL_H=@(h) 2*Weight/rho_h(h)/V^2/S_w;
Drag_h=@(h) (0.5*rho_h(h)*V^2*S_w)*((f/S_w)+((CL_H(h))^2/pi/e/AR))*Cdi;
it gives exactly the same answer (using the value 1 for all of the undefined variables):
>> Drag_h(1)
ans =
1.132
dpb
on 9 Dec 2015
NB: the real difference in the functions as you've written them vis a vis the way I did is the "dot" operator on the exponent. If you try to pass a vector h yours will fail for that reason. Perhaps you've misdiagnosed the cause of an error, maybe? We've not seen the error you received so can only guess, of course...
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!