#-------------------------------------
# Lecture 9 -- Practice 1. Arrays
#-------------------------------------

import numpy as np

# (i) Suppose we have an array of positions, x, that represent a
# numerical grid. Find the array that gives the value of the grid spacings,
# dx = x_{i+1} - x_{i}. Output the array dx.
# array dx of differences between positions. Find the array of dif

x = np.array([0.1, 0.2, 0.4, 0.8, 1.5, 5.5, 6.0, 6.5, 6.8, 7.0])

# (Answer)
dx = x[1:] - x[0:-1] # dx only has 9 elements
print(dx)


#%%
# (ii) Suppose that I am given a matrix A, a vector b and a purported solution,
# x. Verify that x is indeed the solution to the linear equation A.x = b.

A = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([1,2,3])
x = np.array([-7/30, 7/15, 1/10])

# (Answer)
b_check = np.dot(A, x)
print('error: ', b - b_check)