Uploaded by Rieme Anetor

computer class 1

advertisement
Write a loop which changes the value of n to n = 2k for k = 7, 8, 9, 10, 11, 12, 13 and records the
times taking to solve each linear system.
clear all;
for k= 7:13
n=2.^k;
A=rand(n,n);
b=rand(n,1);
tic;
x=A\b;
toc;
end
(You should use the help of MATLAB to understand how to change the tic; and toc; syntax
appropriately to be able to save the time for each iteration of the loop.)
What do you observe with the time it takes to solve the problem as a function of the size n? What
happens when n becomes very large? Why?
Elapsed time is 0.000399 seconds.
Elapsed time is 0.001071 seconds.
Elapsed time is 0.002845 seconds.
Elapsed time is 0.018826 seconds.
Elapsed time is 0.088912 seconds.
Elapsed time is 0.438329 seconds.
Elapsed time is 2.326705 seconds.
Task 2
Write a loop which changes the value of n to n = 2k for k = 7, 8, 9, 10, 11, 12, 13 and records the
times taking to solve each linear system.
clear all;
for k=7:13
n=2.^k
e=ones(n,1);
A=diag(-2.*e,0)+diag(e(1:n-1),1)+diag(e(1:n-1),-1);
b=rand(n,1);
tic;
x=A\b;
toc;
end
Command window
n=
128
Elapsed time is 0.001117 seconds.
n=
256
Elapsed time is 0.001677 seconds.
n=
512
Elapsed time is 0.001909 seconds.
n=
1024
Elapsed time is 0.010735 seconds.
n=
2048
Elapsed time is 0.038848 seconds.
n=
4096
Elapsed time is 0.230925 seconds.
n=
8192
Elapsed time is 1.203014 seconds.
>>
What do you observe with the time it takes to solve the problem compared to the example above?
For which value of k the problem becomes too big to be solved in the computer you are at?
clear all;
for k=14:23
n=2.^k
e=ones(n,1);
A=diag(-2.*e,0)+diag(e(1:n-1),1)+diag(e(1:n-1),-1);
b=rand(n,1);
tic;
x=A\b;
toc;
end
n=
16384
Elapsed time is 7.598612 seconds.
n=
32768
Elapsed time is 61.170771 seconds.
n=
65536
Error using diag
Requested 65536x65536 (32.0GB) array exceeds maximum array size
preference (15.9GB). This might cause MATLAB to become
unresponsive.
Error in tasktwoccone (line 5)
A=diag(-2.*e,0)+diag(e(1:n-1),1)+diag(e(1:n-1),-1);
Related documentation
>>
Task 3
Write a loop which changes the value of n to n = 2k for k = 7, 8, . . . , 20 and records the times taking
to solve each linear system.
clear all;
for k=7:20
n=2.^k
e = ones(n,1);
A = spdiags([e -2*e e], -1:1, n, n);
b=rand(n,1);
tic;
x=A\b;
toc;
end
Command window
n=
128
Elapsed time is 0.000131 seconds.
n=
256
Elapsed time is 0.000055 seconds.
n=
512
Elapsed time is 0.000054 seconds.
n=
1024
Elapsed time is 0.000064 seconds.
n=
2048
Elapsed time is 0.000710 seconds.
n=
4096
Elapsed time is 0.000103 seconds.
n=
8192
Elapsed time is 0.000130 seconds.
n=
16384
Elapsed time is 0.000276 seconds.
n=
32768
Elapsed time is 0.000536 seconds.
n=
65536
Elapsed time is 0.001082 seconds.
n=
131072
Elapsed time is 0.002527 seconds.
n=
262144
Elapsed time is 0.005079 seconds.
n=
524288
Elapsed time is 0.009592 seconds.
n=
1048576
Elapsed time is 0.019181 seconds.
>>
What do you observe with the time it takes to solve the problem compared to the example above?
For which value of k the problem becomes too big to be solved in the computer you are at?
n=
2097152
Elapsed time is 0.039202 seconds.
n=
4194304
Elapsed time is 0.085932 seconds.
n=
8388608
Elapsed time is 0.156133 seconds.
n=
16777216
Elapsed time is 0.314907 seconds.
n=
33554432
Elapsed time is 0.702602 seconds.
n=
67108864
Elapsed time is 1.480248 seconds.
n=
134217728
Elapsed time is 3.965629 seconds.
n=
268435456
Out of memory.
Error in spdiags>makeSparsePresorted (line 199)
A = sparse(I, J, V, m, n);
Error in spdiags (line 116)
res1 = makeSparsePresorted(B, d, m, n);
Error in taskthreeccone (line 5)
A = spdiags([e -2*e e], -1:1, n, n);
Related documentation
>>
Why does this happen in view of the results from Task 2?
Task 4
Use the command inv to invert the matrix A constructed in Task 2 for n = 10.
clear all;
n=10
e=ones(n,1);
A=diag(-2.*e,0)+diag(e(1:n-1),1)+diag(e(1:n-1),-1);
Y= inv(A);
Is this a full or a sparse matrix in terms of the number of zeros?
A=
-2
1
0
0
0
0
0
0
0
0
1 -2
1
0
0
0
0
0
0
0
0
1 -2
1
0
0
0
0
0
0
0
0
1 -2
1
0
0
0
0
0
0
0
0
1 -2
1
0
0
0
0
0
0
0
0
1 -2
1
0
0
0
0
0
0
0
0
1 -2
1
0
0
0
0
0
0
0
0
1 -2
1
0
0
0
0
0
0
0
0
1 -2
1
0
0
0
0
0
0
0
0
1 -2
>> Y
Y=
Columns 1 through 6
-0.9091 -0.8182 -0.7273 -0.6364 -0.5455 -0.4545
-0.8182 -1.6364 -1.4545 -1.2727 -1.0909 -0.9091
-0.7273 -1.4545 -2.1818 -1.9091 -1.6364 -1.3636
-0.6364 -1.2727 -1.9091 -2.5455 -2.1818 -1.8182
-0.5455 -1.0909 -1.6364 -2.1818 -2.7273 -2.2727
-0.4545 -0.9091 -1.3636 -1.8182 -2.2727 -2.7273
-0.3636 -0.7273 -1.0909 -1.4545 -1.8182 -2.1818
-0.2727 -0.5455 -0.8182 -1.0909 -1.3636 -1.6364
-0.1818 -0.3636 -0.5455 -0.7273 -0.9091 -1.0909
-0.0909 -0.1818 -0.2727 -0.3636 -0.4545 -0.5455
Columns 7 through 10
-0.3636 -0.2727 -0.1818 -0.0909
-0.7273 -0.5455 -0.3636 -0.1818
-1.0909 -0.8182 -0.5455 -0.2727
-1.4545 -1.0909 -0.7273 -0.3636
-1.8182 -1.3636 -0.9091 -0.4545
-2.1818 -1.6364 -1.0909 -0.5455
-2.5455 -1.9091 -1.2727 -0.6364
-1.9091 -2.1818 -1.4545 -0.7273
-1.2727 -1.4545 -1.6364 -0.8182
-0.6364 -0.7273 -0.8182 -0.9091
>>
Do you think it’s preferable to invert the matrix or use Gaussial elimination if we want to solve many
linear systems of the form Ax = b?
Download