# Matlab微分和導數

MATLAB提供用於計算符號導數的`diff`命令。 以最簡單的形式，將要微分的功能傳遞給`diff`命令作為引數。

``````syms t
f = 3*t^2 + 2*t^(-2);
diff(f)
``````

``````Trial>> syms t
f = 3*t^2 + 2*t^(-2);
diff(f)

ans =

6*t - 4/t^3
``````

``````pkg load symbolic
symbols

t = sym("t");
f = 3*t^2 + 2*t^(-2);
differentiate(f,t)
``````

``````ans =

6*t - 4/t^3
``````

## 基本微分規則的驗證

h(x) = af(x) + bg(x)相對於`x`，由h’(x) = af’(x) + bg’(x)給出。

sumsubtraction規則表述為：如果`f``g`是兩個函式，則`f'``g'`分別是它們的導數，如下 -

``````(f + g)' = f' + g'

(f - g)' = f' - g'
``````

product規則表述為：如果`f``g`是兩個函式，則`f'``g'`分別是它們的導數，如下 -

``````(f.g)' = f'.g + g'.f
``````

quotient規則表明，如果`f``g`是兩個函式，則`f'``g'`分別是它們的導數，那麼 -

``````f' = 0
``````

chain規則表述為 - 相對於`x`的函式`h(x)= f(g(x))`的函式的導數是 -

``````h'(x)= f'(g(x)).g'(x)
``````

``````syms x
syms t
f = (x + 2)*(x^2 + 3)
der1 = diff(f)
f = (t^2 + 3)*(sqrt(t) + t^3)
der2 = diff(f)
f = (x^2 - 2*x + 1)*(3*x^3 - 5*x^2 + 2)
der3 = diff(f)
f = (2*x^2 + 3*x)/(x^3 + 1)
der4 = diff(f)
f = (x^2 + 1)^17
der5 = diff(f)
f = (t^3 + 3* t^2 + 5*t -9)^(-6)
der6 = diff(f)
``````

``````f =
(x^2 + 3)*(x + 2)

der1 =
2*x*(x + 2) + x^2 + 3

f =
(t^(1/2) + t^3)*(t^2 + 3)

der2 =
(t^2 + 3)*(3*t^2 + 1/(2*t^(1/2))) + 2*t*(t^(1/2) + t^3)

f =
(x^2 - 2*x + 1)*(3*x^3 - 5*x^2 + 2)

der3 =
(2*x - 2)*(3*x^3 - 5*x^2 + 2) - (- 9*x^2 + 10*x)*(x^2 - 2*x + 1)

f =
(2*x^2 + 3*x)/(x^3 + 1)

der4 =
(4*x + 3)/(x^3 + 1) - (3*x^2*(2*x^2 + 3*x))/(x^3 + 1)^2

f =
(x^2 + 1)^17

der5 =
34*x*(x^2 + 1)^16

f =
1/(t^3 + 3*t^2 + 5*t - 9)^6

der6 =
-(6*(3*t^2 + 6*t + 5))/(t^3 + 3*t^2 + 5*t - 9)^7
``````

``````pkg load symbolic
symbols
x=sym("x");
t=sym("t");
f = (x + 2)*(x^2 + 3)
der1 = differentiate(f,x)
f = (t^2 + 3)*(t^(1/2) + t^3)
der2 = differentiate(f,t)
f = (x^2 - 2*x + 1)*(3*x^3 - 5*x^2 + 2)
der3 = differentiate(f,x)
f = (2*x^2 + 3*x)/(x^3 + 1)
der4 = differentiate(f,x)
f = (x^2 + 1)^17
der5 = differentiate(f,x)
f = (t^3 + 3* t^2 + 5*t -9)^(-6)
der6 = differentiate(f,t)
``````

## 指數，對數和三角函式的導數

``````syms x
y = exp(x)
diff(y)
y = x^9
diff(y)
y = sin(x)
diff(y)
y = tan(x)
diff(y)
y = cos(x)
diff(y)
y = log(x)
diff(y)
y = log10(x)
diff(y)
y = sin(x)^2
diff(y)
y = cos(3*x^2 + 2*x + 1)
diff(y)
y = exp(x)/sin(x)
diff(y)
``````

``````y =
exp(x)
ans =
exp(x)

y =
x^9
ans =
9*x^8

y =
sin(x)
ans =
cos(x)

y =
tan(x)
ans =
tan(x)^2 + 1

y =
cos(x)
ans =
-sin(x)

y =
log(x)
ans =
1/x

y =
log(x)/log(10)
ans =
1/(x*log(10))

y =
sin(x)^2
ans =
2*cos(x)*sin(x)

