Laboratory Exercise-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Republic of the Philippines

Naval, Biliran
SCHOOL OF ENGINEERING

LABORATORY ACTIVITY 1
Instruction: Please perform the following problem using Mat Lab software. Write the answer on your
bluebook and analyze how you arrive to the answer. Please also provide for the graph if being required.
For the proof, please screen shot your answer with your name as comment. Please create your folder
(ex. Dacuya, Jonicio) on this link
https://drive.google.com/drive/folders/1PRsQcnqHerPb8fWKTY5Os5Ai4nqPQ2nh?usp=sharing.
and upload your file in your own folder.

Sample Output:

Blue Book
1. What is the output?

a = 3;
b = 9;
c = 2*a+b^2-a*b+b/a-10

Explanation:
The right hand side of that statement is first evaluated: r.h.s. = 2 x 3 + 92 - 3 x 9 + 9/3 - 10 = 53.

Sample Output:

Course: Numerical Methods Laboratory


Program: BSEE 3A
Instructor: Jonicio A. Dacuya, CpE
Republic of the Philippines

Naval, Biliran
SCHOOL OF ENGINEERING
1. What is the output?

a = 3;
b = a*a;
c = a*a*a;
d = sqrt(a);
fprintf('%4u square equals %4u \r', a, b)
fprintf('%4u cube equals %4u \r', a, c)
fprintf('The square root of %2u is %6.4f \r', a, d)

2. What is the output?

a = [3 6 7];
b = [1 9 4];
c = a + b

3. What is the output?

a = [3 6 7];
b = [1 9 4 5];
c = a(2) + b(4)

4. What is the output? Please plot your answer.

x = pi;
y = sin(pi/2)
z = exp(-sin(pi/2))
plot(y,z)

5. What is the output? Please plot your answer.

x = [0:0.1:20];
y = sin(x);
plot(x,y)

BASIC LOOPING

6. What is the output?

b = 3;
for k = 1:5
b
end

7. What is the output?

b = 3;
for k = 1:5
b^k
end

8. What is the output?

b = 3;
for k = 1:5
b^k+3
end

9. What is the output?

sum1 = 0;
for k = 1:9

Course: Numerical Methods Laboratory


Program: BSEE 3A
Instructor: Jonicio A. Dacuya, CpE
Republic of the Philippines

Naval, Biliran
SCHOOL OF ENGINEERING
sum1 = sum1+k;
end
sum1

10. What is the output?

sum1 = 0;
for k = 1:2:9
sum1 = sum1+k;
end
sum1

11. What is the output?

sum1 = 0;
for k = 1:2:9
sum1 = sum1+k;
end
sum1

12. What is the output?

b = [3 8 9 4 7 5];
sum1 = 0;
for k = 1:4
sum1 = sum1+b(k);
end
sum1

13. What is the output?

sum1 = 0;
for n = 1:2
for m = 1:3
sum1 = sum1+n*m;
end
end
sum1

14. What is the output?


for n = 1:2
for m = 1:3
fprintf('n = %3u m = %3u \r', n, m)
end
end

15. What is the output?

b = [2 5 7 4 9 8 3];
c = [2 3 5 7];
sum1 = 0;
for k = 1:4
sum1 = sum1+b(c(k));
end
sum1

Basic Branching

16. What is the output?


num1 = 7;
if (num1 > 5)
fprintf('%4u is greater than 5 \r', num1)
else
fprintf('%4u is less than or equal to 5 \r', num1)

Course: Numerical Methods Laboratory


Program: BSEE 3A
Instructor: Jonicio A. Dacuya, CpE
Republic of the Philippines

Naval, Biliran
SCHOOL OF ENGINEERING
end

17. What is the output?

num1 = 4;
if (num1 >= 5)
fprintf('%4i is greater than or equal to 5 \r', num1)
elseif (num1 > 1)
fprintf('%4i is less than 5 but greater than 1 \r', num1)
elseif (num1 == 1)
fprintf('%4i equals 1 \r', num1)
elseif (num1 > -3)
fprintf('%4i is less than 1 but greater than -3 \r', num1)
else
fprintf('%4i is less than or equal to -3 \r', num1)
end

18. What is the output?

nyear = 1975;
if (mod(nyear, 400) == 0)
fprintf('%6u is a leap year', nyear)
elseif (mod(nyear,4) == 0) & (mod(nyear,100) ~= 0)
fprintf('%6u is a leap year', nyear)
else
fprintf('%6u is not a leap year', nyear)
end

COMBINE LOOPING AND BRANCHING

19. What is the output?

sum1 = 0;
sum2 = 0;
N = 9
for k = 1:N
sum1 = sum1+k;
if (mod(k,3) == 0)
sum2 = sum2+k;
end
end
sum1
sum2

20. What is the output?

x = 3;
while (x < 100)
x = x*3;
end
x

MATRIX OPERATION

21. What is the output?

a = [2 12 25];
b = [3 7 4];
c = a+b

22. What is the output?

a = [3 4; 1 6];
b = [5 2; 11 7];
c = a+b

