how to calculate a derivative

1.105 views (last 30 days)
Nasir Qazi
Nasir Qazi on 26 Feb 2012
Commented: Walter Roberson on 22 Dec 2020
can some one guide me how to calculate a derivative and integration in matlab . can you please give a little example.
  1 Comment
Jan on 26 Feb 2012
Symbolically or numerically?

Sign in to comment.

Accepted Answer

bym on 26 Feb 2012
syms x real
f = 1/x
int(f,1,2) % integration
ans =
diff(f) %differentiation
ans =
[edit - amplification]
syms x a b real positive
f = 1/x
f =
int(f) % without limits
ans =
int(f,a,b) % with limits
ans =
log(b) - log(a)
fn = matlabFunction(f) % convert symbolic to anonymous function
fn =
quadgk(fn,1,2) % integrate numerically
ans =
log(2) % previous result from symbolic integration
ans =
(fn(2+1e-6)-fn(2))/1e-6 %numerical derivative at fn(2)
ans =
subs(diff(f),2) %substitute 2 into symbolic result previously obtained
ans =
Jan on 27 Feb 2012
Yes, Nasir, then the integral is calculated from 1 to 2. "Symbolically" mean calculations with symbols, usually characters. The result is a formula. "Numerically" means, that you calculate a numerical value, a number.

Sign in to comment.

More Answers (2)

Magdalena Glinska
Magdalena Glinska on 16 Nov 2020
f = @(x) sin(x);
second_derivative_f = matlabFunction(diff(sym(f)));

Hamza saeed khan
Hamza saeed khan on 24 Nov 2020
syms x
f = x;
why this code give me error as;
>>Error in diff (line 1)
syms x
Walter Roberson
Walter Roberson on 22 Dec 2020
You probably do not have the symbolic toolbox installed or licensed.
Also, you accidentally named your script diff.m which is going to conflict with calling diff() to differentiate.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by