필터 지우기
필터 지우기

Derivative of a function with value of x

조회 수: 8 (최근 30일)
CaiX
CaiX 2020년 11월 10일
답변: John D'Errico 2020년 11월 11일
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!

답변 (1개)

John D'Errico
John D'Errico 2020년 11월 11일
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)

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by