using LinearAlgebra
Copyright By PowCoder代写 加微信 powcoder
1. QR, reduced QR and least squares¶
m,n = 20,5
A = randn(m,n)
Q,R̂ = qr(A)
R = [R̂; zeros(m-n,n)]
Q̂ = Q[:,1:n]
Q*R ≈ Q̂*R̂ ≈ A
b = randn(m)
5-element Vector{Float64}:
0.13172727086676028
0.0606694290513271
0.17053012521313177
-0.16270227847195728
0.15477983752319258
norm(A*x – b)
4.142558068680029
2. Quadratic fit¶
using Plots
m,n = 100,3
x = range(0, 1; length=m) # 100 evenly spaced points between 0 and 1 (inclusive)
f = 2 .+ x .+ 2x.^2 .+ 0.1 .* randn.() # adds different noise to each sample
scatter(x, f; label=”samples”, legend=:bottomright)
A = [ones(m) x x.^2]
Q,R̂ = qr(A)
R = [R̂; zeros(m-n,n)]
Q̂ = Q[:,1:n]
a,b,c = R̂\(Q̂’f)µ
3-element Vector{Float64}:
1.9499381961862148
1.2436649102021584
1.7909459006599178
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com