Skip to content Skip to navigation

Connexions

You are here: Home » Content » Programming With M-Files: For-Loop Exercises

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of a lens

      Lenses

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

      What is in a lens?

      Lens makers point to Connexions 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 Connexions 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
  • E-mail the author
  • Rate this module (How does the rating system work?)

    Rating system

    Ratings

    Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

    How to rate a module

    Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

    (0 ratings)

Recently Viewed

This feature requires Javascript to be enabled.

Programming With M-Files: For-Loop Exercises

Module by: Darryl Morrell

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

Note: Your browser may not currently support MathML. See our browser support page for additional details. You can always view the correct math in the PDF version.

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)

Comments, questions, feedback, criticisms?

Send feedback