<a href="https://colab.research.google.com/github/Fotrichis/LinearAlgebra_2ndSem/blob/main/Assignment_9_(Galano).ipynb" target="_parent"></a>

# Lab 2 - Plotting Vector using NumPy and MatPlotLib

In this laboratory we will be discussing the basics of numerical and scientific programming by working with Vectors using NumPy and MatPlotLib.

### Objectives
At the end of this activity you will be able to:
1. Be familiar with the libraries in Python for numerical and scientific programming.
2. Visualize vectors through Python programming.
3. Perform simple vector operations through code.

## Discussion

### NumPy

NumPy or Numerical Python, is mainly used for matrix and vector operations. It is capable of declaring computing and representing matrices. Most Python scientific programming libraries uses NumPy as the basic code.

###Scalars 
A single number. Variables representing scalars are typically written in lower case.

Scalars can be whole numbers or decimals.

\begin{align}
a = 12
\qquad
b = 6.5842342
\end{align}

They can be positive, negative, 0 or any other real number.

\begin{align}
c = -8.5412\mathrm{e}{+23}
\qquad
d = \pi
\end{align}


###Vectors 
A vector of dimension *n* is an **ordered** collection of *n* elements, which are called **components**. Vector notation variables are commonly written as a bold-faced lowercase letters or italicized non-bold-faced lowercase characters with an arrow (→) above the letters:

Written: $\vec{v}$ 

Examples:

\begin{align}
   \vec{a} = 
   \begin{bmatrix}
           -10\\
           5
    \end{bmatrix}
    \qquad
    \vec{b} =
    \begin{bmatrix}
          8\\
          21\\
          -3
    \end{bmatrix}
    \qquad
    \vec{c} =
    \begin{bmatrix}
           4.5
    \end{bmatrix}
    \qquad
    \vec{d} =
    \begin{bmatrix}
           a\\
           b\\
           \frac{4}{8}
    \end{bmatrix}
\end{align}

#### Representing Vectors

Now that you know how to represent vectors using their component and matrix form we can now hard-code them in Python. Let's say that you have the vectors:

$$ A = 8\hat{x} + 11\hat{y}  -4\hat{z}\\
B = 15\hat{x} - 7\hat{y} + 10\hat{z}\\
C = 9ax + 10ay - 11az \\
D = 14\hat{i} - 3\hat{j} + 9\hat{k}$$

In which it's matrix equivalent is:

$$ A = \begin{bmatrix} 8 \\ 11\\ -4\end{bmatrix} , B = \begin{bmatrix} 15 \\ -7\\ 10\end{bmatrix} , C = \begin{bmatrix} 9 \\ 10 \\  -11 \end{bmatrix}, D = \begin{bmatrix} 14 \\ -3 \\ 9\end{bmatrix}
$$
$$ A = \begin{bmatrix} 8 & 11&-4\end{bmatrix} , B = \begin{bmatrix} 15 & -7 &  10\end{bmatrix} , C = \begin{bmatrix} 9 & 10 & -11\end{bmatrix} , D = \begin{bmatrix} 14 & -3 & 9\end{bmatrix} 
$$

We can then start doing numpy code with this by:


```python
import numpy as np ## 'np' here is short-hand name of the library (numpy) or a nickname.
```


```python
A = np.array([8, 11, -4])
B = np.array([15, -7, 10])
C = np.array([
              [9],
              [10],
              [-11]
])
D = np.array ([[14],
              [-3],
              [9]])
print('Vector A is ', A)
print('Vector B is ', B)
print('Vector C is ', C)
print('Vector D is ', D)
```

    Vector A is  [ 8 11 -4]
    Vector B is  [15 -7 10]
    Vector C is  [[  9]
     [ 10]
     [-11]]
    Vector D is  [[14]
     [-3]
     [ 9]]


#### Describing vectors in NumPy

NumPy is at the core of many types of scientific computing and is the main tool that we will use to perform Linear Algebra operations with Python.

NumPy arrays can be used to represent vectors but cannot be used to differentiate between row and column vectors.

\begin{align}
\text{column vector} = \begin{bmatrix}1 \\ 2 \\ 3\end{bmatrix}
\end{align}

\begin{align}
\text{row vector} = \begin{bmatrix} a & b & c\end{bmatrix}
\end{align}

Describing vectors is very important if we want to perform basic to advanced operations with them. The fundamental ways in describing vectors are knowing their shape, size and dimensions.


```python
### Checking shapes
### Shapes tells us how many elements are there on each row and column

G = np.array([[15, 11, 2, 9, -0.5, 3, 10]])
G.shape
```




    (1, 7)




```python
### Checking size
### Array/Vector sizes tells us many total number of elements are there in the vector

G.size
```




    7




```python
### Checking dimensions
### The dimensions or rank of a vector tells us how many dimensions are there for the vector.

G.ndim
```




    2



Great! Now let's try to explore in performing operations with these vectors.

#### Addition

