-
Notifications
You must be signed in to change notification settings - Fork 0
/
EX1-Session03-UO265946.py
61 lines (49 loc) · 1.75 KB
/
EX1-Session03-UO265946.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 4 19:25:11 2021
@author: Sara fernández arias UO269546
"""
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(precision = 2) # only two decimals
np.set_printoptions(suppress = True) # do not use exponential format
#%%Funtion definition.
def triangular(A,b):
At = np.copy(A)
bt = np.copy(b)
dig=len(At) #number of elements in main diagonal
for i in range(0,dig-1):
# f will be the number we'll have to multiply row i, in order to make the element in row i+1=0-
f=At[i+1][i]/At[i][i]
# obtaining 0 under the main diagonal[element in At[i+1][i]=0]
At[i+1][i]= At[i+1][i]-f*( At[i][i])
bt[i+1]=bt[i+1]-f*(bt[i])#matrix b changes as well.
#changes in the next element of the main diagonal due to the modification of that row in order to obtain 0 below main diagonal.
At[i+1][i+1]= At[i+1][i+1]- f*(At[i][i+1])
return At,bt#returning the result of the triangularization
#%% plotting.
print('------------DATA------------')
n = 7
A1 = np.diag(np.ones(n))*3
A2 = np.diag(np.ones(n-1),1)
A = A1 + A2 + A2.T
b = np.arange(n,2*n)*1.
print("A:",A)
print("b:",b)
print('------------ TRIANGULAR SYSTEM------------')
At,bt= triangular(A,b)
print("A:",At)
print("b:",bt)
print('------------DATA------------')
n=8
np.random.seed(3)
A1 = np.diag(np.random.rand(n))
A2 = np.diag(np.random.rand(n-1),1)
A = A1 + A2 + A2.T
b = np.random.rand(n)
print("A:",A)
print("b:",b)
print('------------ TRIANGULAR SYSTEM------------')
At,bt= triangular(A,b)
print("A:",At)
print("b:",bt)