# Tema 7
07-1  Derivada d'una funció. 
          Funcions derivables. 
          Càlcul de derivades.

kill(all)$

## Definició de derivada en un punt

f1(x):=x^2-2*x+2$     
y1(x):=4*x-6$     y2(x):=3*x-4$     y3(x):=2*x-2$

wxplot2d([f1(x),y1(x),y2(x),y3(x)], [x,1,4], [legend, "f1(x)", "y1(x)", "y2(x)", "y3(x)"] , 
[style, [lines, 2]] , [gnuplot_preamble, "set grid"], [nticks, 200] )$

'limit((f1(x)-f1(2))/(x-2), x, 2)=limit((f1(x)-f1(2))/(x-2), x, 2);

f2(x):=abs(x^2-2*x-8);

wxplot2d([f2(x)], [x,0, 8],[y,-2,12] , [legend, "f2(x)"] , [ylabel, ""], [style, [lines, 2]] ,
[gnuplot_preamble, "set grid"] )$

'limit((f2(x)-f2(4))/(x-4), x, 4)=limit((f2(x)-f2(4))/(x-4), x, 4);

## Derivades laterals

'(D-f2(4)='limit((f2(x)-f2(4))/(x-4), x, 4, minus)) =
                 limit((f2(x)-f2(4))/(x-4), x, 4, minus);

'(D+f2(4)='limit((f2(x)-f2(4))/(x-4), x, 4, plus))  =
                   limit((f2(x)-f2(4))/(x-4), x, 4, plus);

## Càlcul de derivades

Df1(x)=diff(f1(x), x);

Df1(3)=diff(f1(x), x, 3);

define ( Df1(x) , diff(f1(x), x) );

'Df1(3) = Df1(3);    'Df1(a) = Df1(a);

F(x):=x^2*(sin(1/x));       diff(F(x),x);

define ( DF(x) , diff(F(x),x) );

'DF(0)=limit((F(x))/(x), x, 0);

## Derivada infinita

g1(x):=-sqrt(-x);             g2(x):=sqrt(x);
      g(x):= if x<=0 then g1(x) else g2(x);

wxplot2d([g(x)], [x,-2,2],  [y, -2,2], [legend, "g1(x)"] , [ylabel, ""], [style, [lines, 2]] ,     
[gnuplot_preamble, "set grid"], [nticks, 200] )$

diff(g1(x), x);     diff(g2(x), x);

'(D-g(0))  = limit((g1(x)-g1(0))/(x), x, 0, minus);
'(D+g(0)) = limit((g2(x)-g2(0))/(x), x, 0, plus);

## Derivació de funcions amb paràmetres

f4(x):=a_0 + a_1*x + a_2*x^2 + a_3*x^3;
Df4(x) = diff(f4(x), x);

f5(x):=exp(a*x);
Df5(x) = diff(f5(x), x);

f6(x):=sin(a*x)+x*log(b*x);
Df6(x) = diff(f6(x), x);