1
ENGR 133 Lab 05
Authored By: Yazan Abbasi
Authored on: 10/29/2021
Exercise #1... Problem 4.19
Problem Statement
Write a program that accepts a year and determines whether the year is a leap year. Use this mod function. The output
should be the variable extra_day, which should be 1 if the year is a leap year and a 0 otherwise. The rules for determining
leap years in the Gregorian calendar are as follows:
1. All years evenly divisable by 400 are leap years.
2. Years evenly divisable by 100 but not divisable by 400 are not leap years.
3. Years divisable by 4 but not 100 are leap years.
4. All other years are not leap years.
For exapmle, the years 1800, 1900, 2100, 2300, and 2500 are not leap years, but 2400 is a leap year.
Pseudocode:
• Initialize Variables
• Perform Calculations
• Display Results
Problem Solution
Initialize Variables
Index exceeds the number of array elements (0).
Perform Calculations
extra_day = 0; % initializes counter variable
clc, clear,close all
yearInput = inputdlg('Enter the year (4 digits): '); % asks for input
year = str2num(yearInput{1}); % converts year string to number
if mod(year,400) == 0 % performs logical test for rule 1
extra_day = 1; % changes counter variable if true
elseif mod(year,100) == 0 % performs logical test for rule 2
extra_day = 0; % changes counter variable if true
elseif mod(year,4) == 0 % performs logical test for rule 3
extra_day = 1; % changes counter variable if true
else
extra_day = 0; % changes counter variable if all tests false
end
if extra_day == 1
output = 'is';
2
Display Results
Exercise #2... Problem 4.24
Problem Statement
Write a program that uses a for loop to determine the time at which an object is the closest to the origin at (0,0). Determine also
the minimim distance.
Pseudocode:
• Initialize a time vector over the interval of interest.
• Initialize vectors of the x and y coordinates over time.
• Compute an array of d values for each (x,y) pair.
• Find the minimum value of the d vector -- answers the 1st part.
• Confirm that our solution is reasonable.
Problem Solution
Initialize Variables
Perform Calculations
Display Results
The minimum distance is 1.3581.
It occurs at t = 2.2300 seconds.
fprintf('It occurs at t = %6.4f seconds.\n\n',tmin)
else
output = 'is not';
end
fprintf('The year %4.0f %s a leap year.\n\n',year,output)
clc,clear,close all
t = 0:0.01:4; % initialize time vector
x = 5*t - 10; % initializes x-coordinate vector
y = 25*t.^2 - 120*t + 144; % initializes y-coordinate vector
d = sqrt(x.^2 + y.^2); % initializes distance vector
min_dist = Inf; % initializes placeholder for storing minimu, distance
for k = 1:length(t) % loop to look at every value in distance array
if d(k) < min_dist % replace min_dist if lower than previous value
min_dist = d(k); % replace min_dist if lower than previous value
tmin = t(k); % replace time value
end % terminate if branch
end % terminate for loop
fprintf('The minimum distance is %6.4f.\n\n',min_dist)
3
plot(x,y), grid minor % plot the entire time range
title('Proximity to (0,0) plot')
xlabel('x(t) = 5t-10'),ylabel('y(t) = 25^2 - 120t + 144')
[a,b] = min(d); % store the minimum distance and its index
figure % open a new figure window
plot(x,y), grid minor % plot the entire time range
hold on % configure figure window 2
axis([x(b)-1,x(b)+1,y(b)-1,y(b)+1]) % set axes close to min_dist points
plot(x(b),y(b),'red*','MarkerSize',20) % add marker for min_dist points
title('Proximity to (0,0) plot')
xlabel('x(t) = 5t -10'),ylabel('y(t) = 25^t - 120 + 144')
4
Exercise #3... Problem 4.32
Problem Statement
We are given a function describing force as a function of time and are asked to create a plot over an interval defined by an
undefined upper bound.
Pseudocode:
• Initialize Variables
• Perform Calculations
• Display Results
Problem Solution
Initialize Variables
Perform Calculations
clc,clear,close all
x = 0; y = 0; % x represents time, y represents force
k = 0; % k is the loop counter
while y < 9.8 % the upper limit of force is used as a condition
k = k + 1; % loop counter in incremented
x = x + 0.01; % x variable is incremeneted
y(k) = 10*(1-exp(-x/4)); % new y array element is computed
end
5
Display Results
xmax = x; % the upper limit of time is set
t = 0:0.01:xmax; % time array is computed
plot(t,y),grid minor,xlabel('Time x (s)'), ylabel('Force y (N)')
title('y = 10(1-e^{-x/4})')