Simulation and Optimisation in Engineering AERO2598
Simulation and Optimisation in Engineering
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Simulation and Optimisation in Engineering
AERO2598
Assignment 2
2.1 Numerical Integration
2.2 Curve Fitting - Polynomial Fitting of Data
School of Engineering
Simulation and Optimisation in Engineering
AERO2598
Assignment 2.1
Numerical Integration
Assessment:
Assignment 2.1 is worth 15%.
Submission:
Location: AERO2598 Canvas Site
Objectives:
Evaluate numerical integrals of functions using a combination of Simpson’s Rules and
Trapezoidal Rule using Matlab program.
Submission Instructions:
There are two m-files provided to you on Canvas. One is the template for the function file
you are to submit, and the other is a script file that you may use to test your function file
(Please do not submit the script file used for testing your function).
Submit a single function m-file via Canvas before submission due date.
Note that the Last Name and student ID are required as a comment line at the beginning of
the m-file, but after the function header (i.e. use the % symbol at the beginning of
commented lines).
The m-file submitted on Canvas should be named as:
o LastName_StudentID_composite_integration.m (write your function in this one)
Do not zip any files.
Failure to submit before the due date will require a reasonable explanation and approval
from the Course Coordinator before any late submission can be accepted.
Page 1
AERO2598 Simulation and Optimisation in Engineering: Assignment 2
Page 2
RMIT late assignment submission policy will apply (10% deduction of marks for each day
delay).
Use the Special Consideration process for any submission later than one week.
Submit only the files related to Assignment 2.1.
Background
Often when dealing with data from experiments or computations, the data will be discrete and may
not have uniform spacing. We could use trapezoidal integration on it but it would be more
beneficial (generally more accurate) to use the appropriate Simpson’s rule in regions where we have
a consecutive interval spacing that is uniform or constant.
As an example, given the following x and y data to integrate, there are regions with uniform spacing
that can use Simpson’s rule.
x y
0.000 0
1.0000 0.8415
2.0000 0.9093
3.0000 0.1411
4.0000 -0.7568
4.5000 -0.9775
5.0000 -0.9589
5.1000 -0.9258
5.2000 -0.8835
5.3000 -0.8323
5.8000 -0.5507
6.1000 -0.1822
Simpson’s 1/3rd rule, with step=1
Simpson’s 1/3rd rule, with step=0.5
Simpson’s 3/8th rule, with step=0.1
Trapezoidal rule, with step=0.5
Trapezoidal rule, with step=0.3
Page 3
AERO2598 Simulation and Optimisation in Engineering: Assignment 2
Task
Write a MATLAB function that will integrate discrete data, using Simpson's rules (1/3rd and 3/8th)
where possible and trapezoidal rule elsewhere.
The following function header MUST be used with LastName and StudentID being replaced
with your last name and student number (sxxxxxxx). The m-file must be saved with the same name
as the function name for the function to work.
function int_y_dx = LastName_StudentID_composite_integration(x,y)
You MUST use the following variables for your function input arguments: x and y, where y
represents a vector of values dependent on the corresponding values in vector x. The user of the
function will input two vectors into your compsite_integration function. Please see the test
script for an example of the input arguments for your function.
You MUST use the variable int_y_dx for your function output. This will represent the numerical
integral of area covered by x and y.
Coding Requirements
1. You may utilise the MATLAB trapz() function as part of your program. But for
Simpson’s 1/3rd and 3/8th rules you should use the basic equation given on page 6. (for
example, You must NOT use the integral() function)
2. You should not make any sub-functions inside your function.
3. If the x and y data given to the function is not the same length, the function MUST use the
error() function to give the user an error message and exit.
4. There must NOT be any user interaction within the function (i.e. no use of input() ) .
5. Your code MUST return the SIGNED area under the graph (i.e., the ACTUAL value of the
integral of y with respect to x, NOT absolute area). NOTE: The signed area above x-axis
(bounded by the x-axis and a curve) is positive, whereas the signed area below x-axis
bounded by a curve is negative. Conversely, an unsigned area is the absolute value of the
integral, meaning that the unsigned area below the x-axis bounded by a curve is positive.
6. To attain maximum accuracy, your function must avoid using trapezoidal rule
wherever possible. For example, if there is a run of 4 equally spaced INTERVALS,
then you should use 2× Simpson 1/3rd rules rather than 1× Simpson 3/8th and 1x
trapezoidal. To help you, the following table indicates which rule must be used for a
given number of consecutive equal intervals (as opposed to points).
AERO2598 Simulation and Optimisation in Engineering: Assignment 2
Page 4
No of consecutive
equal intervals
Quadrature rule
1 1× trapezoidal
2 1× Simpson’s 1/3rd
3 1× Simpson’s 3/8th
4 2 × Simpson’s 1/3rd
5+ Use the rule for 3 first since it is most accurate, then
apply the appropriate rule for however many
intervals are left over.
e.g. for 7 intervals, you would use 1× Simpson 3/8th,
leaving 4 left over, for which you would use 2×
Simpson 1/3rd.
Another example how you code your function is shown in the following figure.
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 described
herein before submitting it.
Assessment
Correctly performs integration as specified 70%
Correctly checks for equality of successive intervals as specified 20%
Correctly performs error check on input values 10%
Appropriate deductions for not meeting requirements -20%
Page 5
AERO2598 Simulation and Optimisation in Engineering: Assignment 2
HINTS
(i) Do not place any code above your function header. This is mixing a script and function
definition and cannot be done. The user will pass the data they wish to use to the function via its
input arguments.
(ii) Do not store something to x or y in your function. If you store values in x or y in your
function, then it will overwrite what the user specified for x and y.
(iii) Do test your code. Maybe your code should return a value close to that of trapz (but maybe not
for the data given). Make up your own data (that you know the answer to the integration) and test
that your function returns the correct results.
(iv) Do the learning exercises in the tutorials. They will teach you the skills necessary to do this
task.
AERO2598 Simulation and Optimisation in Engineering: Assignment 2
Page 6
Integration Formulae
Trapezoidal Rule:
݂ሺݔሻ݀ݔ ൌ ଶ ሾ݂ሺݔଵሻ
2݂ሺݔଶሻ+2݂ሺݔଷሻ…2݂ሺݔିଵሻ 2݂ሺݔሻ ݂ሺݔାଵሻ]
i.e. repeated units of ݂ሺݔሻ ݂ሺݔାଵሻ overlapping by one term
Simpson’s 1/3rd Rule:
݂ሺݔሻ݀ݔ ൌ ଷ ሾ݂ሺݔଵሻ
4݂ሺݔଶሻ+2݂ሺݔଷሻ 4݂ሺݔସሻ 2݂ሺݔହሻ….. 2݂ሺݔିଵሻ+ 4݂ሺݔሻ
݂ሺݔାଵሻ]
i.e. repeated units of ݂ሺݔሻ 4݂ሺݔା!ሻ ݂ሺݔାଶሻ overlapping by one term
Can only be used if number of interval ( n) is an even number.
Simpson’s 3/8th Rule:
݂ሺݔሻ݀ݔ ൌ ଷ଼ ሾ݂ሺݔଵሻ
3݂ሺݔଶሻ+3݂ሺݔଷሻ 2݂ሺݔସሻ 3݂ሺݔହሻ 3݂ሺݔሻ
2݂ሺݔሻ…..݂ሺݔାଵሻ]
i.e. repeated units of ݂ሺݔሻ 3݂ሺݔାଵሻ 3݂ሺݔାଶሻ ݂ሺݔାଷሻ overlapping by one term
Can only be used if n is a multiple of 3 (can be odd or even).
݄ ൌ ܾ െ ܽ݊
In the above equations the number of segments/intervals is equal to the value of n because
we start at ݔ.
݊ݑܾ݉݁ݎ ݂ ݅݊ݐ݁ݎݒ݈ܽݏ ൌ ݊
However, usually the number of points used in the interval has the following relationship
with the number of segments/intervals it is broken up into, illustrated in the below figure.
݊ݑܾ݉݁ݎ ݂ ݅݊ݐ݁ݎݒ݈ܽݏ ൌ ݊ݑܾ݉݁ݎ ݂ ݅݊ݐݏ െ 1
a = 0.2 b = 1
No. of points = 5
No. of segments = No. of points-1 = 4
School of Engineering
Simulation and Optimisation in Engineering
AERO2598
Assignment 2.2
Curve Fitting - Polynomial Fitting of Data
Assessment:
The MATLAB Assignment 2.2 is worth 10%.
Objectives:
Generate curves by fitting data using polynomials
Submission Instructions:
There are two m-files provided to you on Canvas. One is the template for the function file
you are to submit and the other is a script file that you should use to test your function file
(please do not submit the script file used for testing your function).
Submit a single function m-file via Canvas before submission due date specified above.
Note that the Last Name and Student ID are required as a comment line at the beginning of
the m-file, but after the function header.
The m-file submitted on Canvas should be named as:
o LastName_StudentID_polynomial_fitting.m (write your function in this one)
Do not zip any files.
Failure to submit before the deadline will require a reasonable explanation and approval
from the Course Coordinator before any later submission can be accepted.
RMIT late assignment submission policy will apply (10% deduction of marks for each day
delay).
Use the Special Consideration process for any submission later than one week.
Submit only the files related to the Assignment 2.2.
Background
When dealing with data from experiments or computations, the data will be discrete and not have
uniform spacing. Fitting the data within a polynomial can help to predict outputs and to deal with
the data in further calculations.
AERO2598 Simulation and Optimisation in Engineering: Assignment 2
The aim of fitting your discrete data to a polynomial is to be able to predict a value of the dependent
variable in terms of the independent variable for those values which do not have corresponding
discrete data. Depending on the relationship of the variables, polynomials of different orders should
be used to find a good fit between the polynomial and the real data. In the figure shown below, real
measurements of liquid water density were taken in increments of 10 °C and several polynomials
were calculated using polyfit()to see the correlation between them and the discrete data.
After a polynomial is obtained, we can try to interpolate or extrapolate y-values for any x-value. Be
careful if you are extrapolating outside the boundaries of your discrete data, the polynomial could
fit well inside your data, but maybe it does not represent well the reality outside them. What is the
density of liquid water (pressurized) for temperatures greater that 100°C? In the figure above, we
can try to extrapolate them, but we do not know if the physical phenomena will be still fitting our
polynomials.
Task
Your task is to write a function which takes two vectors, x (independent variable) and y (dependent
variable), and fits polynomials from order 1 (linear relation) up to order 10 (or the maximum order
possible for the data size). The function should plot the x and y data with markers (no line) and plot
each fitted polynomial (with lines), all on the same plot axis. In a separate figure (or in separate
plots within one figure using subplot), plot the interpolated/extrapolated y value for a given x-point
defined by the user.
Page 2
AERO2598 Simulation and Optimisation in Engineering: Assignment 2
Page 3
Coding Requirements
1. Use the MATLAB built-in function polyfit.
2. You should not make any sub-functions inside the function.
3. Ensure the x and y data given to the function have the same length. If it is not the case, the
function MUST use the error() function to give the user an error message and exit.
4. There must NOT be any user interaction within the function.
5. To fit a function with a polynomial of an order (also called grade or degree) “n” there
must be at least “n+1” points. Your code MUST detect if the number of points is not
sufficient enough and show an error message before trying to calculate wrong polynomials.
6. The user will enter the x and y vectors to the function from the script, and it will also specify
a point “x2” which the function will use to extrapolate its value y(x2) using all the
polynomials calculated.
7. You should create extra x-points in your function to make the polynomial line plots
smoother using more points.
8. If the given data fits exactly a polynomial of an order lower than 10, your function MUST
NOT plot the polynomials of a greater order. You could use a tolerance to test this.
Hint: In order to fulfill requirement number 8, you should compare your coefficients with a certain
tolerance (e.g 10e-6) if you calculate your polynomial, and from certain coefficient all the higher
order coefficients (beyond it) are lower than the tolerance, you may assume that it is a perfect fit for
a polynomial, excluding those higher order coefficients (i.e. they are equal to zero).
Page 4
AERO2598 Simulation and Optimisation in Engineering: Assignment 2
Examples
Example 1
In this first example, the data fits exactly an order 2 polynomial, therefore the code will only plot
two polynomials (order 1 and 2)
The user will produce a call to the function in the workplace such as:
In this case, the function output will produce two plots. The first one is the polynomial fits with the
original data. As the polynomial of order 2 fits exactly the function, only 2 polynomials have to be
plotted.
Page 5
AERO2598 Simulation and Optimisation in Engineering: Assignment 2
The second plot output contains the predictions at position x2 (x-axis is polynomial order, y-axis is
the predicted y value). You have to plot only the number of predictions of the plotted polynomials.
As in this case the original data was a perfect x^2 relation, we expect all polynomials of this order
or greater to perfectly fit the data and extrapolate the same value. In this subplot you can choose
between plotting the guesses of all the polynomials or plot only those guesses for the polynomials
shown in the previous figure.
Example 2.