OBJECTIVES
ME 18A
Introduction to Engineering Computation
ASSIGNMENT 6 USER-DEFINED FUNCTIONS
• To practice structured programming using the top-down design approach.
• To learn how to develop user-defined functions in MATLAB.
• To develop an understanding of the Bisection and Newton-Raphson methods for equation root finding.
GENERAL INSTRUCTIONS
In the following exercise, you will develop two MATLAB functions (m-files) that can be called from the MATLAB Command Window or another program (script m-file).
Use the top-down approach with pseudo-code in designing your functions. Transform the pseudo-code into MATLAB statements and comment lines. The structure of your function m-files should be as follows:
% function definition line
% H1 comment line (include a meaningful H1 comment line)
% Lastname, Firstname
% ME18A Assignment 6, Problem X
% Function Title: Roots of an Equation using the XXXXX method
% date
% Function code :
% End of Function
PROBLEM 1 – Bisection Method
Develop a MATLAB function A61lastname that finds the root of an arbitrary function of a single variable using the bisection method. The function to be evaluated will be defined as an anonymous function in the command window or m-file. Your function will then be called to solve for the root. The user will pass the function, an initial bracket, the error tolerance and the maximum number of iterations to be performed. Your user-defined function will return the root, the value of the function evaluated at the root, the estimated error and the number of iterations used.
Function Verification
After debugging and finalizing your bisection method function, test it with the following functions and initial brackets. Determine the roots to within 0.005% error. Perform the tests in the Command Window. Record the test procedure displaying the anonymous function, the call to A61lastname, and all four output arguments using a diary named A61TESTlastname.txt. Before performing the test in the Command Window, set the numerical format to long.
Rev 10/31/2019 Page 1 of 2
ME18A Introduction to Engineering Computation Assignment No. 6
Case No.
f(x)
Initial Bracket
1
𝑥2 − 9
[0 30]
2
𝑥 − cos 𝑥
[-30 30]
3
𝑒−𝑥 − 10𝑥
[-30 30]
4
𝑥10 − 2
[0 30]
PROBLEM 2 – Newton-Raphson Method
Develop a MATLAB function A62lastname that finds the root of an arbitrary function of a single variable using the Newton-Raphson method. The user will define the function and the first derivative of the function as anonymous functions in the command window or m-file. Your user-defined function will then be called to solve for the root. The user will pass the function, the first derivative, an initial guess, the error tolerance and the maximum number of iterations to be performed. Your user defined function will return the root, the value of the function evaluated at the root, the estimated error and the number of iterations used.
Function Verification
After debugging and finalizing your Newton-Raphson function, test it with the following functions and initial guesses. Determine the roots to within 0.005% error. Perform the tests in the Command Window. Record the test procedure displaying the anonymous function, the call to A62lastname, and all four output arguments using a diary named A62TESTlastname.txt. Before performing the test in the Command Window, set the numerical format to long.
Case No.
f(x)
f(x)
Initial Guess x0
1
𝑥2 − 9
2x
7
2
𝑥 − cos 𝑥
1+sin𝑥
6
3
𝑒−𝑥 − 10𝑥
−𝑒−𝑥 − 10
0
4
𝑥10 − 2
10𝑥9
1
Compare the results from the two root finding methods.
Submit a single ZIP file containing the two function m-files and the two verification diaries using the Assignment 6 submission link on iLearn.
To receive credit for this assignment, a live demonstration of the code generated must be performed for the ME18A Lab Teaching Assistant during the lab session. Be sure to sign the lab attendance sheet when the demonstration is performed.
Rev 10/31/2019 Page 2 of 2