load(draw)$

## Tangenta i normala na graf funkcije

Definiramo dvije funkcije koje će nam u zadanoj točki na grafu funkcije pronaći tangentu i normalu.
Degenerirani slučajevi (tangenta i normala su paralelne s koordinatnim osima) nisu posebno razmatrani 
kako ne bismo previše komplicirali kod.

tangenta(fun,x,x0):=block([der: subst(x0,x,diff(fun,x)),y0:subst(x0,x,fun)], y=der*x+y0-x0*der)$

normala(fun,x,x0):=block([der: subst(x0,x,-1/diff(fun,x)),y0:subst(x0,x,fun)], y=der*x+y0-x0*der)$

1. zadatak

f(x):=%e^(sin(x)^2);

vrijednost funkcije u pi/6

f(%pi/6);

tangenta

tangenta(f(x),x,%pi/6);

normala

normala(f(x),x,%pi/6);

wxdraw2d(user_preamble="set size ratio 1",
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-3,1,3],ytics=[-2,0.5,20],explicit(f(x),x,-3,3),line_width=2,color=red,
explicit(rhs(tangenta(f(x),x,%pi/6)),x,-1.5,2.5),color="orange",
explicit(rhs(normala(f(x),x,%pi/6)),x,-1.5,2.5),
color="blue",
point_type=filled_circle,point_size=1.5,
points([[%pi/6,f(%pi/6)]]),
line_type=dots,line_width=1,
parametric(%pi/6,t,t,0,%e^(1/4)),
parametric(t,%e^(1/4),t,0,0.6),
yrange=[-2,4],
label(["pi/6",%pi/6,-0.1]),label(["e^1/4",-0.2,%e^(1/4)])),wxplot_size=[850,750];

draw2d(terminal=wxt,user_preamble="set size ratio 1",
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-3,1,3],ytics=[-2,0.5,20],explicit(f(x),x,-3,3),line_width=2,color=red,
explicit(rhs(tangenta(f(x),x,%pi/6)),x,-1.5,2.5),color="orange",
explicit(rhs(normala(f(x),x,%pi/6)),x,-1.5,2.5),
color="blue",
point_type=filled_circle,point_size=1.5,
points([[%pi/6,f(%pi/6)]]),
line_type=dots,
parametric(%pi/6,t,t,0,%e^(1/4)),
parametric(t,%e^(1/4),t,0,%pi/6),
yrange=[-2,4],
label(["pi/6",%pi/6,-0.1]),label(["e^(1/4)",-0.2,%e^(1/4)]));

duljina odsječka tangente između koordinatnih osi

yos:subst(0,x,rhs(tangenta(f(x),x,%pi/6)));

xos:rhs(solve(rhs(tangenta(f(x),x,%pi/6))=0,x)[1]);

float(sqrt(xos^2+yos^2));

2. zadatak - tangenta na graf funkcije paralelna sa zadanim pravcem

f(x):=(x-1)/(x+1);

diff(f(x),x);

izraz:ratsimp(diff(f(x),x));

solve(izraz=2,x);

točke na grafu

[[-2,f(-2)],[0,f(0)]];

tražene tangente su

tangenta(f(x),x,0);

tangenta(f(x),x,-2);

slika

wxdraw2d(user_preamble="set size ratio 1",nticks=100,
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-3,1,3],ytics=[-5,0.5,8],explicit(f(x),x,-4,3),line_width=2,color=red,
explicit(rhs(tangenta(f(x),x,0)),x,-1.7,2.5),color="orange",
explicit(rhs(tangenta(f(x),x,-2)),x,-3.9,0.5),
color="green",
explicit(2*x+1,x,-2.8,2.5),
color="blue",
point_type=filled_circle,point_size=1.5,
points([[-2,f(-2)],[0,f(0)]]),
color="black",
yrange=[-5,8],label(["y=2x+1",-1.3,-0.8]),
label(["y=2x+7",-0.6,5.2]),label(["y=2x-1",1.4,1.2])),wxplot_size=[750,750];

