ENGR 240: Applied Numerical Methods for Engineers

Spring 2016

Instructor: Paul Lecoq

Rectabular Extursion
You will get a good grade if you can convince me that you understand the material.
     Collaboration is encouraged except for exams and quizzes.

Class policies:

Time and Location:2pm-3
M,W,F - Room 218. Building 18
Textbook:   Applied Numerical Methods with MATLAB by Steven C. Chapra   -- Any edition, but we will use the third edition and it is your responsibility to make sure you check to be sure you understand any differences.

Can you use numerical methods to find the unit surface area and volume of the Rectabular Extursion shown to the right?


    REQUIRED: USB drive: To save things worked on during class and to transfer files between home and school
    REQUIRED: A working email account.
    Computer: You can either do your work here at school in the classroom (when no one is using it), or at home on your home PC.
    You will want the student edition of matlab. I have asked the book store to order it(hahaha). You may also want to check out this link provided by the matlab people. Math Works . You can use classroom 218 when its not in use, and the last 15 computers in room 217 have matlab installed. I have heard rumors that the computers in the math learning center also have matlab installed.

Course Objectives:

    Finding roots of equations
    Solving systems of linear algebraic equations
    Curve fitting
    Numerical Differentiation
    Numerical Integration
    Ordinary Differential Equations
    Optional Topics

Course Schedule

April 6Chapter 1   General introduction     Math modeling, numerical methods, Problem solvingAssignment 1
April 11     Chapter 2   Matlab fundamentalsAssignment 2
April 18
April 25
May 2
May 9Midterm Exam
May 16
May 23
May 31
June 6
June 13
Final Exam

Assignments   30%
Midterm         25%
Final Exam      30%
Quizzes etc.     15%

If you are going to have to miss a class session, contact a friend to take good notes. Then review the notes before you come to class so you will be prepared for that class.
You don't need to provide a doctor's note. If the reason for absense is good enough for you, it's good enough for me.  However, don't forget that you are still held responsible for all assignments and for whatever is given in class.

If you need to be late for a good reason, (like avoiding a deadly gas attack, or zombie apocalipse) just sneak in quietly, but remember, I often give quizzes at the beginning of class and don't give make-ups.

Office hours:    Monday, Tuesday Wednesday  -- an hour before class
                        Monday, Tuesday                     -- an hour before class and an hour after class as needed.

Call to ask questions or to schedule an office hour at another time:
      (509) 326-5813       (Calls before 7:30 am or after 10:00 pm result in automatic F for the week :-)

Please minimize cell phones ringing.
If you need to leave the class in session for any reason, just quietly sneak out then back into of class. No need to interrupt the class asking if you can leave.

**********************    Assignments **********************************************************************

Assignment 1

You are going to write a series of matlab.m files. We will uses these
files to demonstrate basic programming, functions, and the trade offs between the different types of error.

When you are finished with your project, place all of your .m files in
a zip file and upload that file to moodle.

All programs should have complete help sections where the parameters and return variable of each program is fully documented.

You may work in groups of up to 3. If you do work in a group, include a file called README.txt in your zip file that lists all of your group members.

PART I: A simple function

Please write simple.m. This file should define a simple function,

PART II: A complex function

Please write student_df_3.m, a file that outputs the probability
density of student's t distribution with 3 degrees of freedom.

student_3(x) = 6*sqrt(3) / ( pi * (3 + x^2)^2)

PART III: Anti-derivative functions

Write ad_simple.m and ad_student_df_3.m that are the anti-derivatives of the functions found in simple.m and student_df_3.m, respectively.

PART IV: Closed integral

Write closed_integral.m. This function should compute the value of a closed integral over a and b. The function should have as parameters a function that is the anti-derivative, and the values a and b.

PART V: Riemann Sum


If you recall, the Riemann sum is a great way to estimate the value of an integral. Please write a program called riemann_sum.m. This program should have as parameters: the function whose integral we are estimating, the min x, the max x, and the width of the base of the rectangles. This program should output the reimann Sum in the specified range ( you can use left, right, middle or trap....). (For accuracy's sake, please use the vector builder notation).

PART VI: Compare

Write compare.m, a program that compares riemann sum and closed integral. The program should have as parameters the function and its anti-derivative. You can have more parameters if you like.

The program should then run riemann_sum.m with various sizes of the base and compare that with the result of closed_integral.m. This function should also produce a well labelled graph of the errors as a function of the size of the bases fed to reiman_sum.m.

In comments at the bottom of the program, answer the following questions.
1) What base size worked best for simple.m? What base size worked
best for student_df_3.m? What guided your choice?
2) Would you use reimann_sum in industry? Why or why not?

