How to make a function that uses a mathematical function as an input?

I want to create a function that takes two inputs
f(x) and x1
that I can put through the central finite differentiation formula;
y=(f(x1+h)-f(x1-h))/2h
where h is a constant
and the function on each side = f(x)
so for example;
f(x)=sin(x)
x1=2
y=sin(2+h)-sin(2-h)/2*h
I want to function input that i can subsititute into but I don't know how to go about it, I've looked at anonymous function, parameter and other documentation.
I just want a nudge in the right direction, not a complete answer if at all possible.

 Respuesta aceptada

f=@(x)sin(x);

6 comentarios

Alternatively just add
syms x
at the beginning of your code.
In this case you don't need to create an anonymous function. A plain old function handle will do.
f = @sin;
h = 0.1;
y = (f(2+h)-f(2-h))./(2*h)
With Steven Lord's solution, it works but how would i input composate functions?
For example
x * sin(x)?
Then you'd be back to generate an anonymous function:
f = @(x) x.*sin(x);
Yup figured out but awesome, thanks a tonne.
Sorry for all the begginner questions, was just hard to wrap my head around the task due to its wording.
Thanks for all the help all.
@Benjamin: Need not be sorry. You may find this link useful:https://in.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 12 de Mzo. de 2019

Comentada:

el 13 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by