How do I set an inline function as a parameter?

조회 수: 7 (최근 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 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