Confusion with function handles

5 visualizaciones (últimos 30 días)
Jesse
Jesse el 25 de Jul. de 2012
Greetings all,
This is the second part of what I am trying to work out, and I am new to function handles (i.e, @f, if you will). I'm trying to use three programs scripts at once, since someone told me it was best to do it this way, but I'm not seeing it. I'm open to suggestions, but I am seeing an error.
The first script I have contains the line:
R(D,:) = (V_1*F_1 - r)/(V_1*F_1 + r);
where all the terms are defined, and "r" is imaginary (as this issue was resolved yesterday - shout out to Walter Roberson).
I was told to invoke my second script (titled hankel0.m) at the command prompt, but that I had to define a function f.m first (my third script).
So my f.m file contains:
function f=f(r)
f=R(D,r)*r^2*exp(-r*A)
where R(D,r) = R(D,:) = (V_1*F_1 - r)/(V_1*F_1 + r), r is imaginary, and A is a constant - all of these are in the first script. and at the command prompt I say
>>z=hankel0(@f,B)
to invoke the call to hankel0.m, and B is a constant previously defined in my first script as well (and shows up in the variable space).
When I run the command above, I get an "Undefined function or variable 'D'" error. Now, 'D' is defined in my first script as well, and is in the variable space (it's an array of positive integers) since I ran the first script prior to invoking a hankel0 function call at the command prompt.
Obviously I'm doing something wrong, and I am new to function handles. Can anyone break it down for me? Again, I am new to function handles, so go easy on me.
Thanks!
  5 comentarios
per isakson
per isakson el 26 de Jul. de 2012
Editada: per isakson el 26 de Jul. de 2012
Do we agree that a main function is defined in an m-file as
function out = func_name( in )
code
end
and that a script is defined in an m-file as
code
?
You cannot invoke a script (as defined above) with input arguments!
Thus, hankel0 is that a script or function?
Jesse
Jesse el 26 de Jul. de 2012
per isakson, thanks for responding.
Ok, to answer your question - hankel0 is a function. The line where I stated function f=f(r) and the line after that, that's also a function. My third "file," if you will, is a script - no functions in that for sure.
Apologies for the nomenclature. Also, do you need an "end" statement in your function m-file?
Any further thoughts?

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 26 de Jul. de 2012
Editada: per isakson el 26 de Jul. de 2012
Matlab doesn't need the keyword "end". However, it helps me to understand if you show the complete function or just an excerpt. And Matlab needs it if you include a nested function in the file. IMO it is a good habit to close functions with "end".
Am I right that your function, f, looks something like this?
function f = f(r)
f = R(D,r)*r^2*exp(-r*A)
more code
end
If so it cannot possibly run, because the variables, D and A, are not assigned values in the workspace of the function. The messaqe, "Undefined function or variable 'D'", reports this problem. From Matlabs point of view there could have been a function named, D, in the search path. BTW: "f" is not a good name of a function. Next time Matlab will find it when you forget to pass the variable, "f".
One of the reasons why it is important to distinguish between functions and scripts (Matlabs nomenclature) is that
  • scripts operate in the workspace, in which they are invoked.
  • functions have their own workspace
(Search for "workspace" i the documentation. BTW: All this is better described in the Getting Started.)
Am I right that you have two functions and one script:
function out = hankel0( foo, B )
% foo is function_handle
% B is double scalar
code
end
,
function f = f(r)
% it might be legal, but the same name for the output and
% the function hurts
f = R(D,r)*r^2*exp(-r*A)
more code
end
and
% start script - does it have a name?
R(D,:) = (V_1*F_1 - r)/(V_1*F_1 + r);
more code
% end script
I cannot see the connection between the functions and the script. The call
>>z=hankel0(@f,B)
doesn't engage the script.
Over

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by