



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Numerical Analysis; Subject: Mathematics; University: University of California - Berkeley; Term: Spring 2002;
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Math 128a - Program 2 - Due April 25 Your assignment is to implement a program which computes and plots a curve in the x, y plane, by fitting a cubic spline curve through a given set of points (xi, yi) in the plane. You can think of this as the algorithm underlying various drawing programs like xfig or paint. This set of points may be from any source, including clicking on your mouse, or arrays computed by another program, like Program 1earlier this semester. The basic idea is that you should be able to click on a matlab graphics window to specify a list of points in the plane, and have the program connect them with a curve. There is an option to identify certain points as “corners”, so the program will not try to make the curve smooth there (have a continuous tangent); this lets you draw curves that are partly smooth and partly polygonal. There is another option to make the curve closed, i.e. connect the last point to the first point; this lets you draw circles, for example. If your points are supplied as an input array, they may be the output of Program 1, which computed them as solutions to f (x, y) = 0. So there is also an option to test how well the curve you compute satisfies f (x, y) = 0 along its entire length, not just the input points. We will supply most of the Matlab code for this problem, including a program to test it. You will need to write 4 subroutines, for which we supply detailed input and output descriptions. To help you debug, we will also tell you what some of the correct output should be from the test program. You will turn in your code so we can run it and make sure it works. The main routine is called Param2dSpline.m. Here is a detailed description of its inputs (see also the comments in the code):
A flagmouse indicating the source of the points in the plane. If mouse = 0, they are supplied by the next two input arguments x(1: n) and y(1: n). If mouse = 1, they should be input using the mouse by pointing at the graphics window and clicking on the desired location of each point. A left mouse click means the point is a “non corner point” (explained below), a right mouse click means the point is a “corner point”, and hitting any other key on the keyboard means end of input.
Two arrays x(1: n), y(1: n) defining the x and y coordinates of the points along the curve. If mouse = 0, these are used as input. If mouse = 1, they are ignored and instead set by the user clicking on the mouse as just described.
The name f unc of a function that evaluates f (x, y), where the points along the curve including x(i), y(i) are intended to satisfy f (x(i), y(i)) = 0. If there is no such function (for example if the points are input by the mouse), then the input function should be the empty string ’’. This is used to test whether you have correctly implemented the splines.
A flag closed indicating whether the curve should be closed, that is (x(1), y(1)) should be connected to (x(n), y(n)) (if closed = 1 ), or not (closed = 0).
An array cn(1: m) of corner indices. If mouse = 0, cn(1: m) is used as the input, but if mouse = 1 ,cn is determined by mouse-clicking as described above (right mouse clicks). No matter how cn(i) is specified, it indicates that (x(cn(i)), y(cn(i))) is to be treated as a corner in the curve (details below). If closed = 0, the endpoints are defined to be corners as well, whether or not 1and n are included in cn. If closed = 1, there may be no corners (cn may be empty, i.e. have length zero).
A flag corners, which is > 0 to indicate that the program should identify additional corners automatically, and 0 to indicate that it should not. The way a positive value of corners is
used to detect a corner is discussed further below. Corners detected automatically are in addition to any specified by cn.
The outputs of Param2dSpline will be
x(s) = xc(4, i) ∗ (s(i + 1 )− s)^3 + xc(3, i) ∗ (s − s(i))^3 + xc(2, i) ∗ (s − s(i)) + xc(1, i) ∗ (s(i + 1 )− s) y(s) = yc(4, i) ∗ (s(i + 1 )− s)^3 + yc(3, i) ∗ (s − s(i))^3 + yc(2, i) ∗ (s − s(i)) + yc(1, i) ∗ (s(i + 1 )− s)
where i = 1, ..., ne − 1.
Here is how you will do a spline fit. First we have to define arclength s: s(1) = 0, and corresponds to starting the curve at x(1), y(1). Then
s(2) = ((x(2) − x(1))^2 + (y(2) − y(1))^2 )^1 /^2
the distance from (x(1), y(1)) to (x(2), y(2)). Similarly
s(i) = s(i − 1 ) + ((x(i) − x(i − 1))^2 + (y(i) − y(i − 1))^2 )^1 /^2.
Now consider a segment of the curve (i.e. beginning and ending at corners) starting at (x(i), y(i)) and ending at (x(j), y(j)), both of which are corners, with no corners in between them. Then we will build 2 cubic splines, one interpolating the points
(s(i), x(i)), (s(i + 1 ), x(i + 1 )), ..., (s(j), x(j))
(a) GetMousePts.m Uses the Matlab “ginput” command to get locations of mouse clicks and which button was used. (b) FindCorners.m You need to write this. This is called if corners > 0 to identify corners automatically. It takes as inputs the coordinates of the points on the curve, a threshold (equal to the value of corners) and the closed flag. If the curve is not closed (closed = 0) the end points are considered corners. Otherwise, for every point (x(i), y(i)) with 2 neighbors, it is tested for being a corner as described before. FindCorners returns the list of indices of corners in increasing order. For example, if points i = 1 ,i = 3 and i = 4 are considered corners, then FindCorners should return [1, 3 , 4].
For debugging purposes, here are some correct output values that Test Param2dSpline should print out:
Test Case n # corners detected max |f | along curve Quintic 10 2 7.9093e+ Quintic 90 2 5.0585e- Circle 10 0 2.2358e- Circle 90 0 3.0968e- Cusp 11 3 2.8868e- Cusp 91 3 2.9945e- Rose 6 0 4.0910e- Rose 100 0 2.3852e- Jagged (corner > 0) 11 7 3.9504e- Jagged (corner > 0) 81 6 9.7145e- Jagged (corner = 0) 11 2 7.6430e- Jagged (corner = 0) 81 2 8.8181e-