Site Loader

MAT 343 MATLAB LAB 2 Yu Shan
Question1.
a)
n=1000;
A=floor(10*rand(n));
b=A*z;
z=ones(n,1);
x=A
The solution of the system Ax=b is the vactor z. Because b is a vector of a.
tic,x=A;toc
Elapsed time is 0.029964 seconds.
tic,y=inv(A)*b; toc
Elapsed time is 0.414442 seconds.
sum(abs(x-z))
ans =
6.4826e-10
sum(abs(y-z))
ans =
2.1906e-09
x=Ac is the most accurate solution.
b)
n=2000;
A=floor(10*rand(n));
b=sum(A’)’;
z=ones(n,1);
x=A
x = 1.0000 *2000 columns
tic,x=A;toc
Elapsed time is 0.132001 seconds.
tic,y=inv(A)*b; toc
Elapsed time is 0.245654 seconds.
sum(abs(x-z))
ans =2.0072e-09
sum(abs(y-z))
ans =6.9459e-09
x=Ac is the most accurate solution.
n=5000;
A=floor(10*rand(n));
b=sum(A’)’;
z=ones(n,1);
x=A
x =
1.0000 *5000 columns
tic,x=A;toc
Elapsed time is 1.177055 seconds.
tic,y=inv(A)*b; toc
Elapsed time is 3.636789 seconds.
sum(abs(x-z))
ans =
8.5169e-09
sum(abs(y-z))
ans =
7.1827e-08
x=Ac is the most accurate solution.

Question2.
n=100;
A=eye(n,n)-triu(ones(n,n),1);

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

b=sum(A’)’;
z=ones(n,1);
x=A;
y=inv(A)*b;
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =
1.577722e-32.
sum(abs(x-z))
ans =
0
sum(abs(y-z))
ans =
45
x=A is the most accurate solution.

Question3.
a)
A=floor(10*rand(6));
b=floor(20*rand(6,1))-10;
x=A
x =
-2.4480
0.2038
5.9223
-7.5941
3.7118
-1.0965
b)
A=floor(10*rand(6));
b=floor(20*rand(6,1))-10;
x=A;
rref(A,b)
ans=
1.0000 0 0 0 0 0 -0.0547
0 1.0000 0 0 0 0 0.4390
0 0 1.0000 0 0 0 -1.0480
0 0 0 1.0000 0 0 0.0903
0 0 0 0 1.0000 0 0.3773
0 0 0 0 0 1.0000 0.3061
c)
A=floor(10*rand(6));
b=floor(20*rand(6,1))-10;
x=A;
U=rref(A,b);
U(:,7)-x
ans =
1.0e-05 *
0.0593
-0.1379
0.8479
-0.8620

0.3259
0.3130
d)
A=floor(10*rand(6));
b=floor(20*rand(6,1))-10;
A(:,3)=A(:,1:2)*4,3′;
rref(A b)
ans=
1 0 4 0 0 0 0
0 1 3 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 1
Since there are 6 equations and 7 variables, so there is no solution of the system.
e)

y=floor(20*rand(6,1))-10;
c=A*y;
rref(A c)
ans=
1 0 4 0 0 0 29
0 1 3 0 0 0 23
0 0 0 1 0 0 6
0 0 0 0 1 0 6
0 0 0 0 0 1 -9
0 0 0 0 0 0 0
Since there are 5 equations and 6 variables, so there are infinitely solution.

Question4.
function y=myrowproduct(A,x)
m,n=size(A);
p,q=size(x);
if(q==1;;p==n)
y=zeros(m,q);
for i=1:m
y(i)=A(i,:)*x;
end
else
disp(‘dimensions do not match’)
y=;
end
end
A=floor(10*rand(6))
x=rand(6,1)
A =
3 9 7 3 4 0
8 5 3 7 0 6
1 3 5 8 4 8
7 4 0 3 2 6
8 2 1 3 5 9
1 3 9 5 8 7
x =
0.2094

0.7514
0.1738
0.3364
0.0666
0.5861
myrowproduct(A,x)
ans =
9.8831
11.8249
10.9788
9.1303
9.9686
10.3450
A*x
ans =
9.8831
11.8249
10.9788
9.1303
9.9686
10.3450

Question5.
a)
function C=rowproduct(A,B)
m,n=size(A);
p,q=size(B);
if(p==n)
C = zeros(m,q);
for i=1:m
C(i,1:q)=A(i,:)*B;
end
else
disp(‘dimensions do not match’)
C=;
end
end

A=floor(10*rand(3,4));
B=floor(10*rand(4,5));
rowproduct(A,B)
ans =
78 58 100 86 80
74 16 54 42 48
71 87 114 88 52
A*B
ans =
78 58 100 86 80
74 16 54 42 48
71 87 114 88 52
b)
function C=columproduct(A,B)
m,n=size(A);

p,q=size(B);
if(p==n)
C = zeros(m,q);
for i=1:q
C(1:m,i)=A*B(:,i);
end
else
disp(‘dimensions do not match’)
C=;
end
end
columproduct(A,B)
ans =
78 58 100 86 80
74 16 54 42 48
71 87 114 88 52

Post Author: admin

x

Hi!
I'm Chloe

Would you like to get a custom essay? How about receiving a customized one?

Check it out
x

Hi!
I'm Ted!

Would you like to get a custom essay? How about receiving a customized one?

Check it out