How do I transform this handle into a string?

6 ビュー (過去 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 Center および File ExchangeSpline Postprocessing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by