Matlab Marina For Loops Exercises
Matlab Marina For Loops Exercises
Matlab Marina For Loops Exercises
1. Create a MATLAB program consisting of the MATLAB code of Figure 1. Run the program and
verify that the values in the array numbers are displayed. Determine the size and value(s)
of the variable k after the loop has been executed for the program.
clear; clc;
numbers = 1:0.5:5;
for k = 1:1:length(numbers)
val = numbers(k);
fprintf('%0.1f ', val);
end
fprintf('\n');
Figure 1. Display Numbers Code Segment
2. Write a MATLAB program that will display the numbers from 0 to 45 with a step of 5. The
program should display: 0, 5, 10, 15, 20, etc.. Use iteration (for loop) to solve this problem.
Run your program and verify that it operates correctly.
3. Modify the program of Exercise 3 so that the numbers are displayed in reverse order: 45,
40, 35, …, 5, 0.. Use iteration (for loop) to solve this problem. Run your program and verify
that it operates correctly.
4. Write a MATLAB program that will display the numbers from a user specified start number
to a user specified stop number. Assume that the second number the user enters will be
larger than the first number. Use iteration (for loop) to solve this problem. Run your
program and verify that it operates correctly. A sample run should look like:
Enter number 1: 4
Enter number 2: 9
456789
5. Run your program of Exercise 4 for the case of the stop number being smaller than the start
number, for example number 1 of 8 and number 2 of 4. What does the program do and
why? How could the program Exercise 4 be modified so that the execution of the loop is
avoided and a message is displayed for cases where the second number is less than the first
number?
6. Complete the MATLAB program of Figure 2 that will evaluate and plot the function
f ( t )= 4 − t 2 e −3t for the time range 0 ≤ t ≤ 3 seconds. Use MATLAB’s colon operator and
array operations to solve this problem.
clear, clc, close all;
% create array t of independent variable values
7. Complete the MATLAB program of Figure 3 that will evaluate and plot the function
f ( t )= 4 − t 2 e −3t for the time range 0 ≤ t ≤ 3 seconds. Use iteration (for loop) and scalar
operations to solve this problem.
8. Complete the MATLAB program of Figure 4 that will evaluate the series 1 – 1/2 + 1/4 - 1/8 +
1/16 - … for N terms where the value N is read from the user. Use MATLAB’s colon operator
and array operations to solve this problem.
clear; clc;
% read in number of terms N
2
9. Complete the MATLAB program of Figure 5 that will evaluate the series 1 – 1/2 + 1/4 - 1/8 +
1/16 - … for N terms where the value N is read from the user. Use iteration (for loop) and
scalar operations to solve this problem.
clear; clc;
% read in number of terms N
10. The file roddata.mat holds measurement data on iron rods that came off a metal casting
line. Write a MATLAB program that will help the quality control engineer analyze the casting
line. The program should read in the rod data from the MATLAB .mat file and determine the
number of rods whose dimensions exceed the desired tolerances. The nominal rod size is
125 cm long and 5 cm in diameter and the accepted deviation is +/- 0.5 cm in length and +/-
0.1 cm in diameter. The rod data file contains a two-dimensional array named roddata.
The first column is the length data and the second column is the diameter data. Use a for
loop and conditional structures rather than array operations and array comparisons to solve
this problem.
11. Write a MATLAB program that will determine the minimum and maximum value of an array
along with the locations of the minimum and maximum values. You may assume the array
will be a 1D row array and that there will only be a single instance of the minimum and
maximum values. Do not use MATLAB’s min and max or find functions to solve this
problem.
12. Write a MATLAB program that will smooth an array of noisy data using a two-point average.
Assume the array of data is a 1D row array and that a backwards average will be used for
x + x k −1
the smoothing. For a backwards two-point average, xavg k = k , except for k = 1
2
where xavg1 = x1 . One could also use a forward average or a midpoint average. Use a for
loop and conditional statements rather than array operations. Use the following statement
to create a row vector of noisy data from a normal distribution with a mean of 10.0 and a
standard deviation of 2.0 to test your program:
noisyData = 10.0 + 2.0*randn(1,100);
13. Write a MATLAB program that will display the columns of a 2D array. The elements of each
column should be displayed one column per line, i.e. display each set of the column values
3
as a row. Use the 2D array array2D = [5, 8, -2, 4; 3, 0, 2, 9]; to test your
program.