0% found this document useful (0 votes)
34 views14 pages

AERO2598 Matlab Assignment 1

Uploaded by

Wong Siu Lam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views14 pages

AERO2598 Matlab Assignment 1

Uploaded by

Wong Siu Lam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

School of Engineering

Simulation and Optimisation in Engineering


AERO2598

Assignment 1
1.1 Roots of Equations - Iterations & Looping

1.2 Matlab Functions: Surface Derivatives and Plotting


School of Engineering

Simulation and Optimisation in Engineering


AERO2598

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).

 Use the templates provided through Canvas.

 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.

 The m-files submitted on RMIT Canvas should be named as:

o LastName_StudentID_Matlab_Assignment1_Bisection.m

o LastName_StudentID_Matlab_Assignment1_Dekker.m

 Do not zip any 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. 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.

Overview of Bisection Method


The bisection method starts with a lower bound and upper bound that must bracket the root. The
bisection method then guesses the root is in the middle of the bound, and tests the function at this
point. It then sets a new lower or upper bound equal to the current guess, dependent on whether the
root is between the lower or upper bound and the current guess.
The Bisection Method is an example of a Bracketing Method:
• Bracketing methods require two initial guesses for the root.
• These guesses must “bracket” the solution, i.e. the guess must be located on either side, of
the root.
• Bracketing methods are based on the fact that functions typically change sign in vicinity of a
root.
In general f(x) is real and continuous in the range xl to xu and f(xl) and f(xu) have opposite signs, i.e.,
f(xl)f(xu)<0, then there is at least one real root between xl and xu.

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

Overview of Dekker's Method


Dekker's method is an algorithm that attempts to combine the robustness of the bisection method
with the convergence of the secant method. For every iteration Dekker's method guesses the next
value of the root by using both the bisection method and the secant method. If the guess of the
secant method lies between the last guess of the root and the new of the guess of the bisection
method then this guess is used. If it does not, (else) the guess of the midpoint is used.
See: http://en.wikipedia.org/wiki/Brent%27s_method#Dekker.27s_method
You may wish to insert some fprintf statements into your program to indicate which method
(secant or bisection) it is choosing at each step. Last year a lot of student’s programs incorrectly
chose the bisection method every time; for the parachute problem a correct implementation of
Dekker’s method will use a mix of secant and bisection.

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”.

A summary of the Dekker method

For each iteration we do the following:

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.

2. Make a guess of the root using the Bisection Method: s1 = (a+b)/2

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:

 The Bisection Method

 Dekker’s method (which combines bisection and secant methods)

Assessment Criteria

 Substantial attempt made to implement Bisection Method 30%


 Correct implementation of Bisection Method 50%
 Correct implementation of Dekker’s Method 20%
 Deductions as detailed below -20%

Pseudocode steps to help get you started


When creating software it is very important to ensure your program satisfies the requirements.
Requirements are often very specific as your software may need to interact with user and/or other
software components in a very specific way. Please heed the following warning and ensure your
program meets all the requirements set out below before submitting it.

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

 s: current guess of the root

 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.

It is easy to change the anonymous function if you want.

However, you must keep the initial statement “My_Function = @(d)”.


After that statement and in the same line, you can write your own function, F(d).
Examples:

My_Function = @(d) d^2-4


My_Function = @(d) 75*d^3-25

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

Simulation and Optimisation in Engineering


AERO2598

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 Name Function Input Arguments

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.

nx Scalar Number of points to plot in the x direction.

ny Scalar Number of points to plot in the y direction.

myfun Anonymous function Function that will be plotted.

Function Output Arguments:

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.

 First row must contain the x and y position of the minimum


of the function’s partial derivative with respect to x.
minmaxDX 2×2 matrix  Second row must contain the x and y position of the
maximum of the function’s partial derivative with respect to
x.

 First row must contain the x and y position of the minimum


of the function’s partial derivative with respect to y.
minmaxDY 2×2 matrix  Second row must contain the x and y position of the
maximum of the function’s partial derivative with respect to
y.

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

Getting Started (Refer to “MATLAB – Editor” Table below):


The “main” file Main_Test_Script_surface_derivative provided, is used to defined user
variables and call the function, as shown in the table below. Before writing your function, you
must declare variables that you will send to the function (Line 1-5). Adding the decimal point
before the ^ and * characters in Line 5 tells MATLAB to use element-wise operators, instead of
matrix operators. To call your function, you must call it in your main script (Line 7).

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.

Line MATLAB - Editor


1 xlims = [-3; 3];
2 ylims = [-3; 3];
3 nx = 25;
4 ny = 25;
5 myfun = @(x,y) x.^2 .* (1 - y.^2);
6
7 [minmax1Z,minmaxDX,minmaxDY] =
LastName_StudentID_surface_derivative(xlims,ylims,nx,ny,myfun);

Outputs – 3 Plots:

Plot 1: Plot “myfun” Function


Your task here is to plot the “myfun” function as a 3D plot. You will need to use the surface plot
function (surf(x,y,z)) and the meshgrid function (meshgrid(x,y)) in MATLAB. (in Tutorial 2.2)

Plot 2: Plot the Derivative of “myfun” as a function of x


You can evaluate a derivative numerically (approximately) using the following equation, where
is a small number. A good way of choosing is to make it a certain fraction of the distance
between the points of your plot (e.g. 1/10). This will ensure the calculated derivatives are
reasonably accurate.

, , ,

Note: You must not attempt to calculate the derivative symbolically.

Plot 3: Plot the Derivative of “myfun” as a function of y


You will need to repeat the exercise in Plot 2 above but this time calculating derivative as a
function of y.

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

Plot Maximum/Minimum Values

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.

Matrix Result Output

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).

MATLAB – Command Window


minmax1Z =

-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?

Assessment Criteria and Deductions


 Correctly plots the function 40%
 Correctly plots the derivatives 30%
 Correctly plots and returns minimum/maximum values 30%

 Uses loops -25%


 Evaluates derivatives symbolically -25%
 Incorrect function definition or re-defines input values in function -25%
 Failure to follow coding instructions -25%

Page 5

You might also like