MAT 275 MATLAB Assignment Lab2
MAT 275 MATLAB Assignment Lab2
LAB # 2
Exercise 1
a)
A=
5 -1 3
2 4 -7
6 1 8
B=
12 0 7
3 -2 5
-1 9 10
16
36
17
c=
1 2 3
d=
b)
ans =
60 0 21
6 -8 -35
-6 9 80
ans =
60 0 21
6 -8 -35
-6 9 80
ans =
27 10 13
ans =
62
16
43
c)
C=
5 -1 3
2 4 -7
6 1 8
12 0 7
3 -2 5
-1 9 10
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=
5 -1 3
2 4 -7
6 0 8
f)
a=
2 4 -7
g)
B=
12 0
3 -2
-1 9
% Exercise 2 a
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
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;
i=i+1;
V(i)=3.^i; % assign the value
a=V(i);
end
V;
end
o/p
>> V=pow1(3000)
V=
% 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)