The addition rule is simple, the we just need to add the elements of the matrices according to their index. So in this case if we add vector $A$ and vector $B$ we will have a resulting vector:

$$R = 23\hat{x}+4\hat{y}+6\hat{z} \\ \\or \\ \\ R = \begin{bmatrix} 23 \\ 4\\6\end{bmatrix} $$

So let's try to do that in NumPy in several number of ways:


```python
R = np.add(A, B) ## this is the functional method using the numpy library
P = np.add(C, D)
print ("sum of A & B:")
print (R)
print ("sum of C & D:")
print (P)
```

    sum of A & B:
    [23  4  6]
    sum of C & D:
    [[23]
     [ 7]
     [-2]]



```python
R = A + B   ## this is the explicit method, since Python does a value-reference so it can 
            ## know that these variables would need to do array operations.
R
```




    array([23,  4,  6])




```python
pos1 = np.array([0,0,0])
pos2 = np.array([0,1,3])
pos3 = np.array([1,5,-2])
pos4 = np.array([5,-3,3])
sumR = pos1 + pos2 + pos3 + pos4
productR = np.multiply(pos3, pos4)
R = pos3 / pos4
R

print("sum:")
print(sumR)
print("product:")
print(productR)
print("quotient:")
print(R)
```

    sum:
    [6 3 4]
    product:
    [  5 -15  -6]
    quotient:
    [ 0.2        -1.66666667 -0.66666667]


##### Try for yourself!

Try to implement subtraction, multiplication, and division with vectors $A$ and $B$!

$$ A = \begin{bmatrix} 18 & 4&7\\ 6&10&-1\\ -9&3&-11\end{bmatrix}  B = \begin{bmatrix} 7&-11&7 \\ -3&8&-13\\ 6&1&10\end{bmatrix}
$$


```python
A = np.array([
      [18,4,7],
      [6,10,-1],
      [-9,3,-11]
])
B = np.array([
      [7,-11,7],
      [-3,8,-13],
      [6,1,10]
])

diff = np.subtract(A,B)
prod = np.multiply(A,B)
quot = A/B

print("Difference:")
print(diff)
print("Product:")
print(prod)
print("Quotient:")
print(quot)
```

    Difference:
    [[ 11  15   0]
     [  9   2  12]
     [-15   2 -21]]
    Product:
    [[ 126  -44   49]
     [ -18   80   13]
     [ -54    3 -110]]
    Quotient:
    [[ 2.57142857 -0.36363636  1.        ]
     [-2.          1.25        0.07692308]
     [-1.5         3.         -1.1       ]]


### Scaling

Scaling or scalar multiplication takes a scalar value and performs multiplication with a vector. Let's take the example below:

$$S = 5 \cdot A$$

We can do this in numpy through:



```python
S = np.multiply(5,A)
S
```




    array([20, 15])



Try to implement scaling with two vectors.



$$ C = \begin{bmatrix} 11 & 6\\ 8&10\\ -4&7\end{bmatrix}  D = \begin{bmatrix} 7&-10 \\ -3&3\\ 5&-1\end{bmatrix}
$$

$$P = 8 \ (C+D)$$


```python
C = np.array([
    [11,6],
    [8,10],
    [-4,7]
])

D = np.array([
    [7,-10],
    [-3,3],
    [5,-1]
])

P = np.multiply(8,C+D)
P
```




    array([[144, -32],
           [ 40, 104],
           [  8,  48]])



### MatPlotLib

MatPlotLib or MATLab Plotting library is Python's take on MATLabs plotting feature. MatPlotLib can be used vastly from graping values to visualizing several dimensions of data.

#### Visualizing Data

It's not enough just solving these vectors so might need to visualize them. So we'll use MatPlotLib for that. We'll need to import it first.



```python
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
```


```python
A = [8, -6]
B = [3, -3] 
plt.scatter(A[0], A[1], label='A', c='green') #A[0] is Ax , A[1] is Ay , c=color
plt.scatter(B[0], B[1], label='B', c='magenta')
plt.grid()
plt.legend()
plt.show()
```


```python
A = np.array([1, -1])
B = np.array([1, 5])
R = A + B
Magnitude = np.sqrt(np.sum(R**2))
plt.title("Resultant Vector\nMagnitude:{}" .format(Magnitude))
plt.xlim(-5, 5) #limit of x
plt.ylim(-5, 5) #limit of y
plt.quiver(0, 0, A[0], A[1], angles='xy', scale_units='xy', scale=1, color='red')
plt.quiver(A[0], A[1], B[0], B[1], angles='xy', scale_units='xy', scale=1, color='green')
plt.quiver(0, 0, R[0], R[1], angles='xy', scale_units='xy', scale=1, color='black')
plt.grid()
plt.show()
print("Resultant:")
print(R)
print("Magnitude:")
print(Magnitude)
Slope = R[1]/R[0]
print("Slope:")
print(Slope)
Angle = (np.arctan(Slope))*(180/np.pi)
print("Angle:")
print(Angle)
```


