How do I transform this handle into a string?

5 Ansichten (letzte 30 Tage)
DJ V
DJ V am 15 Dez. 2016
Beantwortet: DJ V am 16 Dez. 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 Kommentare
Walter Roberson
Walter Roberson am 15 Dez. 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 am 15 Dez. 2016
Get rid of the '@" symbol and have a function I could use.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 16 Dez. 2016
Bearbeitet: Stephen23 am 16 Dez. 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 Kommentare
Stephen23
Stephen23 am 16 Dez. 2016
Bearbeitet: Stephen23 am 16 Dez. 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 am 16 Dez. 2016
Using @sin directly as a parameter is even shown right in the description of your assignment.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Walter Roberson
Walter Roberson am 15 Dez. 2016
  1 Kommentar
DJ V
DJ V am 16 Dez. 2016
Bearbeitet: DJ V am 16 Dez. 2016
Sorry, these references are always too alien for me to grasp. I never understood the original code, can you make it clearer?

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 16 Dez. 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 am 16 Dez. 2016
Okay, I solved this one. Thanks to all for responding.

DJ V
DJ V am 16 Dez. 2016
Okay, solved this one. Thanks for all the help.

Kategorien

Mehr zu Spline Postprocessing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by