Course: Numerical Methods Laboratory


Program: BSEE 3A
Instructor: Jonicio A. Dacuya, CpE
Republic of the Philippines

Naval, Biliran
SCHOOL OF ENGINEERING
23. What is the output?

a = [3 5; 1 4];
b = 2*a

24. What is the output?

a = [2 3 5];
b = [2 4 9];
c = a.*b

25. What is the output?

a = [2 3; 1 4];
b = [5 1; 7 2];
c = a.*b

26. What is the output?

a = [2 3 5];
b = sin(a)

27. What is the output?

a = [2 3 5];
b = 2*a.^2+3*a+4

28. What is the output?

A = [3 5; 2 4];
c = A(2,2)+A(1,2)

29. What is the output?

A = [3 5; 2 4];
norm1 = 0;
for m = 1:2
for n = 1:2
norm1 = norm1+A(m,n)^2;
end
end
norm1 = sqrt(norm1)

30. What is the output?

A = [4 1 2; 0 3 1; 0 1 2];
b = [17 ; 19 ; 13];
x = inv(A)*b

MISCELLANEOUS EXERCISES

31. Perform the following.


Let x = [2 5 1 6]. Add 16 to each element Add 3 to just the odd-index elements Compute the square root
of each element Compute the square of each element

32-33. Perform the following.


Let x = [3 2 6 8]’ and y = [4 1 3 5]’ (NB. x and y should be
column vectors).
(a). Add the sum of the elements in x to y
(b). Raise each element of x to the power specified by
the corresponding element in y.
(c). Divide each element of y by the corresponding
element in x
(d). Multiply each element in x by the corresponding

Course: Numerical Methods Laboratory


Program: BSEE 3A
Instructor: Jonicio A. Dacuya, CpE
Republic of the Philippines

Naval, Biliran
SCHOOL OF ENGINEERING
element in y, calling the result z.
(e). Add up the elements in z and assign the result to a
variable called w.
(f). Compute x’*y - w and interpret the result

34. Perform the following.


Given a vector, t, of length n, write down the MATLAB
expressions that will correctly compute the following:
(a). ln(2 + t + t2)
(b). et(1 + cos3t)
(c). cos2t + sin2t

35. Perform the following.

Given the array A = [ 2 4 1 6 7 2 3 5 9], provide the commands


needed to
(a). replace the second element by 0
(b). replace the even indexed elements by [0 4 8 12]
(c). remove third element

36-37. Perform the following.

Given the arrays A = [ 2 3 7 8 4 1 9] and B=[2 4 6] provide


the commands needed to
(a). merge A and B horizontally
(b). insert the value 10 between A(4) and A(5)
(c). insert an array [2 5 10] between A(3) and A(4)
(d). merge A and B vertically

38-39. Perform the following.

a) Create a matrix of zeros with 2 rows and 4 columns.


b) Create the row vector of odd numbers through 21,
L =1 3 5 7 9 11 13 15 17 19 21
Use the colon operator.
c) Find the sum S of vector L’s elements.
d) Form the matrix
A=
232
101

40-41. Perform the following.

a) Create two different vectors of the same length and add them.
b) Now subtract them.
c) Perform element-by-element multiplication on them.
d) Perform element-by-element division on them.
e) Raise one of the vectors to the second power.
f) Create a 3 × 3 matrix and display the first row of and the second column on the screen.

42. Create a new function, that multiply 2 numbers, and use it

43. Create a function that transform years in days

44. Create a function that check if a number is above or below 1814

45. Create a function that receives a vector and display all the elements of this vector

46. Create a function calculate the area (I) between two points (a,b) by the trapezoidal rule:

Course: Numerical Methods Laboratory


Program: BSEE 3A
Instructor: Jonicio A. Dacuya, CpE
Republic of the Philippines

Naval, Biliran
SCHOOL OF ENGINEERING
47-48. Obtain the plot of the following.
t=0:0.1:10;
y1=sin(t);
y2=cos(t);
plot(t,y1,'r',t,y2,'b--');
x=[1.7*pi;1.6*pi];
y=[-0.3; 0.7];
s=['sin(t)';'cos(t)'];
text(x, y, s); % Add comment at (x,y)
title('Sin and Cos'); % Title
legend('sin','cos') % Add legend
xlabel('time') % the name of X-axis
ylabel('sin & cos') % the name of Y-axis
grid on % Add grid
axis square % set figure as a shape of square

49-50. Perform the following (Use branching)

Create a a code that checks if you can buy alcohol in


Norway, the type of alcohol, if you can enter in a night club,
and if you can teach your friend to drive:

• age < 18 – None


• 18 < age < 20 – Alcohol below 22%, no clubbing nor
teach
• 20 < age < 21 Alcohol above 22%, but no clubbing nor
teaching
• 21 < age < 25 – Alcohol above 22% and clubbing, but
no teaching
• age > 25 – All allowed

Course: Numerical Methods Laboratory


Program: BSEE 3A
Instructor: Jonicio A. Dacuya, CpE

You might also like