In [1]:
## Least squares with syntax explained.

using Plots
plot(randn(5), randn(5))
Out[1]:
In [2]:
##
using Random 
Random.seed!(0)
n = 5000
x = round.(range(1, 5.5, length=n) .+ 5.0*(2*rand(n).-1),digits=2)
y = round.(5*x.+1 .+ 3*(2*rand(n).-1), digits=2)
scatter(x,y, legend=false)
xlabel!("x")
ylabel!("y")
Out[2]:
In [3]:
## We work out why this is the right matrix to use in the notes 
A = [ones(n) x]
b = y
c = A\b # this solves a linear system or least squares problem
# it solve a linear system if A is square
# or it's least squares if A is not square
Out[3]:
2-element Vector{Float64}:
 1.0177060382730165
 4.992531862834267
In [4]:
## Let's show an example of broad casting
function cs515fun(x)
  return x^2 + 2x + 1
end
cs515fun.(x)
Out[4]:
5000-element Vector{Float64}:
   2.4025
   6.150399999999999
  22.467600000000004
  41.0881
   7.290000000000001
  19.980900000000002
  19.980900000000002
  45.832899999999995
   0.09000000000000008
  13.9129
   3.9601000000000006
   0.47609999999999975
  11.0224
   ⋮
  15.21
  69.8896
  93.31559999999999
  21.529600000000002
  87.98440000000002
   9.3636
  78.8544
   6.7081
  40.32249999999999
  20.1601
  87.4225
 106.09