A1

.py

School

Texas A&M University *

*We aren’t endorsed by this school

Course

357

Subject

Electrical Engineering

Date

Apr 3, 2024

Type

py

Pages

2

Uploaded by MegaParrot3827 on coursehero.com

import numpy as np import random #A1 task1 N=10 #number of random values generated print("\nAssignment 1 task 1:\n") def sort_ascending(unsorted_list): sorted_list = [] print("unsorted list:",unsorted_list,"\n") for x in range(len(unsorted_list)): low = unsorted_list[0] #sets first element in list as "low" for i in range(len(unsorted_list)): if low > unsorted_list[i]: low = unsorted_list[i] # if "low" value is > than compared value, "low" changes to smaller value sorted_list.append(low) # after all elememts checked in iteration, "low value" added to sorted list unsorted_list.remove(low) #"low value removed from unsorted list before next iteration print("sorted list:",end=" ") return sorted_list def A1_task1(N): rng_list= [] #list to store generated values for i in range(N): rng_list.append(random.randint(-9999,9999)) # generates random values, amount of values set by length variable above sorted_list = sort_ascending(rng_list) return sorted_list sorted_list = A1_task1(N) print(sorted_list) #A1 task2 print("\nAssignment 1 task 2:\n") def A1_task2(): # 1. Defines the following matrices and vectors as Numpy arrays A = np.array([[1,2,3,4],[2,4,6,8],[3,6,9,12],[4,8,12,16]]) C = np.array([[16,12,8,4],[12,9,6,3],[8,6,4,2],[4,3,2,1]]) b = np.array([[-4],[3],[-2],[1]]) d = np.array([[-1],[2],[-3],[4]]) A_t = A.transpose() # 2. Calculates a new array by the following equation of matrix operations (where the superscript T denotes the transpose) # Equation 1: x = ((A + A_t)*b - C*d) x_add = np.add(A, A_t) #adding Matrix A and transposed matrix A_t x1 = x_add @ b #dot product of the combined matrix A and vector b x = x1 - C @ d #eqn(1)
print("x:\n",x,"\n") # 3. Creates a new matrix new by copying the matrix and then switching columns 3 𝐴 𝐴 and 4. A_new = A.copy() # print("\n",A_new) # test print A_new[:, [3,2]] = A_new[:, [2,3]] #print("\n",A_new) # test print # 4. Creates a new matrix new by copying the matrix and then switching rows 1 𝐶 𝐶 and 3. C_new = C.copy() # print("\n",C_new) #test print C_new[[2,0]] = C_new[[0,2]] #print("\n",C_new) #test print # 5. Calculates another array ( _new) by Eqn. (1) using new and new in place of A 𝑥 𝐴 𝐶 and C A_t_new = A_new.transpose() x_new = ((np.add(A_new, A_t_new) @ b) - (C_new @ d)) print("x_new:\n", x_new, "\n") # 6. Finds the maximum and minimum element values of the matrix formed by Eqn(2) 𝑀 # Equation 2: M = A + A_t_new M = np.add(A,A_t_new) print("M:\n",M, "\n") M_max = M.max() print("M_max =", M_max,) M_min = M.min() print("M_min =", M_min,) return x, x_new, M_max, M_min Task2 = A1_task2()
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