Professional Documents
Culture Documents
Laboratory Exercise-1
Laboratory Exercise-1
Laboratory Exercise-1
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:
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)
a = [3 6 7];
b = [1 9 4];
c = a + b
a = [3 6 7];
b = [1 9 4 5];
c = a(2) + b(4)
x = pi;
y = sin(pi/2)
z = exp(-sin(pi/2))
plot(y,z)
x = [0:0.1:20];
y = sin(x);
plot(x,y)
BASIC LOOPING
b = 3;
for k = 1:5
b
end
b = 3;
for k = 1:5
b^k
end
b = 3;
for k = 1:5
b^k+3
end
sum1 = 0;
for k = 1:9
Naval, Biliran
SCHOOL OF ENGINEERING
sum1 = sum1+k;
end
sum1
sum1 = 0;
for k = 1:2:9
sum1 = sum1+k;
end
sum1
sum1 = 0;
for k = 1:2:9
sum1 = sum1+k;
end
sum1
b = [3 8 9 4 7 5];
sum1 = 0;
for k = 1:4
sum1 = sum1+b(k);
end
sum1
sum1 = 0;
for n = 1:2
for m = 1:3
sum1 = sum1+n*m;
end
end
sum1
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
Naval, Biliran
SCHOOL OF ENGINEERING
end
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
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
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
x = 3;
while (x < 100)
x = x*3;
end
x
MATRIX OPERATION
a = [2 12 25];
b = [3 7 4];
c = a+b
a = [3 4; 1 6];
b = [5 2; 11 7];
c = a+b
Naval, Biliran
SCHOOL OF ENGINEERING
23. What is the output?
a = [3 5; 1 4];
b = 2*a
a = [2 3 5];
b = [2 4 9];
c = a.*b
a = [2 3; 1 4];
b = [5 1; 7 2];
c = a.*b
a = [2 3 5];
b = sin(a)
a = [2 3 5];
b = 2*a.^2+3*a+4
A = [3 5; 2 4];
c = A(2,2)+A(1,2)
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)
A = [4 1 2; 0 3 1; 0 1 2];
b = [17 ; 19 ; 13];
x = inv(A)*b
MISCELLANEOUS EXERCISES
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
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.
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:
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