y =

cos(3*x^2 + 2*x + 1)
ans =
-sin(3*x^2 + 2*x + 1)*(6*x + 2)

y =
exp(x)/sin(x)
ans =
exp(x)/sin(x) - (exp(x)*cos(x))/sin(x)^2
``````

``````pkg load symbolic
symbols

x = sym("x");
y = Exp(x)
differentiate(y,x)

y = x^9
differentiate(y,x)

y = Sin(x)
differentiate(y,x)

y = Tan(x)
differentiate(y,x)

y = Cos(x)
differentiate(y,x)

y = Log(x)
differentiate(y,x)

% symbolic packages does not have this support
%y = Log10(x)
%differentiate(y,x)

y = Sin(x)^2
differentiate(y,x)

y = Cos(3*x^2 + 2*x + 1)
differentiate(y,x)

y = Exp(x)/Sin(x)
differentiate(y,x)
``````

## 計算高階導數

``````f = x*exp(-3*x);
diff(f, 2)
``````

MATLAB執行上面程式碼將返回以下結果 -

``````ans =
9*x*exp(-3*x) - 6*exp(-3*x)
``````

``````pkg load symbolic
symbols

x = sym("x");
f = x*Exp(-3*x);

differentiate(f, x, 2)
``````

``````syms x
y = 3*sin(x)+7*cos(5*x);  % defining the function
lhs = diff(y,2)+y;        %evaluting the lhs of the equation
rhs = -5*cos(2*x);        %rhs of the equation
if(isequal(lhs,rhs))
disp('Yes, the equation holds true');
else
disp('No, the equation does not hold true');
end
disp('Value of LHS is: '), disp(lhs);
``````

``````No, the equation does not hold true
Value of LHS is:
-168*cos(5*x)
``````

``````pkg load symbolic
symbols

x = sym("x");
y = 3*Sin(x)+7*Cos(5*x);           % defining the function
lhs = differentiate(y, x, 2) + y;  %evaluting the lhs of the equation
rhs = -5*Cos(2*x);                 %rhs of the equation

if(lhs == rhs)
disp('Yes, the equation holds true');
else
disp('No, the equation does not hold true');
end
disp('Value of LHS is: '), disp(lhs);
``````

## 查詢曲線的最大和最小值

``````syms x
y = 2*x^3 + 3*x^2 - 12*x + 17;  % defining the function
ezplot(y)
``````

``````pkg load symbolic
symbols

x = sym('x');
y = inline("2*x^3 + 3*x^2 - 12*x + 17");

ezplot(y)
print -deps graph.eps
``````

``````syms x
y = 2*x^3 + 3*x^2 - 12*x + 17;  % defining the function
ezplot(y, [-2, 2])
``````

``````pkg load symbolic
symbols

x = sym('x');
y = inline("2*x^3 + 3*x^2 - 12*x + 17");

ezplot(y, [-2, 2])
print -deps graph.eps
``````

``````g = diff(y)
``````

MATLAB執行程式碼並返回以下結果 -

``````g =

6*x^2 + 6*x - 12
``````

``````pkg load symbolic
symbols

x = sym("x");

y = 2*x^3 + 3*x^2 - 12*x + 17;
g = differentiate(y,x)
``````

``````s = solve(g)
``````

MATLAB執行程式碼並返回以下結果 -

``````s =
1
-2
``````

``````pkg load symbolic
symbols

x = sym("x");

y = 2*x^3 + 3*x^2 - 12*x + 17;
g = differentiate(y,x)
roots([6, 6, -12])
``````

``````subs(y, 1), subs(y, -2)
``````

MATLAB執行程式碼並返回以下結果 -

``````ans =
10
ans =
37
``````

``````pkg load symbolic
symbols

x = sym("x");

y = 2*x^3 + 3*x^2 - 12*x + 17;
g = differentiate(y,x)

roots([6, 6, -12])

subs(y, x, 1), subs(y, x, -2)
``````

## 求解微分方程

MATLAB提供了用於求解微分方程的`dsolve`命令。

``````dsolve('eqn')
``````

``````dsolve('eqn','cond1', 'cond2',…)
``````

``````'Df = -2*f + cos(t)'
``````

``````'D2y + 2Dy = 5*sin(3*x)'
``````

``````s = dsolve('Dy = 5*y')
``````

MATLAB執行程式碼並返回以下結果 -

``````s =
C2*exp(5*t)
``````

``````dsolve('D2y - y = 0','y(0) = -1','Dy(0) = 2')
``````

MATLAB執行程式碼並返回以下結果 -

``````ans =
exp(t)/2 - (3*exp(-t))/2
``````