draw2d(terminal=wxt,user_preamble="set size ratio 1",
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-3,1,3],ytics=[-5,0.5,8],explicit(f(x),x,-4,3),line_width=2,color=red,
explicit(rhs(tangenta(f(x),x,0)),x,-1.7,2.5),color="orange",
explicit(rhs(tangenta(f(x),x,-2)),x,-3.9,0.5),
color="green",
explicit(2*x+1,x,-2.8,2.5),
color="blue",
point_type=filled_circle,point_size=1.5,
points([[-2,f(-2)],[0,f(0)]]),
color="black",
yrange=[-5,8],label(["y=2x+1",-1.3,-0.8]),
label(["y=2x+7",-0.6,5.2]),label(["y=2x-1",1.4,1.2]));

3. zadatak - tangenta i normala paralelne s koordinatnim osima

f(x):=2*(x^2-1)^(1/3);

f(0);

diff(f(x),x);

kod normale dobivamo error jer se dijeli s nulom zbog f'(0)=0, a taj slučaj u definiciji funkcije
"normala" nismo posebno razmatrali. No, mi znamo da je u tom slučaju normala okomita na x-os pa je
njezina jednadžba jednaka x=x0, tj. u našem slučaju x=0.

tangenta(f(x),x,0);

normala(f(x),x,0);

wxdraw2d(user_preamble="set size ratio 1",nticks=100,
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-5,1,5],ytics=[-4,1,6],explicit(f(x),x,-5,5),line_width=2,color=red,
explicit(rhs(tangenta(f(x),x,0)),x,-4.5,4.5),color="#23ab0f", line_width=5,
parametric(0,t,t,-4,5),
color="blue",
point_type=filled_circle,point_size=1.5,
points([[0,f(0)]]),
color="black",
yrange=[-5,6],label(["y=-2",4,-1.8]),
label(["x=0",0.25,4.5])),wxplot_size=[700,700];

draw2d(terminal=wxt,user_preamble="set size ratio 1",
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-5,1,5],ytics=[-4,1,6],explicit(f(x),x,-5,5),line_width=2,color=red,
explicit(rhs(tangenta(f(x),x,0)),x,-4.5,4.5),color="#23ab0f", line_width=5,
parametric(0,t,t,-4,5),
color="blue",
point_type=filled_circle,point_size=1.5,
points([[0,f(0)]]),
color="black",
yrange=[-5,6],label(["y=-2",4,-1.8]),
label(["x=0",0.25,4.5]));

4. zadatak - kut između krivulja

definiramo funkciju koja će nam olakšati računanje kuta između krivulja

kut_krivulja(fun1,fun2,x,x0):=block([k1:subst(x0,x,diff(fun1,x)),k2:subst(x0,x,diff(fun2,x))],
atan(abs(k1-k2)/(1+k1*k2)))$

f1(x):=x^2+5;
f2(x):=2*x^2+1;

solve(f1(x)=f2(x),x);

kut između krivulja u točki presjeka u prvom kvadrantu

kut_krivulja(f1(x),f2(x),x,2);

kut između krivulja u točki presjeka u drugom kvadrantu

kut_krivulja(f1(x),f2(x),x,-2);

wxdraw2d(user_preamble="set size ratio 1",
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-5,1,5],ytics=[-1,1,15],
explicit(f1(x),x,-5,5),
color="#23ab0f",
explicit(f2(x),x,-5,5),
line_width=2,color=red,
explicit(rhs(tangenta(f2(x),x,2)),x,0.9,4.5),
explicit(rhs(tangenta(f1(x),x,2)),x,-0.2,4.5),
point_type=filled_circle,point_size=1.5,
points([[2,f1(2)],[-2,f1(-2)]]),
color="black",
yrange=[-0.5,15],label(["y=-2",4,-1.8])),wxplot_size=[700,700];