```python
n = A.shape[0]
plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.quiver(0,0, A[0], A[1], angles='xy', scale_units='xy',scale=1)
plt.quiver(A[0],A[1], B[0], B[1], angles='xy', scale_units='xy',scale=1)
plt.quiver(0,0, R[0], R[1], angles='xy', scale_units='xy',scale=1)
plt.show()
```

Try plotting Three Vectors and show the Resultant Vector as a result.
Use Head to Tail Method.

In this task, we import the matplotlib to execute the limitations of x and y axis, the grid, and quievers that are significant on plotting single or multiple vectors in the Google Colaboratory. To begin the program, we need to input a values on the arrays that will be the basis or the starting point of the vectors. Quivers are the ones that connects two points to indicate a scaling unit and its direction- the head to tail method. This is controlled by setting a point to the given values of x and y of an array, for example, A[0] is for x of vector A. These things must be labeled on different colors to avoid confusion for the joining points. Lastly, the resultant, e sum of the vectors; the magnitude, square root of the squared resultant; slope, quotient of y over x of resultant; and angle, which is the arctan of slope divided by the ratio of 180 over pie can also be observed in the given figure. 

## 2 Vectors


```python
C = np.array([-5, -3])
D = np.array([-4, 6])
R = C + D
Magnitude = np.sqrt(np.sum(R**2))
plt.title("Resultant Vector\nMagnitude:{}" .format(Magnitude))
plt.xlim(-10, 10) #limit of x
plt.ylim(-10, 10) #limit of y
plt.quiver(0, 0, C[0], C[1], angles='xy', scale_units='xy', scale=1, color='blue')
plt.quiver(C[0], C[1], D[0], D[1], angles='xy', scale_units='xy', scale=1, color='orange')
plt.quiver(0, 0, R[0], R[1], angles='xy', scale_units='xy', scale=1, color='brown')
plt.grid()
plt.show()
print("Resultant:")
print(R)
print("Magnitude:")
print(Magnitude)
Slope = R[1]/R[0]
print("Slope:")
print(Slope)
Angle = (np.arctan(Slope))*(180/np.pi)
print("Angle:")
print(Angle)
```


```python
E = np.array([8, 2])
F = np.array([1, 9])
R = E + F
Magnitude = np.sqrt(np.sum(R**2))
plt.title("Resultant Vector\nMagnitude:{}" .format(Magnitude))
plt.xlim(-15, 15) #limit of x
plt.ylim(-15, 15) #limit of y
plt.quiver(0, 0, E[0], E[1], angles='xy', scale_units='xy', scale=1, color='red')
plt.quiver(E[0], E[1], F[0], F[1], angles='xy', scale_units='xy', scale=1, color='blue')
plt.quiver(0, 0, R[0], R[1], angles='xy', scale_units='xy', scale=1, color='green')
plt.grid()
plt.show()
print("Resultant:")
print(R)
print("Magnitude:")
print(Magnitude)
Slope = R[1]/R[0]
print("Slope:")
print(Slope)
Angle = (np.arctan(Slope))*(180/np.pi)
print("Angle:")
print(Angle)
```

## 3 Vectors


```python
A = np.array([-1, 9.1])
B = np.array([-3, 2])
C = np.array([-4, -11])
R = A + B + C
Magnitude = np.sqrt(np.sum(R**2))
plt.title("Resultant Vector\nMagnitude:{}" .format(Magnitude))
plt.xlim(-15, 16) #limit of x
plt.ylim(-15, 16) #limit of y
plt.quiver(0, 0, A[0], A[1], angles='xy', scale_units='xy', scale=1, color='black')
plt.quiver(A[0], A[1], B[0], B[1], angles='xy', scale_units='xy', scale=1, color='purple')
plt.quiver(-4, 11, C[0], C[1], angles='xy', scale_units='xy', scale=1, color='brown')
plt.quiver(0, 0, R[0], R[1], angles='xy', scale_units='xy', scale=1, color='red')
plt.grid()
plt.show()
print("Resultant:")
print(R)
print("Magnitude:")
print(Magnitude)
Slope = R[1]/R[0]
print("Slope:")
print(Slope)
Angle = (np.arctan(Slope))*(180/np.pi)
print("Angle:")
print(Angle)
```

626f58929333c5fa911a7021fbd047bc53af783c
ipynb
Jupyter Notebook
Assignment_9_(Galano).ipynb
Fotrichis/LinearAlgebra_2ndSem
e9b9e2b588961c6b30ced96797643ad1ce447823
Apache-2.0
Assignment_9_(Galano).ipynb
Fotrichis/LinearAlgebra_2ndSem
e9b9e2b588961c6b30ced96797643ad1ce447823
Apache-2.0
Assignment_9_(Galano).ipynb
Fotrichis/LinearAlgebra_2ndSem
e9b9e2b588961c6b30ced96797643ad1ce447823
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES