next up previous
Next: About this document ...

University of Cincinnati
Department of Electrical & Computer Engineering and Computer Science


20 ENFD 112 - Fundamentals of Programming

\framebox{\sc Laboratory 2: Modeling, Scripting, Visualization}

Spring 2008


{\bb 1. Objective}


The purpose of this assignment is to perform some elementary modeling involving well-known trigonometric and geometric relations and to use MATLAB to visualize the use of the model. The student will program an elementary MATLAB script which will take user input, so some elementary processing on that input, and produce and display a visual result that is comprehensible.



{\bb 2. MATLAB Scripting: Input, Process, Output}


Create a MATLAB script with the following characteristics:

  1. Input: The user is prompted for four numbers. Each will be interpreted as the radius of a circle. An example of user input is the following:
       Circle 1 radius> 5.78
       Circle 2 radius> 10.3
       Circle 3 radius> 4
       Circle 4 radius> 8.1
    
    where numbers are entered by the user through a series of keystrokes involving keys '0' to '9' and '.' and ended by pressing the 'enter' key. Strings such as Circle 1 radius> are prompts that your script displays when it is ready to receive input for a circle.
  2. Process: The script will find the coordinates of the centers for the four circles so that
    1. Circle 1 and Circle 3 are tangent to each other at the origin with Circle 1 on the left.
    2. The centers of Circle 1 and Circle 3 are on the x axis
    3. Circle 2 is tangent to Circle 1 and Circle 3 and its center above the x axis
    4. Circle 4 is tangent to Circle 1 and Circle 3 and its center below the x axis.
  3. Output: Plot these circles together with 6 straight lines connecting the centers of the circles. An example is shown in Figure 1.

Figure 1: Output from the MATLAB program.
\begin{figure}\centerline{\epsfysize=4in\epsfbox{Fig/circle.ps}}
\end{figure}

Figure 2: A triangle with sides of length $a$, $b$, and $c$. The side $AC$ is the base of the triangle. In the text the height $h$ at $B$ (the distance from $B$ to the base $AC$) and the distance $a_2$ (the horizontal distance from $C$ to the vertical line passing through $B$) are computed in terms of the angle $BCA$.
\begin{figure}\centerline{\epsfysize=3in\epsfbox{Fig/triangle.ps}}
\end{figure}



{\bb 3. Analysis and Assistance}


The coordinates of a point in the plane will be represented as a pair $\langle x,y\rangle$ where $x$ is the horizontal displacement of the point from the origin and $y$ is the vertical displacement of the point from the origin. If $x$ is positive, the displacement is above the origin and if $x$ is negative, it is below. Similarly for $y$. The origin has coordinates $\langle 0,0\rangle$. Let $r_1,r_2,r_3,r_4$ be the radii of Circle 1, Circle 2, Circle 3, and Circle 4, respectively. The coordinates of the center of Circle 1 are then $\langle r_1,0\rangle$ and of the center of Circle 3 $\langle
-r_3,0\rangle$ as shown. To find the coordinates of the centers we must derive a relationship between their horizontal and vertical displacements and the distances between centers, which are known quantities.

This relationship may be derived easily with the help of Figure 2 which shows a triangle with points $A$, $B$, $C$, sides of length $a$, $b$, $c$, and base $AC$. We need to determine the value of unknowns $h$ and $a_2$ as a function of the knowns $a$, $b$, $c$. The expression we derive will be used to determine the coordinates of the centers of Circles 2 and 4 since $a$, $b$, and $c$ will be distances between centers (sums of radii). By the theorem of Pythagoras,

\begin{eqnarray*}
h^2 + a_2^2 &=& c^2\\
h^2 + a_1^2 &=& b^2
\end{eqnarray*}

Subtracting the second equation from the first gives

\begin{displaymath}a_2^2 - a_1^2 = c^2 - b^2 \end{displaymath}

Since $a_1+a_2=a$, we can substitute $a-a_2$ for $a_1$, and rearrange, to get

\begin{displaymath}a_2^2 = c^2 -b^2 +(a - a_2)^2 = c^2 -b^2 +a^2 -2aa_2 + a_2^2 \end{displaymath}

Subtracting $a_2^2$ from both sides, rearranging, and dividing both sides by $2a$ results in

\begin{displaymath}a_2 = (c^2 + a^2 - b^2)/2a. \end{displaymath}

From trigonometry,

\begin{eqnarray*}
&&cos(<_{BCA}) = a_2/c\\
&&sin(<_{BCA}) = h/c\\
\mbox{so,  }&&\\
&&h=c^{ .} sin(cos^{-1}((c^2+a^2-b^2)/2ac))\\
\end{eqnarray*}

To see how to use these results, consult Figure 3 where the coordinates $\langle x_2,y_2\rangle$ of the center of Circle 2 are found. Distance $d_{2,3}$ (corresponding to $c$ in the triangle of Figure 2) is the sum of the radii of Circles 2 and 3 which is $r_2+r_3$. Distance $d_{1,2}$ (corresponding to $b$ in the triangle of Figure 2) is the sum of the radii of Circles 1 and 2 which is $r_1+r_2$. The distance between centers of Circles 1 and 3 (corresponding to $a$ in the triangle of Figure 2) is $r_1+r_3$. The distance corresponding to $a_2$ in Figure 2 is $r_3-x_2$. Therefore

\begin{eqnarray*}
y_2 &=& (r_2+r_3)^{ .}sin(cos^{-1}(((r_2+r_3)^2+(r_1+r_3)^2-(r...
...x_2 &=& r_3 - (((r_2+r_3)^2+(r_1+r_3)^2-(r_1+r_2)^2)/2(r_1+r_3))
\end{eqnarray*}

The scripts in Figures 4 to 9 should be major hints in coding a solution to this problem. These scripts show how to draw lines and circles with given centers. The last figure shows how to obtain the center of circle 2. You will have to figure out how to obtain the center of circle 4.

Figure 3: Calculation of the center coordinates of Circle 2.
\begin{figure}\centerline{\epsfysize=5in\epsfbox{Fig/circle.1.ps}}
\end{figure}



{\bb 4. Submission}


Submit the single m file requested in Section 2 on or before April 13 using blackboard. See the course webpage at http://gauss.ececs.uc.edu/Courses/HTML/E112.html for instructions.

Figure 4: Script for drawing an approximation to a circle of radius 1 with a series of lines. The more lines, the better the approximation. The number of lines is input parameter n. Lines are drawn between pairs of points that are distance 1 from the origin at angles specified by vector angle which is created from angle = 0:2*pi/n:2*pi;. All angles in angle are uniformly spaced from 0 to $2\pi $ radians. The functions sin and cos are used to get the x and y coordinates of a point.
\begin{figure}\begin{verbatim}% PlotCircle_0.m: Draw a circle of radius 1 cen...
...], [y1 y2]); % Draw the linesaxis equal;
grid on;\end{verbatim}
\end{figure}

Figure 5: Alternative script for drawing an approximation to a circle of radius 1 with a series of lines. This time plot is used to draw the lines. All other considerations are the same as in Figure 4.
\begin{figure}\begin{verbatim}% PlotCircle_1.m: Plot a circle of radius 1 cen...
... plot(x,y); % Plot the circleaxis equal;
grid on;\end{verbatim}
\end{figure}

Figure 6: Script for drawing an approximation to a circle of radius 1 centered at a specified point. The x and y coordinates of the center are obtained via console input.
\begin{figure}\begin{verbatim}% PlotCircle_2.m: Plot a circle of radius 1 wit...
... plot(x,y); % Plot the circleaxis equal;
grid on;\end{verbatim}
\end{figure}

Figure 7: Script for drawing two circles of specified radius that are tangent to each other at the origin and whose centers lie on the x axis. The radii of the circles are obtained via console input.
\begin{figure}\begin{verbatim}% PlotCircle_3.m: Plot two circles of specified...
...1,x2,y2); % Draw all circlesaxis equal ;
grid on;\end{verbatim}
\end{figure}

Figure 8: Alternative to Figure 7 differing in the use of two calls to plot, one for each circle, and in the specification of circle colors.
\begin{figure}\begin{verbatim}% PlotCircle_4.m: Alternative to PlotCircle_3.m...
... 1 0]); % Draw circle 2 greenaxis equal;
grid on;\end{verbatim}
\end{figure}

Figure 9: Script for drawing two circles with radii R1 and R3, that are tangent to each other at the origin with the R1 circle centered on the x axis to the left of the origin and the R2 circle centered on the x axis to the right of the origin, and a third circle with radius R2 that is tangent to and above the other two circles.
\begin{figure}\begin{verbatim}% PlotCircle_5.m: Plot two circles (circles 1 a...
...0 0 1]); % Draw circle 3 blueaxis equal;
grid on;\end{verbatim}
\end{figure}




next up previous
Next: About this document ...
John Franco 2008-03-31