-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumericalMethods1_1B.m
76 lines (43 loc) · 1.28 KB
/
NumericalMethods1_1B.m
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
62
63
64
65
66
67
68
69
70
%authors: Ivan Liljeqvist and Filip Martinsson
%version: 12/04/2015
%get N from user
prompt = 'Please input the number of unknowns 5<= n <= 10: ';
n = input(prompt);
%generate A and b
[A,b]=illposed(n);
%calculate x
x=A\b;
%alter an element in a with 0.001 - disturb the vector
disturbance=0.001;
%array that will hold the ratios produced in the loop
answers=[];
for j=1:n,
b_dist=b;
b_dist(j,1)=b_dist(j,1)+disturbance;
%calculate x when disturbed
x_dist=A\b_dist;
%calculate the difference between distrubed and undisturbed x
x_diff=x_dist-x;
%measure the relative disturbance in input and output
R_in = norm(b_dist-b)/norm(b);
R_out = norm(x_dist - x)/norm(x);
%calculate the ratio between R_in and R_out
ratio=R_out/R_in;
answers(1,j)=ratio;
end
%cond calculated by MATLAB
k=cond(A);
%format the answers as a table
table_contents=[]
%populate the table
for m=1:n,
table_contents(m,1)=m;
table_contents(m,2)=disturbance;
table_contents(m,3)=answers(1,m);
table_contents(m,4)=k;
end
%make GUI table
table=uitable('data',table_contents,'ColumnName',{'Element disturbed','Disturbance','(R_out/R_in)','cond'})
talbe.Position=[0 0 500 800];
%Use MATLAB's cond to calculate the answer automatically
%k=cond(A);