How do I set an inline function as a parameter?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Stephen23
el 10 de Jun. de 2018
Editada: Stephen23
el 10 de Jun. de 2018
@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.
Respuestas (1)
John D'Errico
el 10 de Jun. de 2018
Editada: John D'Errico
el 10 de Jun. de 2018
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 comentarios
John D'Errico
el 10 de Jun. de 2018
Editada: John D'Errico
el 11 de Jun. de 2018
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
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!