Skip to content Skip to navigation Skip to collection information

OpenStax_CNX

You are here: Home » Content » Freshman Engineering Problem Solving with MATLAB » Programming With M-Files: For-Loop Exercises

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

Programming With M-Files: For-Loop Exercises

Module by: Darryl Morrell. E-mail the author

Summary: This module provide several practice exercises on the use of for-loops.

Exercise 1

Frequency is a defining characteristic of many physical phenomena including sound and light. For sound, frequency is perceived as the pitch of the sound. For light, frequency is perceived as color.

The equation of a cosine wave with frequency f f cycles/second is

y=cos2πft y 2 π f t
(1)
Create an m-file script to plot the cosine waveform with frequency f=2 f 2 cycles/s for values of t t between 0 and 4.

Exercise 2

Suppose that we wish to plot (on the same graph) the cosine waveform in Exercise 1 for the following frequencies: 0.7, 1, 1.5, and 2. Modify your solution to Exercise 1 to use a for-loop to create this plot.

Solution A

The following for-loop is designed to solve this problem:

t=0:.01:4;
hold on
for f=[0.7 1 1.5 2]
    y=cos(2*pi*f*t);
    plot(t,y);
end
When this code is run, it plots all of the cosine waveforms using the same line style and color, as shown in Figure 1. The next solution shows one rather complicated way to change the line style and color.

Figure 1: Plot of cosines at different frequencies.
Figure 1 (Fig1.png)

Solution B

The following code changes the line style of each of the cosine plots.

fs = ['r-';'b.';'go';'y*']; %Create an array of line style strings
x=1; %Initialize the counter variable x
t=0:.01:4;
hold on
for f=[0.7 1 1.5 2]
    y=cos(2*pi*f*t);
    plot(t,y,fs(x,1:end)); %Plot t vs y with the line style string indexed by x
    x=x+1; %Increment x by one
end
xlabel('t');
ylabel('cos(2 pi f t)')
title('plots of cos(t)')
legend('f=0.7','f=1','f=1.5','f=2')
This code produces the plot in Figure 2. Note that this plot follows appropriate engineering graphics conventions-axes are labeled, there is a title, and there is a legend to identify each plot.

Figure 2: Plot of cosines at different frequencies.
Figure 2 (Fig2.png)

Exercise 3

Suppose that you are building a mobile robot, and are designing the size of the wheels on the robot to achieve a given travel speed. Denote the radius of the wheel (in inches) as r r, and the rotations per second of the wheel as w w. The robot speed s s (in inches/s) is related to r r and w w by the equation

s=2πrw s 2 π r w
(2)
On one graph, create plots of the relationship between s s and w w for values of r r of 0.5in, 0.7in, 1.6in, 3.2in, and 4.0in.

Exercise 4

Multiple Hypotenuses

Figure 3: Sides of a right triangle.
Figure 3 (RT.png)
Consider the right triangle shown in Figure 3. Suppose you wish to find the length of the hypotenuse c c of this triangle for several combinations of side lengths a a and b b ; the specific combinations of a a and b b are given in Table 1. Write an m-file to do this.
Table 1: Side Lengths
a a b b
1 1
1 2
2 3
4 1
2 2

Solution

This solution was created by Heidi Zipperian:

a=[1 1 2 4 2]
b=[1 2 3 1 2]
for j=1:5
    c=sqrt(a(j)^2+b(j)^2)
end
A solution that does not use a for loop was also created by Heidi:
a=[1 1 2 4 2]
b=[1 2 3 1 2]
c=sqrt(a.^2+b.^2)

Collection Navigation

Content actions

Download:

Collection as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add:

Collection to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks

Module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks