MATLAB: How to calculate a derivative

derivative and integration

can some one guide me how to calculate a derivative and integration in matlab . can you please give a little example.

Best Answer

Symbolically
syms x real
f = 1/x
int(f,1,2) % integration
ans =
log(2)
diff(f) %differentiation
ans =
-1/x^2
[edit - amplification]
syms x a b real positive
f = 1/x
f =
1/x
int(f) % without limits
ans =
log(x)
int(f,a,b) % with limits
ans =
log(b) - log(a)
fn = matlabFunction(f) % convert symbolic to anonymous function
fn =
@(x)1./x
quadgk(fn,1,2) % integrate numerically
ans =
0.6931
log(2) % previous result from symbolic integration
ans =
0.6931
(fn(2+1e-6)-fn(2))/1e-6 %numerical derivative at fn(2)
ans =
-0.2500
subs(diff(f),2) %substitute 2 into symbolic result previously obtained
ans =
-0.2500