How do I transform this handle into a string?
5 次查看(过去 30 天)
显示 更早的评论
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
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?
采纳的回答
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
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
2016-12-16
Using @sin directly as a parameter is even shown right in the description of your assignment.
更多回答(4 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spline Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!