Each of the problems below is intended to be solved by generating a script or m-file. Be sure to have a very clearly written header for each M-file so that anyone reading your work can decipher the sort of problems you are solving in each M-file. Also, be sure to label all axes and assign each plot a title.

A complete set of m-file 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 2-3 submit a single M-file and two, four-panel plot pages. Click here to see what your resulting plots should look like.

  1. 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
  2. We'll start off by revisiting PS5 problems #8-13 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.
  3. 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 M-file and one plot page. Click here to see what your resulting plot should look like.
  4. 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 5-6 submit a single M-file and one plot page. Click here to see what your resulting plot should look like.
  5. Using a WHILE LOOP, generate an array X that goes from -2pi.gif (52 bytes) to 2pi.gif (52 bytes) in steps of 0.1.
  6. 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.