draw2d(terminal=wxt,user_preamble="set size ratio 1",
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-5,1,5],ytics=[-1,1,15],
explicit(f1(x),x,-5,5),
color="#23ab0f",
explicit(f2(x),x,-5,5),
line_width=2,color=red,
explicit(rhs(tangenta(f2(x),x,2)),x,0.9,4.5),
explicit(rhs(tangenta(f1(x),x,2)),x,-0.2,4.5),
point_type=filled_circle,point_size=1.5,
points([[2,f1(2)],[-2,f1(-2)]]),
color="black",
yrange=[-0.5,15],label(["y=-2",4,-1.8]));

## Ekstremi

1. zadatak

f(x):=x*%e^(-x);

nultočke

solve(x*%e^(-x)=0,x);

lokalni ekstremi

diff(f(x),x);

solve(%=0,x);

f(1);

radi se o lokalnom maksimumu jer je f''(1)<0

subst(1,x,diff(f(x),x,2));

točke infleksije

solve(diff(f(x),x,2)=0,x);

y=0 je desna horizontalna asimptota (stoga funkcija nema desnu kosu asimptotu)

limit(f(x),x,inf);

funkcija nema lijevu horizontalnu asimptotu, niti lijevu kosu asimptotu

limit(f(x),x,minf);

limit(f(x)/x,x,minf);

graf funkcije

wxdraw2d(user_preamble="set size ratio 1",
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-5,1,5],ytics=[-10,1,2],
explicit(x*%e^(-x),x,-2,5),
yrange=[-7,2]),wxplot_size=[700,700];

draw2d(terminal=wxt,user_preamble="set size ratio 1",
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-5,1,5],ytics=[-10,1,2],
explicit(x*%e^(-x),x,-2,5),
yrange=[-7,2]);

animacija tangente po grafu funkcije - uočite položaj tangente na graf funkcije
u konveksnom i konkavnom području, uočite točku infleksije;
kliknite na donju sliku i kotačićem na mišu možete mijenjati tangentu ili pak
pomoću tipki u toolbaru ("trokut", "kvadrat", "slider") 

with_slider_draw(
   x0, makelist(-0.5+0.25*dx,dx,0,18),
   proportional_axes = xy,
   line_width = 2,
   color = blue,
       explicit(x*%e^(-x), x, -2, 5),
   color = red,
   point_type=filled_circle,
   point_size=1.5,
   points([[x0,x0*%e^(-x0)]]),
   points_joined = true,
   point_size=0,
   points([[x0,x0*%e^(-x0)]-1.5/sqrt(1+(1-x0)^2*%e^(-2*x0))*[1,(1-x0)*%e^(-x0)],
   [x0,x0*%e^(-x0)]+1.5/sqrt(1+(1-x0)^2*%e^(-2*x0))*[1,(1-x0)*%e^(-x0)]]),
   dimensions=[900,500],
   yrange = [-2, 2],
   xrange = [-2, 5.5],
   xaxis = true,
   yaxis = true
)$

2. zadatak

g(x):=x*%e^(-x^2);

nultočke

solve(g(x)=0,x);

kandidati za lokalne ekstreme - stacionarne točke

diff(g(x),x);

factor(%);

solve(%=0,x);

ispitivanje stacionarnih točaka

diff(g(x),x,2);

g2:factor(%);

lokalni minimum

subst(-1/sqrt(2),x,g2);

g(-1/sqrt(2));

g(-1/sqrt(2)),numer;

lokalni maksimum

subst(1/sqrt(2),x,g2);

g(1/sqrt(2));

g(1/sqrt(2)),numer;

točke infleksije

solve(g2=0,x);

y=0 je lijeva i desna horizontalna asimptota (dakle, nema kosih asimptoti) 

limit(g(x),x,inf);

limit(g(x),x,minf);

graf funkcije

wxplot2d(x*%e^(-x^2), [x,-3,3],[y,-1,1])$

ili malo bolja i detaljnija slika

