Sie sind auf Seite 1von 3

Exercise 1

clc
% Create matrix A, B, b, c, d
A=[1,4,2;2,5,8;3,6,9]
B=[1,2,3;4,5,6;7,8,9]
b=[4;23;27]
c=[4,3,2]
d=[1;2;3]
% (a) Perform the operations AB,BA,cB,Ad
AB=A*B
BA=B*A
cB=c*B
Ad=A*d
%(b) Construct a 3x6 matrix C and 4x3 matrix D
C=[A,B]
D=[B;c]
%(c) Solve system Ax=b
x= A\b
% (d) Replace A(2,3)= 0
A(2,3)=0
% (e)Extract the 3rd row of the matrix A
row3=A(3,:)
% (f) Delete second row of the matrix A and third row of the
matrix B
A(2,:)=[]
B(3,:)=[]
Exercise 2
(a)
function S = Exercise2a(a,r,n)
% Tesst : At workspace type : [S] = Exercise2a( 3,1/2,10 )
S=0;
% initialize running sum
for k = 0:n-1
S = S + [a*(r^k)]; % Use loop for
end
Ans : S =
5.9941

(b)
function S = Exercise2b( a,r,n )
% Tesst : At workspace type : [S] = Exercise2b( 3,1/2,10 )
e=0:n-1;
R=r.^e;
S = a*R;
S = sum(S);
end
Ans : S = 5.9941
Exercise 3
% (a) evaluates the product of the first 10 odd numbers using a
for loop
clc
a = 1
for i = 1:2:20
a = a*i
end
%product of the first 10 odd numbers
b=a
% Final result
% (b) Use the MATLAB command prod.
p = prod(1:2:20)
Exercise 4
clc
% clear screen
y=[]
% initialize the vector y to the empty vector
value=2
% initialize the test value to be added to the
vector y
while value <1000
% Stop condition : "below 1000"
y=[y, value]
% augment the vector y
value=value*2
end
y
Ans : y =
2

16

32

64 128 256 512

Exercise 5
function y = f(x)
if x==10
disp ('the function is undefined at x = 10') % In case x = 10
elseif x<=3
y= 1+x^2; % In case x < =3
elseif x>5
y= x/(x-10); % In case x >5
else
y=exp(x);
% In case 3<= x <=5
end
Ans : f(1) = 2 , f(4) = 54.5982 , f(7) = -2.3333
f(10) = the function is undefined at x = 10

Das könnte Ihnen auch gefallen