prob3

.html

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

11

Subject

Computer Science

Date

Apr 28, 2024

Type

html

Pages

5

Uploaded by BaronFreedom13241 on coursehero.com

EECS16A: Homework 3 Problem: Segway Tours Run the following block of code first to get all the dependencies. In [1]: # %load gauss_elim.py from gauss_elim import gauss_elim from numpy import zeros, cos, sin, arange, around, hstack from matplotlib import pyplot as plt from matplotlib import animation from matplotlib.patches import Rectangle import numpy as np from scipy.interpolate import interp1d import scipy as sp Example Usage of gauss_elim In [2]: def test_gauss_elim(A, b): aug_matrix = np.c_[A, b] print('Augmented:') print(aug_matrix) print('\nAfter GE:') print(gauss_elim(aug_matrix) ) test_gauss_elim(np.array([[3, 10], \ [-2, 5]]), \ np.array([35, 0]) \ ) Augmented: [[ 3 10 35] [-2 5 0]] After GE: [[1 0 5] [0 1 2]] Dynamics In [22]: # Dynamics: state to state A = np.array([[1, 0.05, -.01, 0], [0, 0.22, -.17, -.01], [0, 0.1, 1.14, 0.10], [0, 1.66, 2.85, 1.14]]); # Control to state b = np.array([.01, .21, -.03, -0.44]) nr_states = b.shape[0] # Initial state state0 = np.array([-0.3853493, 6.1032227, 0.8120005, -14])
# Final (terminal state) stateFinal = np.array([0, 0, 0, 0]) Part (d) In [21]: # YOUR CODE HERE! # Complete Gaussian Elimination to see if we can reach stateFinal in 2 steps. oh = stateFinal - np.dot(np.dot(A,A), state0) we = np.c_[np.dot(A,b),b] lol = np.c_[we,oh] gauss_elim(lol) Out[21]: array([[ 1., 0., 0.], [ 0., 1., 0.], [-0., -0., 1.], [ 0., 0., 0.]]) Part (e) In [20]: # YOUR CODE HERE! # Complete Gaussian Elimination to see if we can reach stateFinal in 3 steps. bro = stateFinal - np.dot(np.dot(np.dot(A,A),A),state0) mee = np.c_[np.dot(np.dot(A,A),b), np.dot(A,b)] lil = np.c_[mee, b] big = np.c_[lil, bro] gauss_elim(big) Out[20]: array([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]]) Part (f) In [23]: # YOUR CODE HERE! # Complete Gaussian Elimination to see if we can reach stateFinal in 4 steps. jeo = stateFinal - np.dot(np.dot(np.dot(np.dot(A,A),A),A),state0) eric = np.c_[np.dot(np.dot(np.dot(A,A),A),b), np.dot(np.dot(A,A),b)] jorge = np.c_[eric, np.dot(A,b)] pinga = np.c_[jorge, b] pene = np.c_[pinga, jeo] print(pene) gauss_elim(pene) [[ 2.62100300e-02 2.41570000e-02 2.08000000e-02 1.00000000e-02 3.17637529e-05] [ 2.29773000e-02 2.43630000e-02 5.57000000e-02 2.10000000e-01 -6.30740802e-02] [-1.26984820e-01 -8.34880000e-02 -5.72000000e-02 -3.00000000e-02 3.18901788e-01] [-5.87888940e-01 -3.42448000e-01 -2.38500000e-01 -4.40000000e-01 1.77659810e+00]] Out[23]:
array([[ 1. , 0. , 0. , 0. , -13.24875075], [ 0. , 1. , 0. , 0. , 23.73325125], [ 0. , 0. , 1. , 0. , -11.57181872], [ 0. , 0. , 0. , 1. , 1.46515973]]) Part (g) Preamble This function will take care of animating the segway In [25]: # frames per second in simulation fps = 20 # length of the segway arm/stick stick_length = 1. def animate_segway(t, states, controls, length): #Animates the segway # Set up the figure, the axis, and the plot elements we want to animate fig = plt.figure() # some config segway_width = 0.4 segway_height = 0.2 # x coordinate of the segway stick segwayStick_x = length * np.add(states[:, 0],sin(states[:, 2])) segwayStick_y = length * cos(states[:, 2]) # set the limits xmin = min(around(states[:, 0].min() - segway_width / 2.0, 1), around(segwayStick_x.min(), 1)) xmax = max(around(states[:, 0].max() + segway_height / 2.0, 1), around(segwayStick_y.max(), 1)) # create the axes ax = plt.axes(xlim=(xmin-.2, xmax+.2), ylim=(-length-.1, length+.1), aspect='equal') # display the current time time_text = ax.text(0.05, 0.9, 'time', transform=ax.transAxes) # display the current control control_text = ax.text(0.05, 0.8, 'control', transform=ax.transAxes) # create rectangle for the segway rect = Rectangle([states[0, 0] - segway_width / 2.0, -segway_height / 2], segway_width, segway_height, fill=True, color='gold', ec='blue') ax.add_patch(rect)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help