0% found this document useful (0 votes)
48 views2 pages

MATLAB Conditional and Loop Examples

This document contains the student's answers to 4 questions involving MATLAB code. Question 1 checks if a year variable is before, equal to, or after 1800. Question 2 displays dates and checks if they are before or after 1814. Question 3 displays restrictions for alcohol and activities depending on a person's age. Question 4 numerically approximates an integral using the trapezoidal rule.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

MATLAB Conditional and Loop Examples

This document contains the student's answers to 4 questions involving MATLAB code. Question 1 checks if a year variable is before, equal to, or after 1800. Question 2 displays dates and checks if they are before or after 1814. Question 3 displays restrictions for alcohol and activities depending on a person's age. Question 4 numerically approximates an integral using the trapezoidal rule.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

MOHD KHAIRUL FAHMI BIN MUHAMAD 2018400498

Q1 year = 1750;

if year < 1800

disp('Year is before 1800');

elseif year == 1800

disp('Year is 1800');

else

disp('Year is above 1800');

end

Ans: Year is before 1800

Q2 dates = [1015 1066 1660 1814 1905 2014];

how_may_dates = length(dates);

for i = 1: how_may_dates

disp(['The date is: ' num2str(dates(i))]);

if dates(i) < 1814

disp('Before 1814');

elseif dates(i) == 1814

disp('It is 1814!');

else disp('After 1814');

end

end

ans: The date is: 1015


Before 1814
The date is: 1066
Before 1814
The date is: 1660
Before 1814
The date is: 1814
It is 1814!
The date is: 1905
After 1814
The date is: 2014
After 1814
MOHD KHAIRUL FAHMI BIN MUHAMAD 2018400498

Q3 age = 17

if age < 18

disp('None');

elseif age < 20

disp('Alcohol below 22%, no clubbing nor teach');

elseif age < 21

disp('Alcohol above 22%, but no clubbing nor teaching ');

elseif age < 25

disp('Alcohol above 22% and clubbing, but no teaching ');

else

disp('All allowed');

end
ans: age = 17
None

Q4 f = @(x)x*sin(x);
a = 0;
b = pi/2;
n = 10;
h = (b-a)/n;
s = 0.5*(f(a)+f(b));
for i = 1:n-1
s = s + f(a+i*h)
end
I = h*s

Ans: s = 0.8100
s = 0.9071
s = 1.1210
s = 1.4903
s = 2.0457
s = 2.8081
s = 3.7879
s = 4.9830
s = 6.3793

I = 1.0021

You might also like