## Create the matrix
A = [
4 9 6 0 4 5 3.0
4 9 4 1 9 9 7
4 9 4 6 0 1 0
4 9 6 1 7 6 1
4 9 4 8 7 9 8
4 9 6 2 3 9 9
4 9 4 6 0 1 3
4 9 6 9 4 3 2
4 9 4 9 0 2 5
4 9 4 6 0 0 5
4 9 4 6 0 0 9
]
11×7 Matrix{Float64}: 4.0 9.0 6.0 0.0 4.0 5.0 3.0 4.0 9.0 4.0 1.0 9.0 9.0 7.0 4.0 9.0 4.0 6.0 0.0 1.0 0.0 4.0 9.0 6.0 1.0 7.0 6.0 1.0 4.0 9.0 4.0 8.0 7.0 9.0 8.0 4.0 9.0 6.0 2.0 3.0 9.0 9.0 4.0 9.0 4.0 6.0 0.0 1.0 3.0 4.0 9.0 6.0 9.0 4.0 3.0 2.0 4.0 9.0 4.0 9.0 0.0 2.0 5.0 4.0 9.0 4.0 6.0 0.0 0.0 5.0 4.0 9.0 4.0 6.0 0.0 0.0 9.0
## Create a simple instance of our least squares problem
using Random
n = 25
Random.seed!(1) # make it repeatable
x = 10.0.*rand(n).-5.0
y = 3.0*x .- 2.0 + 2.0*randn(n)
25-element Vector{Float64}: -15.030093558609678 -15.635224716068214 -4.432325717254104 -13.972621198585301 5.666345397007744 5.521161061257487 -14.155528382068526 10.336019581896505 8.54940247997104 10.351905031777392 -10.181144678846753 -7.537226095628984 -10.612359946231168 -13.2763812264547 2.3003576088972713 6.718984191912116 -0.8725471141813601 -6.864478717275617 12.01124720512554 -13.767221727527021 14.180858152288002 -10.985476465629734 10.437486778965752 11.874125755329054 8.85313113613898
## Make a plot
using Plots
pyplot(dpi=300, size=(600,600))
scatter(x, y, label="data")
gui()
ArgumentError: Package PyPlot not found in current path. - Run `import Pkg; Pkg.add("PyPlot")` to install the PyPlot package. Stacktrace: [1] macro expansion @ ./loading.jl:2296 [inlined] [2] macro expansion @ ./lock.jl:273 [inlined] [3] __require(into::Module, mod::Symbol) @ Base ./loading.jl:2271 [4] #invoke_in_world#3 @ ./essentials.jl:1089 [inlined] [5] invoke_in_world @ ./essentials.jl:1086 [inlined] [6] require(into::Module, mod::Symbol) @ Base ./loading.jl:2260 [7] top-level scope @ ~/.julia/packages/Plots/FFuQi/src/backends.jl:883 [8] eval @ ./boot.jl:430 [inlined] [9] _initialize_backend(pkg::Plots.PyPlotBackend) @ Plots ~/.julia/packages/Plots/FFuQi/src/backends.jl:882 [10] backend(pkg::Plots.PyPlotBackend) @ Plots ~/.julia/packages/Plots/FFuQi/src/backends.jl:245 [11] #pyplot#323 @ ~/.julia/packages/Plots/FFuQi/src/backends.jl:86 [inlined] [12] top-level scope @ In[3]:3
##
## Save the picture
pyplot(size=(250,250)) # this is a 2.5-by-2.5 inch picture, suitable for a small graphic
scatter(x, y, label="data")
savefig("least-squares-example-1.pdf")
ArgumentError: Package PyPlot not found in current path. - Run `import Pkg; Pkg.add("PyPlot")` to install the PyPlot package. Stacktrace: [1] macro expansion @ ./loading.jl:2296 [inlined] [2] macro expansion @ ./lock.jl:273 [inlined] [3] __require(into::Module, mod::Symbol) @ Base ./loading.jl:2271 [4] #invoke_in_world#3 @ ./essentials.jl:1089 [inlined] [5] invoke_in_world @ ./essentials.jl:1086 [inlined] [6] require(into::Module, mod::Symbol) @ Base ./loading.jl:2260 [7] top-level scope @ ~/.julia/packages/Plots/FFuQi/src/backends.jl:883 [8] eval @ ./boot.jl:430 [inlined] [9] _initialize_backend(pkg::Plots.PyPlotBackend) @ Plots ~/.julia/packages/Plots/FFuQi/src/backends.jl:882 [10] backend(pkg::Plots.PyPlotBackend) @ Plots ~/.julia/packages/Plots/FFuQi/src/backends.jl:245 [11] #pyplot#323 @ ~/.julia/packages/Plots/FFuQi/src/backends.jl:86 [inlined] [12] top-level scope @ In[5]:2
## Create the matrix A
A = [ones(n) x]
c = A \ y # solve the least squares problem
2-element Vector{Float64}: -1.3357028961897217 2.919346646806541
## Add the line
plot!(x, x -> c[1] + c[2]*x, label="fit")
gui()
## Try the same with the quadratic
n = 25
Random.seed!(1) # make it repeatable
x = 10.0.*rand(n).-5.0
y = -0.5*x.^2 + 3.0*x .- 2.0 + 2.0*randn(n)
scatter(x, y, label="data")
gui()
##
A = [ones(n) x x.^2]
c = A \ y # solve the least squares problem
3-element Vector{Float64}: -1.7001802095000254 2.921023818202273 -0.47110904925752056