How do I set an inline function as a parameter?

7 次查看(过去 30 天)
I thought this would work:
function x=bisectie(a,b,err,inline(f,'x'))
I would like to be able to call it like this:
bisectie(0,1,0.00001,x-2^(-x))
but it says x is undefined. Does anyone know how I should write it correctly?
  1 个评论
Stephen23
Stephen23 2018-6-10
编辑:Stephen23 2018-6-10
@Cristina X: inline functions are totally outdated, very inefficient, and will be removed soon from MATLAB altogether. The best way to store a reference to a function is to use a function handle. Function handles are the correct object for storing functions because that is exactly what they were designed for.

请先登录,再进行评论。

回答(1 个)

John D'Errico
John D'Errico 2018-6-10
编辑:John D'Errico 2018-6-10
I assume you want to pass in a function to be used in your bisection code?
First, don't use inline functions, unless your MATLAB release is nearly so old that it came on a 3.5 inch floppy disk. (It is possible that you were not even born when that was the case.) inline functions are inefficient.
Use a function handle.
f = @(x) x - 2.^(-x);
Now, just pass in f to your code.
bisectie(0,1,0.00001,f)
Internally, just call it like any function, i.e., as f(x).
  2 个评论
snowflake
snowflake 2018-6-10
The thing is I want the user to be able to write whatever function he/she wants, bisectie(0,1,0.00001, x^2 + 1) or bisectie(0,1,0.00001, x^3)
function x=bisectie(a,b,err,@(x) f)
Could something like this work?
John D'Errico
John D'Errico 2018-6-10
编辑:John D'Errico 2018-6-11
NO. It cannot work as you are writing it. Why not read the help docs about functions?
doc function
As easily, just read my answer, where I showed how to do this.
Create a function as a function handle, as I showed you how to do.
If you insist on being able to provide any function, then it is quite simple to just require a function handle. But, if your goal is to be able to take a string as input (god save me from students, who insist on writing code they would never really want to use to complete a homework assignment, nor should they use the code they do produce) then you can use inline. This does work:
f = inline('x^2+1');
f(2)
ans =
5
But, no, you cannot write it like this:
bisectie(0,1,0.00001, x^2 + 1)
Could you convert a string to symbolic form internally, then use str2sym, then symfun? Well, yes. That will also be inefficient as hell.
syms x
f = symfun(str2sym('x^2+1'),x)
f(x) =
x^2 + 1
double(f(2))
ans =
5
A better solution is to use str2func. For example:
str = 'x^2 + 1';
f = str2func(['@(x) ',str])
f =
function_handle with value:
@(x)x^2+1
f(2)
ans =
5
Is the function handle faster? Yes, it seems to be so, as I have read it is.
str = 'x.^2 + 1';
f0 = inline(str);
f = str2func(['@(x) ',str]);
timeit(@() f0)
ans =
2.0863e-06
timeit(@() f)
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
> In timeit (line 158)
ans =
5.0523e-07

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by