MATLAB: How to do differentiation

differentiation

Dear sir/madam,
I have a function y=f(x). I need to differentiate this function and find the value of the differentiation(slope) at a given point (x1,y1). How could I do this in matlab? Looking forward to hearing from you soon.
Thanking you, BSD

Best Answer

variant 1 use Symbolic Math Toolbox e.g.: your function f(x) = x^3+x^2*2+1
syms x
y = x^3+x^2*2+1;
df = matlabFunction(diff(y))
df =
@(x)x.*4.0+x.^2.*3.0
>> point = df(5)
point =
95
>>
variant 2 use interpolation
x = linspace(-5,5,12);
y = x.^3+x.^2*2+1;
fpp = interp1(x,y,'pchip','pp')
dfpp = fnder(fpp)
df = @(x)fnval(dfpp,x)
point = df(5)