wxdraw2d(grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-3,1,3],ytics=[-1,0.25,1],
explicit(x*%e^(-x^2),x,-3,3),
point_size=2,color=red,point_type=filled_circle,
key="ekstrem",
points([[-1/sqrt(2),g(-1/sqrt(2))],[1/sqrt(2),g(1/sqrt(2))]]),
color="#23ab0f",key="infleksija",
points([[0,0],[sqrt(3/2),g(sqrt(3/2))],[-sqrt(3/2),g(-sqrt(3/2))]]),
xrange=[-3,3],
yrange=[-1,1]),wxplot_size=[800,600];

draw2d(terminal=wxt,
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-3,1,3],ytics=[-1,0.25,1],
explicit(x*%e^(-x^2),x,-3,3),
point_size=2,color=red,point_type=filled_circle,
key="ekstrem",
points([[-1/sqrt(2),g(-1/sqrt(2))],[1/sqrt(2),g(1/sqrt(2))]]),
color="#23ab0f",key="infleksija",
points([[0,0],[sqrt(3/2),g(sqrt(3/2))],[-sqrt(3/2),g(-sqrt(3/2))]]),
xrange=[-3,3],
yrange=[-1,1]);

animacija kretanja tangente po grafu funkcije

with_slider_draw(
   x0, makelist(-2.5+0.25*dx,dx,0,18),
   proportional_axes = xy,
   line_width = 2,
   color = blue,
       explicit(x*%e^(-x^2), x, -3, 3),
   color = red,
   point_type=filled_circle,
   point_size=1.5,
   points([[x0,x0*%e^(-x0^2)]]),
   points_joined = true,
   point_size=0,
   points([[x0,x0*%e^(-x0^2)]-1.5/sqrt(1+(1-2*x0^2)^2*%e^(-2*x0^2))*[1,(1-2*x0^2)*%e^(-x0^2)],
   [x0,x0*%e^(-x0^2)]+1.5/sqrt(1+(1-2*x0^2)^2*%e^(-2*x0^2))*[1,(1-2*x0^2)*%e^(-x0^2)]]),
   dimensions=[900,500],
   yrange = [-1.5, 1.5],
   xrange = [-3.5, 3.5],
   xaxis = true,
   yaxis = true
)$

3. zadatak

h(x):=(x-4)/(3*x-x^2);

nultočke

solve(h(x)=0,x);

kandidati za lokalne ekstreme - stacionarne točke

diff(h(x),x);

ratsimp(%);

solve(%=0,x);

ispitivanje stacionarnih točaka

h2:ratsimp(diff(h(x),x,2));

lokalni minimum

subst(6,x,h2);

h(6);

lokalni maksimum

subst(2,x,h2);

h(2);

točke infleksije - samo je jedna (prva dva rješenja su kompleksni brojevi)

solve(h2=0,x);

2^(4/3)+2^(2/3)+4,numer;

x=0 i x=3 su (lijeve i desne) vertikalne asimptote

limit(h(x),x,0,plus);

limit(h(x),x,0,minus);

limit(h(x),x,3,plus);

limit(h(x),x,3,minus);

y=0 je (lijeva i desna) horizontalna asimptota. Dakle, nema kosih asimptoti.

limit(h(x),x,inf);

limit(h(x),x,minf);

graf funkcije

wxplot2d([(x-4)/(3*x-x^2)], [x,-5,5],[y,-20,20],
[gnuplot_preamble,"set xtics (-4,-3,-2,-1,0,1,2,3,4)"])$

malo detaljnija slika

wxdraw2d(grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-5,1,15],ytics=[-5,1,5],
explicit((x-4)/(3*x-x^2),x,-5,15),
point_size=2,color=red,point_type=filled_circle,
key="ekstrem",
points([[6,-1/9],[2,-1]]),
color="#23ab0f",key="infleksija",
points([[2^(4/3)+2^(2/3)+4,h(2^(4/3)+2^(2/3)+4)]]),
line_width=3,color=red,key="x=3",line_type=dots,
parametric(3,t,t,-5,5),
xrange=[-5,15],
yrange=[-5,5]),wxplot_size=[800,600];

