Least Square Fit 25 points Solution



In a language of your choice, write a program that

does a least square fit of the thrust data used in Homework 1.

Use your data x = 0.0 0.1 0.2 … 1.9

y = 0.0 6.0 14.1 … 0.0

Be sure time 0.0 has value 0.0, time 1.9 has value 0.0

Do the least square fit with 3, 4, … , 17 degree polynomials.

Compute the maximum error, the average error and RMS error

for each fit. If convenient, look at the plots of your fit

and compare to the input.

Print out your data. Remove excess printout, print:

Print out your polynomial order for each degree 3..17 and

print out maximum, average and RMS error for each.

Lecture 1 shows how to compute error

copy whatever you need from Lecture 4, Least square fit

Submit your source code file(s) and your output file

submit cs455 hw2 your-source your-output

My MatLab errors looked like:hw2_m.out

(with a lot of complaints from MatLab!)

UGH! Matlab has started blabbing errors. I will ignore them.

My “C” errors did not improve as much:hw2_c.out

Plotting three points between each starting point in MatLab, n=19, gave:

The steps are typically:

Have the thrust data in X array, time values.

Have the thrust data in Y array, newton values.

Build the least square simultaneous equations, n=3 first

Given a value of Y for each value of X,

Y_approximate = C0 + C1 * X + C2 * X^2 + C3 * X^3

Then the linear equations would be:

| SUM(1 *1) SUM(1 *X) SUM(1 *X^2) SUM(1 *X^3) | | C0 | | SUM(1 *Y) |

| SUM(X *1) SUM(X *X) SUM(X *X^2) SUM(X *X^3) | | C1 | | SUM(X *Y) |

| SUM(X^2*1) SUM(X^2*X) SUM(X^2*X^2) SUM(X^2*X^3) |x| C2 | = | SUM(X^2*Y) |

| SUM(X^3*1) SUM(X^3*X) SUM(X^3*X^2) SUM(X^3*X^3) | | C3 | | SUM(X^3*Y) |

Note that index [i][j] of the matrix has SUM(X^(i+j))

Solve the simultaneous equations for C0, C1, C2, C3

using some version of “simeq”.

This is equivalent to polyfit.

Compute errors by using “peval” with the coefficients

for each X and subtract the corresponding Y.

Save maximum error, compute average and RMS error.


Repeat, advancing n to 4, 5, …, 17

If you use least_square_fit.c or least_square_fit.java

make changes in function data_set

put in time and thrust data from homework 1

k=20 number of data points

xx=time[i] i is idata for Matlab and Fortran, else i=idata-1

yy=thrust[i] i is idata for Matlab and Fortran, else i=idata-1

Remember to change array sizes C[20] Y[20] A[400] pwr[40]

Ok to delete unneeded code. Do initialization.