AERO2598 Matlab Assignment 1
AERO2598 Matlab Assignment 1
Assignment 1
1.1 Roots of Equations - Iterations & Looping
Assignment 1.1
Roots of Equations - Iterations & Looping
Assessment:
Assignment 1.1 is worth 10%.
Submission:
Your task is to write two Matlab programs to find the root of an equation using two
different methods:
The Bisection Method
Dekker’s method (which combines bisection and secant methods)
You should submit TWO m-files, a different file must be used for each method. You must
write your name and student ID in the specified lines in the header.
At the beginning of the m-file code, students must write their name and student ID as a text
line (i.e. with a % symbol at the beginning of this line in the m-file).
Submit your MATLAB m-files via the Canvas before due date specified on Canvas.
It is student’s responsibility to ensure that all work required is submitted via Canvas.
o LastName_StudentID_Matlab_Assignment1_Bisection.m
o LastName_StudentID_Matlab_Assignment1_Dekker.m
Failure to submit the assessment solution file through Canvas before the end of submission
date and time, will need special consideration approval from the course coordinator to re-
submit. In that case, RMIT policy of late assignment submission will apply (10% deduction
of marks for each day delay).
Please only submit code related to the Assignment Component, not for the tutorial.
Page 1
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
Background
You have been shown some common methods of finding roots to non-linear equations in the
lecture. Each method has its strengths and weaknesses.
The bisection method is easy to use and robust, but often converges slowly, i.e., it may need
many iterations.
The secant method generally converges faster but there are circumstances when it will not
converge to the root or converge slower than the bisection method.
Newton's method converges faster than the prior two methods but it requires knowledge of the
function’s derivative which may be hard to derive or unknown if, working with numerical data. It
may fail to converge, or converge slower than the bisection method for some functions and initial
guesses.
Some useful information on these methods can be found in lecture notes and also on internet
sources (e.g., Wikipedia). This information will help you significantly in developing your
MATLAB code
Anonymous functions
MATLAB provides a simple, convenient and effective way to define a function that we can use
throughout a program. These functions are called anonymous functions. An anonymous function is
convenient because it is contained within the program and does not have to be stored in a separate
file. Anonymous functions are created using the “@” operator. They can only contain a single
executable statement. We will learn how to write anonymous functions later in the course. For now,
you may want to use anonymous functions to solve the problem because it will make your code
much nicer. Additional information about how to use the anonymous functions is provided further
below.
Page 2
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
• The Bisection Method is also known as binary chopping, interval halving or Bolzano’s
method.
• It is one type of incremental search methods where the search interval is divided in half.
• The location of the root is then determined as lying at the midpoint of the subinterval
See the lecture notes and also http://en.wikipedia.org/wiki/Bisection_method
Hint: A way to test if the sign of two numbers A and B are different is: if A*B < 0
NOTE: Dekker’s method is fairly involved; please do not stress if you find it too difficult.
Note that you can still get 80% (distinction) without attempting Dekker’s method.
We are treating Dekker’s method an advanced “honours question”.
1. Test if abs(f(a))<abs(f(b)). If this is true then we swap a and b. This test will
ensure that b is always a “better” guess of the root than a, because the function will be closer
to zero at b than it is at a.
3. Make a guess of the root using the Secant Method, call it s2. Recall the Secant Method
requires two previous guesses to obtain a new guess. For the two previous guesses, we use
the previous two values of b (for the very first iteration, because we don’t have two previous
b’s, we use a and b)
4. If s2 is between b and s1, then s2 becomes the new b: b_new = s2 If not, then s1
becomes the new b: b_new = s1
Page 3
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
5. If f(b_new) has opposite sign to f(a), then “a” stays the same: a_new = a
If not, then we set the new “a” to be the old “b”: a_new = b
6. If abs(a_new – b_new) < tolerance, then break out: we are done. Do not forget
to set “s” to the value of the last guess: s = (a_new + b_new) / 2
7. If we are not done, save the previous value of b: b_old = b Then set a = a_new and b
= b_new, and loop back up to step 1.
Task Description
Your task is to write two MATLAB programs to find the root of an equation using two different
methods:
Assessment Criteria
Matlab Code: To assist with marking, you MUST comply with ALL of the following
requirements. Failure to comply will result in a 20% penalty of total marks.
The m-files contain names of variables that you MUST use to aid marking and are listed again
below:
a: lower bound
b: upper bound
Maxiterations: Maximum number of iterations your program may take before it gives
up trying to find the root. You may choose an appropriate value (say 50)
Page 4
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
Tolerance: accuracy (uncertainty bound) the program must continue iterating to, unless
it exceeds Maxiterations. Choose an appropriate value, such as 1E-12.
The next component is optional. It may be helpful to develop a nicer code. Check the additional
documentation if you are interested in using it.
My_Function: anonymous function that defines the equation to be solved. You have
been provided with an anonymous function already, but feel free to change it if you wish
following the instructions in the m-files.A function has been added in the m-template,
together with an example of how to use it.
Your code should find the roots of any equation, so keep in mind that, in order to mark you, the
tutors will change the equation inside “My_Function”. Your code has to be able to find the roots of
any equation. If your code finds the roots only for a given function and it does not work for a
different function, it will be considered not valid.
As an example, for the two different equations provided above, the code should find that the first
equation has a root for d=±2. If you use the second function, your code should find the root to be
d=0.693.
Note that if you have an even number of roots inside your boundaries the code will be unable to
find a root. So consider that you have to define your boundaries in a way that an odd number of
roots is contained within them. As an example, in the first equation, represented in the figure below,
your code will work if you define a lower boundary a1=-1 and an upper boundary of b=8 since
My_Function (a1)* My_Function (b)>0. However, it won’t if your boundaries are a2=-
8 and b=8 since My_Function (a2)* My_Function (b)>0.
Page 5
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
You can call your function at any time later in the code. It as simple as writing
My_Function(Value).
As an example if I write:
X=My_Function(1)
X will be 3 if the function implemented was d^2+2 or 50 if the function implemented was 75*d^3-
25
You may make the programs interactive by prompting the user for the initial values of a, b,
Maxiterations, and Tolerance.
If you do so, you MUST do it using the MATLAB input command, i.e:
a = input(‘….’);
b = input(‘….’);
Maxiterations = input(‘….’);
etc.
You must assume that for the initial values of a and b, f(a) and f(b) have opposite sign (i.e. a
and b bracket a root; this is a necessary condition for these methods to work). The tutor will check
your function with a random function, and with both an interval containing and non-containing a
root.
Page 6
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
At the end of execution, the variable root MUST contain the final estimate of the root.
If both f(a) and f(b) have the same sign, there are no roots in the interval chosen (or there are a
pair number of roots). In this case, you have to plot a message that tells that the method cannot be
used. Hint: use fprintf and an if-else statement.
Page 7
School of Engineering
Assignment 1.2
Matlab Functions: Surface Derivatives and Plotting
Assessment:
Assignment 1.2 is worth 15%.
Submission:
You will be provided with two MATLAB files:
• Main_Test_Script_surface_derivative.m (Please do not submit this file.
This is the main script you will use to test your function)
• LastName_StudentID_surface_derivative.m (Please submit this file with
YOUR last name and student ID in the function header as comment and file name.
Make sure to also modify the function name within the script)
You should thus submit ONE m-file named as
LastName_StudentID_surface_derivative.m.
Submit your MATLAB m-files via the Canvas by the due date. It is student’s responsibility
to ensure that all work required for the assessment is submitted properly by Canvas.
Use the templates provided to you through Canvas. Indicate your name and ID in the
commented lines at the beginning of the file.
Do not zip the files.
Failure to submit the assessment solution file through Canvas before the end of
submission date and time, will need special consideration approval from the course
coordinator to re-submit (with reasonable explanation). In that case, RMIT policy of late
assignment submission will apply (10% deduction of marks for each day delay).
DO NOT submit any code related to Tutorial, only the Assignment
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
Objectives:
Generate surface plots of a function and calculate derivative (gradient) of a function
Task Requirements:
Your task is to write a MATLAB FUNCTION that displays a surface plot of a given
anonymous function of two variables. Your function MUST have the following form:
Function Output Arguments
[minmax1Z,minmaxDX,minmaxDY] =
LastName_StudentID_surface_derivative(xlims,ylims,nx,ny,myfun)
Function Requirements:
It MUST be named using the following notation:
LastName_StudentID_surface_derivative
It MUST NOT contain any loops (e.g., for or while loops)
It MUST NOT contain any user interaction (input, dialog boxes, etc.)
It MUST NOT calculate the derivatives symbolically
YOU MUST NOT, under any circumstances, re-define the input/output
arguments in your function. The whole point of writing a function is that the user
will pass in the values that they choose as the inputs to your function.
Output Requirements:
Display a surface plot of the function.
Display surface plots of the partial derivative of the function in the x and y directions,
which MUST be calculated numerically (NOT SYMBOLICALLY), in two other plots.
Axes MUST be labeled appropriately, and each plot MUST have a title.
On each of the three plots, a marker MUST be drawn on the maximum and minimum
value. If there are multiple maximum or minimum values, you only need to find and plot
one point.
The x and y positions of the maximum and minimum values MUST be returned to the
user as three arrays, as in the tables below:
Page 1
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
Function Input Arguments:
MATLAB
Type Explanation
Variable
xlims Vector of length 2 Minimum and maximum x values for the plot.
ylims Vector of length 2 Minimum and maximum y values for the plot.
MATLAB
Type Explanation
Variable
First row must contain the x and y position of the function’s
minimum.
minmax1Z 2×2 matrix Second row must contain the x and y position of the
function’s maximum.
NOTE: If there are multiple minima/maxima, you only need to find and return
the co-ordinates of one.
Page 2
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
NOTE: The main script and function script are TWO SEPARATE FILES. The main script
calls the function script via Line 7 and the two files MUST be in the same folder in your
computer for MATLAB to find it. This folder MUST also be the current MATLAB working
directory.
Outputs – 3 Plots:
, , ,
The graphical output of your results will appear as shown in the figure below. The sub-plotting
function ( subplot(n_rows, n_columns, position) ) has been used in the example below to draw
all plots in the same figure window. You may plot in separate windows instead if you wish.
Page 3
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
Maximum
Minimum
For each plot, you must also indicate the maximum and minimum values by marking them as shown in
the figure above in blue. See the function scatter3(Px,Py,Pz) to create a point on a 3D plot.
Lastly, you MUST display the final results (x and y position of the min and max) as indicated in section
“Function Output Arguments” in the command window as shown below (Your results may differ).
-3 3
-3 -3
minmaxDX =
3 -3
3 3
minmaxDY =
-3.0000 -2.3684
3.0000 -2.3684
Page 4
AERO2598 Simulation and Optimisation in Engineering: Assignment 1
Things to consider
How do I create an array of the x points? (Hint: look up the linspace function in the help,
then use it in combination with meshgrid)
How do I create an array of the y points?
How do I evaluate the anonymous function at all x and y points?
How do I (numerically) take a derivative of an array?
How do I find the minimum/maximum values of an array?
How do I plot a surface?
How do I plot points in 3D?
How do I put multiple plots on the same axes?
Page 5