draw2d(terminal=wxt,
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-5,1,15],ytics=[-5,1,5],
explicit((x-4)/(3*x-x^2),x,-5,15),
point_size=2,color=red,point_type=filled_circle,
key="ekstrem",
points([[6,-1/9],[2,-1]]),
color="#23ab0f",key="infleksija",
points([[2^(4/3)+2^(2/3)+4,h(2^(4/3)+2^(2/3)+4)]]),
line_width=3,color=red,key="x=3",line_type=dots,
parametric(3,t,t,-5,5),
xrange=[-5,15],
yrange=[-5,5]);

SLIKA NAS PONEKAD MOŽE PREVARITI. Zato je na donjoj slici nacrtan graf promatrane funkcije
samo na dijelu domene x>3 kako bi se jasnije vidio lokalni minimum u točki x=6 i kako bi se 
jasnije vidjela točka infleksije.

wxdraw2d(grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-4,2,30],ytics=[-1.5,0.25,1.5],
explicit((x-4)/(3*x-x^2),x,3.1,30),
point_size=2,color=red,point_type=filled_circle,
key="ekstrem",
points([[6,-1/9]]),
color="#23ab0f",key="infleksija",
points([[2^(4/3)+2^(2/3)+4,h(2^(4/3)+2^(2/3)+4)]]),
line_width=3,color=red,key="x=3",line_type=dots,
parametric(3,t,t,-5,5),
xrange=[-5,30],
yrange=[-1,1]),wxplot_size=[800,600];

draw2d(terminal=wxt,
grid=true,xaxis=true,xaxis_width=1,xaxis_type=solid,xlabel="x",
yaxis=true,yaxis_width=1,yaxis_type=solid,ylabel="y",line_width=3,color=blue,
xtics=[-5,1,30],ytics=[-1.5,0.25,1.5],
explicit((x-4)/(3*x-x^2),x,3.1,30),
point_size=2,color=red,point_type=filled_circle,
key="ekstrem",
points([[6,-1/9]]),
color="#23ab0f",key="infleksija",
points([[2^(4/3)+2^(2/3)+4,h(2^(4/3)+2^(2/3)+4)]]),
line_width=3,color=red,key="x=3",line_type=dots,
parametric(3,t,t,-5,5),
xrange=[-5,30],
yrange=[-1,1]);

4. zadatak

k(x):=(2*x^2-8)/(5*x);

nultočke

solve(k(x)=0,x);

nema lokalnih ekstrema (nema realnih rješenja)

diff(k(x),x);

ratsimp(%);

solve(%=0,x);

nema točaka infleksije (to ne mora značiti da je funkcija na svojoj prirodnoj domeni
uvijek konveksna ili konkavna)

diff(k(x),x,2);

ratsimp(%);

x=0 je (lijeva i desna) vertikalna asimptota

limit(k(x),x,0,plus);

limit(k(x),x,0,minus);

nema horizontalnih asimptota

limit(k(x),x,inf);

limit(k(x),x,minf);

pravac y=2/5*x je (lijeva i desna) kosa asimptota

limit(k(x)/x,x,inf);

limit(k(x)-2/5*x,x,inf);

limit(k(x)/x,x,minf);

limit(k(x)-2/5*x,x,minf);

graf funkcije

wxplot2d([(2*x^2-8)/(5*x),2/5*x],[x,-10,10],[y,-20,20],
   [gnuplot_preamble, "set grid;"],[style,[lines,2,1],[points,0.5,2,6]]),wxplot_size=[800,600]$

plot2d([(2*x^2-8)/(5*x),2/5*x],[x,-10,10],[y,-20,20],[gnuplot_term,wxt],
       [gnuplot_preamble, "set grid;"],[style,[lines,2,1],[points,1,2,8]])$