Derivative of a function with value of x
42 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
John D'Errico
on 11 Nov 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)
0 Comments
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!