Function handle doesn't work as intended
Mostrar comentarios más antiguos
Hi, I have a question regarding my matlab-code. Basically, the code should calculate the heat-transfer for drop-wise condensation. At the end of the program, I should have a funvtion with deltaT as variable parameter. But the error-message basically says that .* can't be used with function handles. I attached the file, so if anyone is willing to help me, you would make me very happy :) Any ideas how I could get the program fixed? Thanks in advance and best regards, Karsten
2 comentarios
James Tursa
el 15 de Jun. de 2017
Please copy and paste the entire error message so we know which exact line is causing the problem and which exact error message MATLAB is giving you.
Karsten Gros
el 15 de Jun. de 2017
Editada: James Tursa
el 15 de Jun. de 2017
Respuesta aceptada
Más respuestas (2)
James Tursa
el 15 de Jun. de 2017
Just quickly looking though what was posted, this line:
grid onq_tot2 = @(deltaT) integral(@(r) stopp(r), r_c, r_max)
looks like it should be two separate lines:
grid on
q_tot2 = @(deltaT) integral(@(r) stopp(r), r_c, r_max)
Karsten Gros
el 19 de Jun. de 2017
Editada: Karsten Gros
el 19 de Jun. de 2017
0 votos
8 comentarios
Walter Roberson
el 19 de Jun. de 2017
Your original line
q_tot1 = @(deltaT) 2./r_c.*((1+cos(Theta)./(2.*h_i)+delta./(lamda_coat.*sin(Theta).^2)+(r.*Theta)./(4.*lamda_l.*sin(Theta)))).^(-1).*r_c.^2.*pi.*(deltaT.*ln(r_c./r_min(deltaT))+2.*T_sat.*sigma./(H_fg.*rho_l).*(1./r_c-1./r_min(deltaT)))
has +(r.*Theta) as part of it, but r is not defined at that point. Changing that r to r_c allows the code to progress.
Karsten Gros
el 20 de Jun. de 2017
Walter Roberson
el 20 de Jun. de 2017
You have
q_tot2 = @(deltaT) integral(@(r) stopp(r), r_c, r_max)
you need
q_tot2 = @(DT) arrayfun(@(deltaT) integral(@(r) stopp(r,deltaT), r_c, r_max), DT);
This passes through the deltaT to stopp, which needs two parameters but you were only passing one. It also takes care of some issues with the fact that fplot calls the function with a vector but the code was only expecting to be called with a scalar.
When you have a true function (created with function) that uses a parameter and you do not pass something in that position, then MATLAB does not look throuh the calling environment to find a parameter with the name and use it: true functions are blind to their calling environment (except to what is provided by evalin('caller') or provided by inputname(), or dbstatus() or mfilename())
When you define an anonymous function and explicitly refer to a variable that is not named in the @() argument list, then at the time that the anonymous function is defined, MATLAB will look for a definition for that variable and will copy its current value into the function definition, "capturing" the value. If you refer to a variable that is not in the @() parameter list and is not defined at the time the anonymous function is defined, then MATLAB will give you an error at execution time, even if a variable of the same name is assigned to between the time the anonymous function is created and the time the anonymous function is called.
In your case you had something of the form
@(variable) somefunction(different_variable)
where somefunction was defined as an anonymous function @(somename, variable) . But at the time of calls, MATLAB does not look into the calling environment to search for a variable with a matching name, so even through somefunction wanted "variable" and there was a "variable" in the environment at the time of the call, MATLAB does not do that kind of looking by name. Looking by name is only for explicit references (and only at the time anonymous functions are defined.) So for example,
different_variable = 2343.3432914;
abc = @(variable) somefunction(different_variable, variable)
would be fine, because variable of the call would be matched against the parameter name from the definition, and the explicitly mentioned different_variable would be looked for in the environment and found and its 2343.3432914 value would be copied in to the function definition.
Karsten Gros
el 28 de Jun. de 2017
Walter Roberson
el 28 de Jun. de 2017
MATLAB passes values positionally. For example, @(Angle) sin(Angle) and @(theta) sin(theta) do the same thing. You do not need to match parameter names between different anonymous functions.
Karsten Gros
el 28 de Jun. de 2017
Editada: Walter Roberson
el 28 de Jun. de 2017
Walter Roberson
el 28 de Jun. de 2017
No, you have not defined dT for that last line. Is there a reason you changed it from
q_tot = @(deltaT) q_tot1(deltaT)+q_tot2(deltaT);
Variable names do NOT need to match in the calling function and the called function! Variable names need to be consistent within any one function.
Karsten Gros
el 28 de Jun. de 2017
Categorías
Más información sobre Graphics Object Properties en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!