Parts I-IV: 10 points each. Functions should work and be well labeled.
Part V: 30 points. Should work correctly and be well labeled.
Part VI: 30 points. The function should work correctly and be
well labeled ( 15 points ). The comments at the bottom of the program
should be based off of the data generated and be reasonable given
the data. The commands should also be clear. Imagine you are explaining
this to someone who is not a matlab programmer (Gary Glaze? yeah, sure,
why not. In fact, I may just ask Glaze to grade this section ).
Assignment 2
Part 1
Please zip up all of your .m files and submit the zip to moodle. All .m files need to have comments that the top that explain return variable and the parameters of the function.

1. Please write a function that computes the Fibonacci numbers. The first line of the file should be: "function numbers = fib ( n )". The file should be called fib.m. Running the function should return a vector with the first n Fibonacci numbers. ex: fib(6) should return "ans = 1 1 2 3 5 8". You might need to use a for loop for this one.
2. Please write a function that computes the harmonic series. The first line of the file should be "function numbers = harm( n )". The file should be called harm.m. Running the function should return a vector with the first n harmonic numbers. ex: harm(4) should return "ans = 1 .5 .333333 .25". You might need to use a for loop for this one.
3. Please write a function that computes the dot product between two vectors. The first line of the file should be "function product = dotproduct(x,y)". The file should be called dotproduct.m. Running the function should return a scalar that is the dot product between
the two vectors x and y. ex: dotproduct([1 2 3 4], [5 6 7 8]) should
return "ans = 70".
4. Please write a function that estimates the inner product of two functions. The first line of the file should be "function product = innerproduct(f,g,x)". f and g should be functions that have one parameter (such as cos, sin) and x should be a vector. The file should be called innerproduct.m. Running the function should return a scalar that is the dot product between f(x) and g(x). ex: innerproduct('cos', 'sin', [1 2 3 4]) should return "ans = 0.4312".

Assignment 2
Part 2
PART 2: Roots


Let us imagine that we have a collection of points in R^2 (ie: points in 2 dimensional points. Let us call this collection C = { (x_1,y_1), (x_2,y_2),...,
(x_n,y_n) }. I would like you to find the equation of a line that
best fits the data. In other words I want you to find a function, f(x)
that predicts y with a high degree of accuracy. We often say that vector x = {x_1, x_2,..,x_n} and vector y = {y_1, y_2, ..., y_n}. We say that x and y are the data set vectors.

We thus want to find a function that minimizes (y_1 - f(x_1))^2 +
(y_2 - f(x_2))^2 +....+ (y_n - f(x_n))^2. Now, we make a small assumption.
We assume that f(x) = mx. We assume that f is a line through the origin.
So, we now want to minimize the equation: (y_1 - m*x_1)^2 +
(y_2 - m*x_2)^2 +....+ (y_n - m*x_n)^2.

How do we find the min of an equation? We set the derivative of the
equation to zero and solve. The derivative of the equation with
respect to m is: -2*x_1(y_1-m*x_1) -2*x_2(y_2-m*x_2) - ... - 2*x_n(y_n-m*x_n).
If we can find the root of this equation, we can find the m that minimizes the error, and thus the slope of the line that best fits the data.

A more compact form of the equation is:
d_error(m) = Sigma -2*x_i (y_i - m * x_i).

STEP 1: (20 points)

Write a function called derivative_error.m. It should take a vector
of x's, a vector of y's, and a slope. It should return the value of the
derivative of the error function above at m. The first line of the file should read "function der = derivative_error(x,y, m)" where x and y are vectors and m is the slope of a line through the origin. der is the derivative of the error function at m.

STEP 2: (30 points)

Our current root finding functions ( for newton's method and the bisection method ) find the roots of a function with one variable. That is functions that are of the form f(x), where x is the variable.

But now we need to find the root of a function ( like derivative_error ) that has more than one parameter! So we need to modify one of our current root finding functions to take
into account our two extra vectors, x and y. We need a new file!

Write a file called find_error_root.m. The first line of this function should be "function roots = find_error_root(x, y, m_start, m_end)" where x and y are the data set vectors, and m_start and m_end are the end points of a region of slopes we are examining. The program should either use newton's method or bisection to find a root of the derivative_error function in the region of m_start to m_end.

STEP 3: (20 points)

In comments at the bottom of find_error_root.m, I want you to
answer the following questions.

1) Which method of finding the root did you use? Why?
2) what is the best fit slope for the data: x=[1 2 3 4 5]; y=[1 2 3 4 5];
3) What is the best fit slope for the following data:
x = [34 30 31 32 33 28 27 30 43 37 30 22 33 47 33 25 30 19 24 21 25 24 42];
y = [0 1 0 0 0 0 0 0 1 1 1 0 0 2 0 0 0 0 0 0 2 0 1];


Zip up all files used and submit the zip file

Assignment 3