## Practice Final Exam

#### Final Exam 2015 Final Exam Final Exam Files

#### Solution 1

import numpy as np
from gekko import GEKKO

# Check solution with GEKKO
m = GEKKO()
m.time=[0,1]
u = m.Var(value=1)
x = m.Var(value=0)
m.Obj((x-3)**2)
m.Equation(5*x.dt()==-x+2*u)
m.options.IMODE = 6
m.options.NODES = 3
m.solve()

# Matrix solution for Orthogonal Collocation on Finite Elements
#
# min (x-3)^2
# s.t. 5 * dx/dt = -x + 2 * u
#
# Equations 1-2 (subject to equations at each node)
# 5 * xdot1 = -x1 + 2*u1
# 5 * xdot2 = -x2 + 2*u2
# Equations 3-4 (collocation equations)
# t2 * 0.75 * xdot1 - t2 * 0.25 * xdot2 = x1 - x0
# t2 * 1.00 * xdot1 = x2 - x0
# Equations 5-6 (derivative of objective = 0)
# 2 * (x1-3) = 0
# 2 * (x2-3) = 0
#
# Rearrange to put all variables on left-hand side
# Equations 1-2 (subject to equations at each node)
# 5 * xdot1 + x1 - 2*u1 = 0
# 5 * xdot2 + x2 - 2*u2 = 0
# Equations 3-4 (collocation equations)
# t2 * 0.75 * xdot1 - t2 * 0.25 * xdot2 - x1 = -x0
# t2 * 1.00 * xdot1 - x2 = -x0
# Equations 5-6 (derivative of objective = 0)
# 2 * x1 = 6
# 2 * x2 = 6
#
# Set-up and solve A y = b
# y = [xdot1 xdot2 x1 x2 u1 u2]
# Matrix A
A = np.array([[5,0,1,0,-2,0],\
[0,5,0,1,0,-2],\
[0.75,-0.25,-1,0,0,0],\
[1,0,0,-1,0,0],\
[0,0,2,0,0,0],\
[0,0,0,2,0,0]])
# Column vector b
b = np.array([0,0,0,0,6,6])
# Solve A y = b as y = A^-1 * b
ymat = np.linalg.solve(A,b)

print('Variables with Orthogonal Collocation')
print(['u1 = ' + str(ymat)])
print(['u2 = ' + str(ymat) + \
' (Matrix) vs ' + str(u.value[-1]) + ' (GEKKO)'])
print(['x1 = ' + str(ymat)])
print(['x2 = ' + str(ymat) + \
' (Matrix) vs ' + str(x.value[-1]) + ' (GEKKO)'])
print(' ')
print('Derivatives with Orthogonal Collocation')
print(['d(x11)/dt = ' + str(ymat)])
print(['d(x21)/dt = ' + str(ymat)]) KKT Conditions for Dynamic Optimization: Solution Files

#### Solution 2 See Inverted Pendulum Solution See Level Control
{"ft_lang_label":"__label__en","ft_lang_prob":0.5599804,"math_prob":0.9999112,"size":3484,"snap":"2020-45-2020-50","text_gpt3_token_len":1596,"char_repetition_ratio":0.14942528,"word_repetition_ratio":0.9689808,"special_character_ratio":0.52123994,"punctuation_ratio":0.16331361,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99949896,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,9,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-26T21:42:08Z\",\"WARC-Record-ID\":\"<urn:uuid:cf733acd-1bd1-4823-bda2-f886bbdd3039>\",\"Content-Length\":\"34279\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fa496337-2bb6-4650-ac70-105edc636664>\",\"WARC-Concurrent-To\":\"<urn:uuid:4058a901-d433-450f-b056-769839666fcc>\",\"WARC-IP-Address\":\"128.187.56.207\",\"WARC-Target-URI\":\"https://apmonitor.com/do/index.php/Main/FinalExam\",\"WARC-Payload-Digest\":\"sha1:PZATZMDGNENB5J6PGG4YATVTXQZWC74F\",\"WARC-Block-Digest\":\"sha1:Z632X3UBZOYCJAJQXCT2XUNXMXKYAEUY\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107892062.70_warc_CC-MAIN-20201026204531-20201026234531-00165.warc.gz\"}"}