Each
of the problems below is intended to be solved by generating a script or
mfile. Be sure to have a very clearly written header for each Mfile so that
anyone reading your work can decipher the sort of problems you are solving in
each Mfile. Also, be sure to label all axes and assign each plot a title.
A
complete set of mfile solutions can be found here. Of course, your work does not
have to look exactly like these scripts but the results (e.g. plots) should
look much the same. Don’t be lazy and start by looking at these answers
… only refer to these solutions if you get stuck or want to check your
finished work.
In
this last problem set we are going to learn two of the most basic programming
operations to accomplish repeated calculations. These are programming
"loops" using FOR and WHILE statements.
For
problems 23 submit a single Mfile and two, fourpanel plot pages. Click here to see
what your resulting plots should look like.
 Read the HELP
pages on the following functions (either by typing "help" and
then the function name at the MATLAB command prompt, or by clicking
these links): sum,
for,
while
 We'll start
off by revisiting PS5
problems #813 but this time for each polynomial order 2 through 9 we
are going to quantify the "goodness" of our fit to the
original data set (i.e. sin(X)).
For each of the polynomials 2 through 9 calculate the square of the
difference between the data generated by sin(X) and each of
the polynomial equations. This difference is known as the SQUARED ERROR
for each data point. By taking the SUM off the difference between each
pair of data points you will calculate the TOTAL SQUARED ERROR.
 In each of the eight plot
panels use TEXT to indicate what the TOTAL SQUARED ERROR is between the
polynomial and the original data set.
For problem 4
submit a single Mfile and one plot page. Click here to
see what your resulting plot should look like.
 Here you are to use a FOR LOOP
to increment the order of the polynomial coefficients that you calculate
using POLYFIT from 2 through 9. Each time you evaluate a new set of
coefficients you should evaluate them using POLYVAL just as before but
plot them out on a single graph. So, the end result of your program will
produce be a single graph with 9 separate data lines plotted on it. One
will be the data for sin(X)
and the other eight(8) will be for polynomials of order 2 though 9.
For problems 56
submit a single Mfile and one plot page. Click here to
see what your resulting plot should look like.
 Using a WHILE
LOOP, generate an array X
that goes from 2 to 2 in steps of
0.1.
 Here you are
to use a WHILE LOOP to increment the order of the polynomial
coefficients until the TOTAL SQUARED ERROR between the polynomial data
and your original data set (i.e. sin(X) is LESS THAN 0.01. Each time
you generate a new set of coefficients you should evaluate them using POLYVAL
just as before but plot them out on a single graph. So, the end result
of your program will produce be a single graph with multiple data lines
plotted on it. One will be the data for sin(X) and the
others will be for polynomials starting at order 2 and going up to a
point where the two data sets differ by less than 0.01.
