> with(plots):
> lagrange := proc (n, q, r, L) local i, uzm_q, uzm_r, rel_r_q, Lq, Lr, Lrt; global row; uzm_q := seq(q[i] = q[i](t), i = 1 .. n); uzm_r
:= seq(r[i] = r[i](t), i = 1 .. n); for i to n do Lq[i] := subs([uzm_q, uzm_r], diff(L, q[i])); Lr[i] := subs([uzm_q, uzm_r], diff(L, r[i])) end do; for i to n do Lrt[i] := diff(Lr[i], t) end do; rel_r_q :=
seq(r[i](t) = diff(q[i](t), t), i = 1 .. n); for i to n do row[i] :=
subs(rel_r_q, Lrt[i]-Lq[i] = 0) end do; seq(row[i], i = 1 .. n) end proc:
>
> #Zad1
> E := (1/2)*m*v[1]^2:
> U := (1/2)*k*x^2:
> L := E-U:
> lagrange(1, x, v, L):
>
> #Zad2
> restart:
> E := (1/2)*m*l^2*omega[1]^2:
> U := -m*g*l*cos(phi[1]):
> L := E-U:
> lagrange(1, phi, omega, L):
>
> #Zad3
> restart:
> E := (1/2)*m*(v[1]^2+v[2]^2):
> U := (1/2)*k*(sqrt(x[1]^2+x[2]^2)-l)^2+m*g*x[2]:
> L := E-U:
> lagrange(2, v, x, L):
>
> #Zad4
> restart:
> E :=
(1/2)*m[1]*v[1]^2+(1/2)*m[2]*(v[1]^2+2*v[1]*v[2]*l*cos(x[2])+l^2*
v[2]^2):
> U := (1/2)*k*x[1]^2-m[2]*g*l*cos(x[2]):
> m[1] := 1: m[2] := .1: g := 9.81: l := .1: k := 100:
> wp := x[1](0) = .5*l, (D(x[1]))(0) = 0, x[2](0) = (1/3)*Pi, (D(x[2]))(0) = 0:
> L := E-U:
> lagrange(2, x, v, L):
> dsolve({wp, row[1], row[2]}, {x[1](t), x[2](t)}, numeric):