You are on page 1of 10

MAT 275 MATLAB

Name : Dana Othman Almufarrj

LAB # 2

Exercise 1

a)

>> A= [5 -1 3;2 4 -7; 6 1 8] % assign the matrix A

A=

5 -1 3

2 4 -7

6 1 8

>> B=[12 0 7;3 -2 5; -1 9 10] % assign the matrix B

B=

12 0 7

3 -2 5

-1 9 10

>> b=[16;36;17] % assign the matrix b


b=

16

36

17

>> >> c=[1 2 3] % assign the matrix c

c=

1 2 3

>> d=[4; 3; 2] % assign the matrix d

d=

b)

>> A.*B %multiply the matrix

ans =
60 0 21

6 -8 -35

-6 9 80

>> B.*A %multiply the matrix

ans =

60 0 21

6 -8 -35

-6 9 80

>> c>> c*A %multiply the matrix

ans =

27 10 13

>> B*d %multiply the matrix

ans =

62

16
43

c)

>> C=[A; B] % assign the matrix C

C=

5 -1 3

2 4 -7

6 1 8

12 0 7

3 -2 5

-1 9 10

>> D=[B d] % assign the matrix D

D=

12 0 7 4

3 -2 5 3

-1 9 10 2

d)
>> x=A\b % solve for Ax=b

x=

5.0000

3.0000

-2.0000

e)

>> A(3,2)=0 %assign the 0 to A(3,2)

A=

5 -1 3

2 4 -7

6 0 8

f)

>> a=A(2,:) % display the 2nd row of A

a=

2 4 -7
g)

>> B(:,3)=[] % empty the 3 column of B

B=

12 0

3 -2

-1 9

% Exercise 2 a

>>function S=sum1(r,a,n) %design the function


S=0;
for k=1:n
S=S+a*r^(k-1); % define the value of sum
end
S;
end

o/p
>> A=sum1(1/3,5,8)

A=

7.4989
% Exercise 2 b

function S=A1(r,a,n)
B(1)=a;
for e=0:n-1
R=r.^e;
B(e)=a.*R;
end
S=sum(B);
end

o/p

>> S=A1(1/3,5,8)

S=

7.4989

% Exercise 3 a

function A=produc(n) % design the function

p = 1; % initialize running product


for k = 2:n
if rem(k,2)~=0
p = p*k; %assign the value to function
end
end
A=p;
end
A=produc(15)

A >> A=produc(15)
A=

2027025

% Exercise 3b
function A=pro(n)

k=1;
p = 1; % initialize running product
for i = 1:n
if rem(i,2)~=0
p(k) = i;
k=k+1;
end
end
A=prod(p);
end

>> A

A=

2027025

% Exercise 4
function V=pow1(n) % design the function
i=1;
a=0;
V(1)=3;

while a<n % if power is less than 3000

i=i+1;
V(i)=3.^i; % assign the value
a=V(i);
end

V;
end
o/p

>> V=pow1(3000)

V=

3 9 27 81 243 729 2187

% Exercise 5
function val=value(x) % design the function

if x==7
disp('the function is undefined at x = 7')
else
if x<=2 %apply the conditions for x
value=exp(x-1);
else
if x<=4 %apply the conditions for x
value=x.^2+x;
else
if x>4 %apply the conditions for x
value=x/(x-7);
end
end
end
end
val =value;
end

>> value(1)

ans =
1

>> value(2)

ans =

2.7183

>> value(3)

ans =

12

>> value(4)

ans =

20

>> value(7)

the function is undefined at x = 7

You might also like