Borrar filtros
Borrar filtros

Derivative of a function with value of x

8 visualizaciones (últimos 30 días)
CaiX
CaiX el 10 de Nov. de 2020
Respondida: John D'Errico el 11 de Nov. de 2020
Hi, I've been trying to code for the derivative of ln(x), where x is a value to be set by the user. Here's what I've done so far..
x = input('Please enter value of x: \n');
f = log(x)
y = diff(f)
disp(y)
I'm not quite sure if im on the right track... should I use
syms x
or
diff(f,x)
I would greatly appreciate any help/explanation. Thank you and stay safe!

Respuestas (1)

John D'Errico
John D'Errico el 11 de Nov. de 2020
You want to find the derivative of a function. First, create the function in a symbolic form.
syms x
f = log(x);
So f is a variable, containing the "function" we want. First, differentiate it.
df = diff(f,x);
So df is a variable. It contains the derivative, but it is not truly a function.
Now, choose a point to evaluate the function at.
x0 = input('Please enter value of x: \n');
And substitute into df. We can use subs for that.
subs(df,x,x0)
Or, if you prefer, we can convert df into a function.
df_fun = matlabFunction(df,x);
df_fun(x0)

Community Treasure Hunt

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

Start Hunting!

Translated by