How do I transform this handle into a string?

조회 수: 5 (최근 30일)
DJ V
DJ V 2016년 12월 15일
답변: DJ V 2016년 12월 16일
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
Walter Roberson
Walter Roberson 2016년 12월 15일
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 2016년 12월 15일
Get rid of the '@" symbol and have a function I could use.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 12월 16일
편집: Stephen23 2016년 12월 16일
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
Stephen23
Stephen23 2016년 12월 16일
편집: Stephen23 2016년 12월 16일
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 2016년 12월 16일
Using @sin directly as a parameter is even shown right in the description of your assignment.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Walter Roberson
Walter Roberson 2016년 12월 15일
  댓글 수: 1
DJ V
DJ V 2016년 12월 16일
편집: DJ V 2016년 12월 16일
Sorry, these references are always too alien for me to grasp. I never understood the original code, can you make it clearer?

댓글을 달려면 로그인하십시오.


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

DJ V
DJ V 2016년 12월 16일
Okay, solved this one. Thanks for all the help.

카테고리

Help CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by