obliczenia_naukowe/l2/matcond.jl

23 lines
584 B
Julia
Raw Permalink Normal View History

2024-11-08 17:07:02 +01:00
# https://cs.pwr.edu.pl/zielinski/lectures/scna/matcond.jl
using LinearAlgebra
function matcond(n::Int, c::Float64)
# Function generates a random square matrix A of size n with
# a given condition number c.
# Inputs:
# n: size of matrix A, n>1
# c: condition of matrix A, c>= 1.0
#
# Usage: matcond(10, 100.0)
#
# Pawel Zielinski
if n < 2
error("size n should be > 1")
end
if c< 1.0
error("condition number c of a matrix should be >= 1.0")
end
(U,S,V)=svd(rand(n,n))
return U*diagm(0 =>[LinRange(1.0,c,n);])*V'
end