## 85  Pendulum Gravity Estimation

User’s Guide for DIRCOL

Problem 2.5 Pendulum

### 85.1  Problem Formulation

Find u over t in [0; 2 ] to minimize

J =
 1 2
*((xtfxfmeas)2 + (ytfyfmeas)2

subject to:

 dx dt
= u
 dy dt
= v
 du dt
= lambda*x/m
 dv dt
= lambda*y/mg
 x2 + y2 − L2 = 0
 [x0  y0  u0  v0] = [0.4  −0.3  0  0]
 [xfmeas  yfmeas] = [−0.231625  −0.443109]
 L = 0.5
 m = 0.3

Reference: [33]

### 85.2  Problem setup

```toms t g

% Initial guess
gopt = 20;
xopt = 0.4-(0.4+0.231625)*t/2;
yopt = -0.3-(-0.3+0.443109)*t/2;
uopt = 0;
vopt = 0;
lambdaopt = -5;

for n=[20 51]
```
```    p = tomPhase('p', t, 0, 2, n);
setPhase(p);
tomStates x y u v
tomControls lambda

% Initial guess
x0 = {g == gopt
icollocate({
x == xopt
y == yopt
u == uopt
v == vopt})
collocate(lambda == lambdaopt)};

% Box constraints
cbox = {1 <= g <= 100};

% Boundary constraints
cbnd = initial({x == 0.4; y == -0.3
u == 0; v == 0});

L   = 0.5;
m   = 0.3;
xmeas = -0.231625;
ymeas = -0.443109;

% ODEs and path constraints
ceq = collocate({
dot(x) == u
dot(y) == v
dot(u) == lambda.*x/m
dot(v) == lambda.*y/m-g
x.^2 + y.^2 - L^2 == 0});

% Objective
objective = 1/2*((final(x)-xmeas)^2+(final(y)-ymeas)^2);
```

### 85.3  Solve the problem

```    options = struct;
options.name = 'Pendulum Gravity';
solution = ezsolve(objective, {cbox, cbnd, ceq}, x0, options);
gopt = subs(g, solution);
xopt = subs(x, solution);
yopt = subs(y, solution);
uopt = subs(u, solution);
vopt = subs(v, solution);
lambdaopt = subs(lambda, solution);
```
```Problem type appears to be: qpcon
Starting numeric solver
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2011-02-05
=====================================================================================
Problem: ---  1: Pendulum Gravity               f_k       0.000000017178935982
sum(|constr|)      0.000001384358714611
f(x_k) + sum(|constr|)      0.000001401537650593
f(x_0)     -0.124997863253000020

Solver: snopt.  EXIT=0.  INFORM=1.
SNOPT 7.2-5 NLP code
Optimality conditions satisfied

FuncEv    1 ConstrEv   98 ConJacEv   98 Iter   43 MinorIter   96
CPU time: 0.265625 sec. Elapsed time: 0.266000 sec.
```
```Problem type appears to be: qpcon
Starting numeric solver
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2011-02-05
=====================================================================================
Problem: ---  1: Pendulum Gravity               f_k       0.000000000017699633
sum(|constr|)      0.000000032137877804
f(x_k) + sum(|constr|)      0.000000032155577437
f(x_0)     -0.124997846074063950

Solver: snopt.  EXIT=0.  INFORM=1.
SNOPT 7.2-5 NLP code
Optimality conditions satisfied

FuncEv    1 ConstrEv   13 ConJacEv   13 Iter   12 MinorIter  190
CPU time: 0.437500 sec. Elapsed time: 0.484000 sec.
```
```end
```

### 85.4  Show result

```disp(sprintf('Gravity estimated to %g',gopt));
```
```Gravity estimated to 9.82655
```