Click here to load reader
Upload
karina-anggelia
View
213
Download
0
Embed Size (px)
Citation preview
Writing Matlab functions Dynamica WB1632 – Homework assignment Lecturer: Robert Babuska
Introduction
This is one of the homework assignments for the course Dynamica (WB1632). Homework means you
have to work it out in your free study time and not during one of the Matlab / Simulink computer
sessions, in which you will be working on other assignments.
Your task in this assignment is to implement a Matlab function that will find intersections of two point-
wise defined curves in 2D. This problem is quite simple, as it only involves elementary geometry and no
prior knowledge of dynamics is needed to solve it. However, it is not entirely trivial and some thinking
is required. We will use the result of this assignment in one of our next simulation assignments.
The learning objectives of this assignment are as follows:
1. Be able to decompose a given problem into easier-to-solve sub-problems, write the code to solve
the sub-problems and then integrate the solutions in one whole.
2. Distinguish three phases in the process of converting a given problem into a computer program
that provides a solution: (i) finding an algorithmic solution, (ii) implementing the algorithm in a
specific programming language (Matlab in our case), and (iii) testing whether the code works
correctly.
3. Implement each algorithm first in the form of a Matlab script (for the easy of debugging and
testing) and then convert it into a function (for the ease of later use).
4. Realize that numerical computations are not exact and take this fact into account in your
implementation.
We will guide you step by step through the overall process and leave space for you to implement the
algorithms. Please, read this entire text first, before you start implementing in Matlab.
Note: You have to upload your result to Blackboard by March 13, 2014 23:59 at the latest. Note that you
will need the function for your final Simulink assignment during the last computer session. Make sure
you have the M-file available, for instance, somewhere in the cloud and, just for the case, take it with
you also on a USB stick or a portable hard disk.
Assignment
Given are two curves, � and �, defined point-wise in a 2D space. In between the points, the curve is
linear. The points are stored in two matrices � and �, respectively.
Implement a Matlab function that will calculate all intersections of the two curves and return them in a
matrix, say �. In addition, the function must plot the curves, � in blue, � in green, with small dots
marking the individual points and larger red dots marking the intersections.
Example: Suppose � is a sinusoidal curve and � consists two connected line segments, as defined by the
following Matlab commands. Note that each row of the matrices contains one point, the x-coordinate in
the first column and the y-coordinate in the second column:
WB1632 Dynamica - Numerieke Simulaties (2013 - 2014) 2
x = (0:0.04:1)';
P = [x sin(10*x)];
Q = [0 0; .5 .5; 0.8 0.4];
Your function will generate the following plot:
and it will also return the following matrix:
R = 0 0
0.2851 0.2851
0.6745 0.4418
Solution approach
The most important step is to thoroughly understand the problem and be able to find an algorithmic
solution to it. The actual implementation in a programming language is usually quite straightforward,
once we understand the syntax of the language.
So, in our case, the question is what algorithm we can think of to find the intersections. Look at the
figure and notice that each curve consists of many consecutive line segments. The problem can therefore
be restated as finding the intersections among all pairs of line segments of the two curves. We can write
this down in a piece of a pseudo-code:
Input: curves P and Q
initialize R empty
for all segments sp(i) of curve P
find all intersections of sp(i) with all segments sq(j) of curve Q
add the intersections found to R
end loop
Output: R
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
x
y
WB1632 Dynamica - Numerieke Simulaties (2013 - 2014) 3
Contrary to the code of a specific programming language, writing pseudo-code does not have any strict
rules. The idea is to get close to programming, but abstract from trivial details, such as how to extract
the actual line segments from the matrices � and �. The pseudo-code typically also contains high-level
concept like “find all intersections of …” which need to be further worked out. This is something we do
once we are sure that the overall algorithm is sound. As this problem is fairly simple and we are sure that
the above idea will work, let us directly move on to the details of computing the intersection of two line
segments.
Given two line segments, each defined by two points, the goal is to find their intersections. Although we
could easily derive the equations ourselves, somebody has probably already done it for us. Searching the
Internet for “intersection of two lines” leads us (among others) to the Wikipedia page:
http://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
with the following solution using determinants:
As Matlab has been designed for linear algebra, this solution should be quite effective in terms of
implementation and execution speed. With this information at hand, you can proceed to implementing
the algorithm yourself (use the Matlab function det). After getting the case of two intersecting
segments right, pay attention to the fact that the lines can be parallel, in which case the intersection does
not exist and the function should return an empty matrix. Here, numerical issues may pop up, as
rounding-off errors will make two lines that are parallel on paper not exactly parallel in calculations. The
line segments can also (partly) coincide / overlap, in which case there are infinitely many intersections
and we obviously cannot compute them all. In that case of (partly) overlapping line segments, the
function should only return the end point(s).
The function you will implement should have the following syntax:
R = intersectLines(P,Q)
with
P = [px1 py1; px2 py2] and Q = [qx1 qy1; qx2 qy2]
the end points of the two line segments. You can test the function on the following cases:
P = [1 2.5; 2 2.5]; Q = [1 2; 2 3]; % intersecting line segments
P = [1.5 2.8; 1.7 3.7]; Q = [1 2; 2 3]; % nonintersecting segments
P = [1.5 2.8; 1.7 3.7]; Q = P + 1; % parallel segments
P = [1 2; 2 2]; Q = P; % coinciding segments
P = [1 2; 2 2]; Q = [P(2,:); 1.5 2]; % partly coinciding segments
WB1632 Dynamica - Numerieke Simulaties (2013 - 2014) 4
Once you are done, you can proceed to implementing the overall algorithm as given in the pseudo-code
on page 2. The curve intersection function will have the following syntax:
R = intersectCurves(P,Q)
And will call the intersectLines function repeatedly to calculate all the intersections. You can
test this function on the example given in the beginning of page 2.
Help on Matlab commands and functions
Use the doc or help functions of Matlab to quickly find information on commands and functions you
intend to use. For instance, to learn the Matlab syntax for writing functions, type in the Workspace
doc function.
To learn about for loops, type in the Workspace
doc for
If you do not know the exact name of the function or command, use lookfor with some appropriate
keywords, such as
lookfor determinant
Feedback
We are curious how you managed this assignment. Is it too easy or too hard? Is something not clear? Let
us know, please, at [email protected].