Scientific Computing Homework 3

Roger Jang


Due date: 20200504 23:59:59

Ellipse Fit

Important info for all homework

Here is the original descriptions of the assignment in MATLAB: Ellipse fitting. The descriptions for Python is similar so we will not repeat here.

Here is a file list of example code (to download them all: exampleCode.zip) for you to start with. The readme.txt of the example code folder is shown next:

This is the readme.txt file in example code for HW03 of Scientific Computing Your submission (you only need to submit one of the following): Python: ellipseFit.py Matlab: ellipseFit.m Example input/output files: input.txt: input file output.standard_python.txt: output file (the correct answer returned by Python) output.standard_matlab.txt: output file (the correct answer returned by MATLAB) Input/output file format: Input: Each two-row is a set of 2D points, with the first row is X coordinates and the second row is Y coordinates. For each two-row, you need to generate a set of [cx, cy, rx, ry] For instance, "input.txt" has 20 rows, indicating there are 10 sets of 2D points, and you need to output 10 sets of answers. Output: A matrix of numbers, with each row being an answer to a set of 2D points. Example main program to run the test: Python: mainTest.py, with the following command to run it. python mainTest.py < input.txt > output.python.txt Matlab: mainText.m, with the following command (within matlab) to run it. mainText input.txt output.matlab.txt Standard program for MATLAB ellipseFitSP.p is a standard program that can be invoked just like an M file in MATLAB to check against your submission of "ellipseFit.m". In particular, if you type "ellipseFitSP(data, 1), the program will show the dataset together with the final ellipse for fitting. Some of the fitting results are placed under "output" folder for comparison. Requirement: Python: You need to use scipy.optimize.fmin() for DSS, and numpy.linalg.lstsq() for least-square estimate. Matlab: You need to use fminsearch() for DSS, and "\" for least-square estimate.

For least-squares estimate in Python, you need to use numpy.linalg.lstsq(), otherwise your submission will be rejected. Please refer to the following link for examples. https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.linalg.lstsq.html

For your reference, here is an example of using ellipseFitSP.p to do the fitting, together with some plots to show the results:

Example 1: ../exampleCode/mainTest02.mfunction output=mainTest(inputFile, outputFile) if nargin<1, inputFile='input.txt'; end if nargin<2, outputFile='output.matlab.txt'; end % Read input file fprintf('Reading input file "%s"...\n', inputFile); output=[]; id=1; fidInput=fopen(inputFile, 'r'); if fidInput<0, error('Cannot open input file "%s"!', inputFile); end while ~feof(fidInput) x = str2num(fgetl(fidInput)); y = str2num(fgetl(fidInput)); data=[x; y]; % output(id, :)=ellipseFit(data); % You can comment out this line (and uncomment the next line) to test ellipseFitSP.p figure; output(id, :)=ellipseFitSP(data, 1); title(sprintf('Data ID=%d', id)); fprintf('id=%d\n', id); id=id+1; end fclose(fidInput); % Write output file fprintf('Writing output file "%s"...\n', outputFile); fidOutput=fopen(outputFile, 'w'); if fidOutput<0, error('Cannot open output file "%s"!', outputFile); end for i=1:length(output) fprintf(fidOutput, '%f ', output(i,:)); fprintf(fidOutput, '\n'); end fclose(fidOutput);Reading input file "input.txt"... id=1 id=2 id=3 id=4 id=5 id=6 id=7 id=8 id=9 id=10 Writing output file "output.matlab.txt"... ans = 9.0024 4.9721 10.9646 7.1436 5.9747 5.9938 3.0150 6.0088 6.9694 9.9781 6.0374 8.1119 6.9718 10.3292 6.0908 6.4056 7.2007 3.9102 6.1792 7.1658 5.0892 3.8345 13.3067 7.3028 8.0125 5.9950 5.9924 7.0963 7.6332 8.9463 7.7131 7.0760 4.9583 4.9811 9.9671 8.0358 8.9557 10.0168 6.1044 7.0933