In [5]:
using PyPlot

Aufgabe 1

gauss1.jl:


In [24]:
N = 10
A = rand(N,N)
A[1,1] = 0.99
A[1,2] = 0.01

b = rand(N)

Ap1 = 1.0*A
bp1 = 1.0*b

imshow(mod(A+0.00001,1), cmap="gray", interpolation="none")

for n in 1:(N-1)
  for i in (n+1):N
    factor = A[i,n]/A[n,n]
    for j in 1:N
      Ap1[i,j] = A[i,j] - factor*A[n,j]
    end
    bp1[i] = b[i] - factor*b[n]
  end
  A = 1.0*Ap1
  b = 1.0*bp1
  sleep(1.0)
  imshow(mod(A+0.00001,1), cmap="gray", interpolation="none")
end


Teil a

$x_i = \frac{b_i-\sum^N_{j=i+1}A^{(N-1)}_{i,j}x_j}{A^{(N-1)}_{i,i}}$ mit $A^{(N-1)}_{i,j} = \begin{pmatrix} A^{(N-1)}_{1,1} & A^{(N-1)}_{1,2} & \dots & A^{(N-1)}_{1,j} \\ 0 & A^{(N-1)}_{2,2} & \dots & A^{(N-1)}_{2,j} \\ \vdots & 0 & \ddots & \vdots \\ 0 & \dots & 0 & A^{(N-1)}_{i,j} \end{pmatrix}$

Teil b


In [34]:
x = Array(Float64, N)
for i in N:-1:1
    sum = 0
    for j in i+1:N
        sum += A[i,j]*x[j]
    end
    x[i] = (b[i]-sum)/A[i,i]
end

Teil c


In [41]:
N = 3
A = Array{Float64}([1 -2 2; 2 1 -4;-1 -1 2])
b = Array{Float64}([1 0 3])

# gauss1.jl
Ap1 = 1.0*A
bp1 = 1.0*b
for n in 1:(N-1)
  for i in (n+1):N
    factor = A[i,n]/A[n,n]
    for j in 1:N
      Ap1[i,j] = A[i,j] - factor*A[n,j]
    end
    bp1[i] = b[i] - factor*b[n]
  end
  A = 1.0*Ap1
  b = 1.0*bp1
end

# Teil b 
x = Array(Float64, N)
for i in N:-1:1
    sum = 0
    for j in i+1:N
        sum += A[i,j]*x[j]
    end
    x[i] = (b[i]-sum)/A[i,i]
end

x


Out[41]:
3-element Array{Float64,1}:
 -4.0
 -6.0
 -3.5

In [ ]: