Scientific Computing Homework 4

Roger Jang


Due date: 20200518 23:59:59

Hyperplane fitting via TLS

Important info for all homework

Here is the original descriptions of the assignment in MATLAB: Function for hyperplane fitting via TLS. 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 HW04 of Scientific Computing Your submission (you only need to submit one of the following): Python: hyperplaneFitViaTls.py Matlab: hyperplaneFitViaTls.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: The first line is the number of rows, D, of the subsequent dataset. The dataset is a D-by-N matrix, with each row being placed in a horizontal line, and the last row is the desired output. The above (D+1) lines are repeated as many times as the number of datasets in the file. For instance, "input.txt" has 10 datasets in the file. Output: For a dataset, the output will be the coefficients of the fitting hyperplane. 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 hyperplaneFitViaTlsSP.p is a standard program that can be invoked just like an M file in MATLAB to check against your submission of "hyperplaneFitViaTls.m". In particular, if you type "hyperplaneFitSP(data, 1), the program will show the dataset together with the final plane for fitting.

For your reference, here is an example of using hyperplaneFitViaTlsSP.p to do the fitting, together with some plots (if the input dimension is 1 or 2):

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) row=str2num(fgetl(fidInput)); data=[]; for i=1:row data(i,:)=str2num(fgetl(fidInput)); end dataSet{i}=data; % output{id}=hyperplaneFitViaTls(data); % You can comment out this line (and uncomment the next line) to test ellipseFitSP.p output{id}=hyperplaneFitViaTlsSP(data, 1); title(sprintf('Case id=%d', id)); fprintf('id=%d, row=%d\n', id, row); 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"... Plotting not support! id=1, row=5 Plotting not support! id=2, row=7 Plotting not support! id=3, row=8 id=4, row=2 Plotting not support! id=5, row=9 Plotting not support! id=6, row=6 id=7, row=3 Plotting not support! id=8, row=11 Plotting not support! id=9, row=4 Plotting not support! id=10, row=10 Writing output file "output.matlab.txt"... ans = 1¡Ñ10 <a href="matlab:helpPopup cell" style="font-weight:bold">cell</a> array Columns 1 through 9 {6¡Ñ1 double} {8¡Ñ1 double} {9¡Ñ1 double} {3¡Ñ1 double} {10¡Ñ1 double} {7¡Ñ1 double} {4¡Ñ1 double} {12¡Ñ1 double} {5¡Ñ1 double} Column 10 {11¡Ñ1 double}