obliczenia_naukowe/l2/3.jl
2024-11-08 17:07:02 +01:00

43 lines
1,000 B
Julia
Executable file

#!/usr/bin/env julia
# Jacek Poziemski 272389
using LinearAlgebra
include("hilb.jl")
include("matcond.jl")
struct Errors
gauss::Float64
inverse::Float64
end
function solve(A::Matrix{Float64}, n::Int)::Errors
x::Vector{Float64} = ones(Float64, n)
b::Vector{Float64} = A * x
gauss::Float64 = norm((A \ b) - x) / norm(x)
inverse::Float64 = norm((inv(A) * b) - x) / norm(x)
return Errors(gauss, inverse)
end
println("hilbert matrices")
println()
for n in 1:30
A::Matrix{Float64} = hilb(n)
errors = solve(A, n)
println("n: $n; cond: $(cond(A)); rank: $(rank(A)); gauss error: $(errors.gauss); inverse error: $(errors.inverse)")
end
println()
println("random matrices")
println()
for n in [5, 10, 20]
for c in [0, 1, 3, 7, 12, 16]
A::Matrix{Float64} = matcond(n, 10.0^c)
errors = solve(A, n)
println("n: $n; c: $c; cond: $(cond(A)); rank: $(rank(A)); gauss error: $(errors.gauss); inverse error: $(errors.inverse)")
end
end