フィルターのクリア

How do I set an inline function as a parameter?

5 ビュー (過去 30 日間)
snowflake
snowflake 2018 年 6 月 10 日
編集済み: John D'Errico 2018 年 6 月 11 日
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 Center および File ExchangeFunction Creation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by