Lebanese University M2207 2017-2018
Faculty of Sciences (I) MATLAB Sheet 1
Exercise 1 Give the result that we obtain by executing the following MATLAB instructions :
>> x = linspace(0,2,100) >> A = [1 2 3;4 5 6]
>> a = 10:-1:0 >> B = A.^2
>> b = -1:0.1:1 >> C = B’
>> y = x’ >> [m n] = size(A)
>> y = x.^2 >> M = [2 -1 0;3 1 1;0 5 2]
>> m = max(x) >> d = det(M)
>> L = length(x) >> U = inv(M)
>> plot(x,y,’r-*’,x,z,’g-s’); >> V = M*U
Exercise 2 Write a MATLAB function
function[y]=myFactorial(n)
which takes as input a positive integer n and returns the factorial of n, i.e.
y = n! = n ∗ (n − 1) ∗ (n − 2) ∗ · · · ∗ 1
Exercise 3 Write a MATLAB function
function[s, p]=mySumProd(v)
which takes as input a vector v and returns the sum s and the product p of the components of v,
X Y
s= vk , p= vk
k k
Exercise 4 Write a MATLAB function
function[s]=mySign(x)
which takes as input a real number x and returns 1 if x > 0, −1 if x < 0 and 0 if x = 0.
Exercise 5 Run the following MATLAB script that calls the above three MATLAB functions.
clear all;
clc;
y = myFactorial(5);
disp(y);
v = [1 3 -2 5 4 -3];
[s,p]=mySumProd(v)
for k=1:length(v)
s = mySign(v(k))
end