How do I transform this handle into a string?

5 visualizaciones (últimos 30 días)
DJ V
DJ V el 15 de Dic. de 2016
Respondida: DJ V el 16 de Dic. de 2016
My code is:
function x = find_zero( f,x1,x2 )
%FIND_ZERO Summary of this function goes here
% Detailed explanation goes here
Y=fnzeros(f,[x1,x2]);
end
The question is below:
%
  2 comentarios
Walter Roberson
Walter Roberson el 15 de Dic. de 2016
Why ? Your assignment does not call for turning the handle into a string. What would you do with the string if you had it?
DJ V
DJ V el 15 de Dic. de 2016
Get rid of the '@" symbol and have a function I could use.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 16 de Dic. de 2016
Editada: Stephen23 el 16 de Dic. de 2016
You do not need to "Get rid of the '@' symbol". That symbol creates a function handle. A function handle is much more useful for you than anything you could do mucking about with strings. In fact playing around with string would be an awful way to write this code. With a function handle you can simply call it like you would any other function.
Have a look at this:
>> myfun = @(fun,x) fun(x); % define an anonymous function
>> myfun(@sin,pi/2)
ans = 1
>> myfun(@cos,pi/2)
ans = 0
>> myfun(@(n)n+1,pi/2)
ans = 2.5708
I just supply the function handle (either @sin or @cos, or anything else) to my custom function myfun, and it calls that function and does whatever myfun wants with it.
  4 comentarios
Stephen23
Stephen23 el 16 de Dic. de 2016
Editada: Stephen23 el 16 de Dic. de 2016
You could use Walter Roberson's code, or even simpler is to define the function handle directly, without bothering about an anonymous function at all:
x = find_zero(@cos,x1,x2);
Do not make code more complicated than it needs to be! A function handle is just a function that can be called. If you want, you can think of it as a way of "renaming" functions:
>> fun = @sin;
>> fun(pi/2)
ans = 1
Walter Roberson
Walter Roberson el 16 de Dic. de 2016
Using @sin directly as a parameter is even shown right in the description of your assignment.

Iniciar sesión para comentar.

Más respuestas (4)

Walter Roberson
Walter Roberson el 15 de Dic. de 2016
  1 comentario
DJ V
DJ V el 16 de Dic. de 2016
Editada: DJ V el 16 de Dic. de 2016
Sorry, these references are always too alien for me to grasp. I never understood the original code, can you make it clearer?

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 16 de Dic. de 2016
Sometimes you have code where you want to evaluate a function immediately.
x = 0:0.1:2*pi;
y = sin(x); % call the sin function right now
plot(x, y)
Sometimes you have code where you want to specify a function to be evaluated eventually, not immediately. To do that you specify a function handle. When the function handle is evaluated, it is exactly like you evaluated the original function at that point.
fh = @sin;
integral(fh, 0, 1)
In that example, I don't want to call the sin function and pass the result into the integral function. I want to let integral call sin with inputs of its choosing so it can decide where to evaluate the function to accurately compute the integral.
One analogy for immediate function calls versus function handles is talking to a person standing next to you. If you want to talk to them right away, you talk to them directly (the direct function call of the first code block.) If you want to talk to them later, you may ask them for their phone number. Later you use that phone number to talk to them. In this rough analogy, the phone number is a "person handle" like fh is a function handle in the second code block.

DJ V
DJ V el 16 de Dic. de 2016
Okay, I solved this one. Thanks to all for responding.

DJ V
DJ V el 16 de Dic. de 2016
Okay, solved this one. Thanks for all the help.

Categorías

Más información sobre Spline Postprocessing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by