School of Engineering
Computational Engineering Analysis AERO2463
Assignment 4
4.1 Curve Fitting - Polynomial Fitting of Data
4.2 Finite Differences and Differential Equations
School of Engineering
Computational Engineering Analysis - AERO2463
Assignment 4.1
Curve Fitting - Polynomial Fitting of Data
Assessment:
The MATLAB Assignment 4.1 is worth 5%.
Objectives:
Generate curves by fitting data using polynomials
Submission Instructions:
LastName_StudentID_polynomial_fitting.m (write your function in this one)
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.
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.
Coding Requirements
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).
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.
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.
In this case, the fitting will not be perfect with any polynomial, and the code will have to plot all the required polynomials:
The two plots produced will look like the ones below:
The small amount of scatter in the data leads to dramatically different predictions of the data at an x value of 2.5 (the last known data point was 2.3).
The lesson to be learnt here is: Never extrapolate data with a polynomial unless you know the data is governed by a polynomial of a given order.
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 plots fitted polynomials as specified 60%
Correctly plots interpolated/extrapolated y-value for user input x-value in separate plots 20%
If data set mathches a polynomial, higher-order polynomials are not fitted or plotted 20%
Code does not produce error message if x and y are of different size. -20%
Code does not produce error message applied to data set with less than 11 points. -20%
Appropriate deductions for not meeting requirements (e.g. wrong file name) -20%
IMPORTANT: HINTS (DOs and DON’Ts)
DO NOT PLACE CODE ABOVE YOUR FUNCTION DEFINITION. This is mixing a script and function and cannot be done. The user will pass the data they wish to use to the function via the input arguments!
DO NOT STORE SOMETHING TO X, X2 OR Y IN YOUR FUNCTION. If you store values in x, x2 or y in your function, then it will overwrite what the user specified for x, x2 and y.
TEST YOUR CODE: Your code may fit well a provided set of data (but it may not fit for another data set). Make up your own data (that you know the answer to) and test that your function returns the correct results.
TUTORIALS: Please do the learning exercises in the tutorials. They will teach you the skills necessary to do this task.
School of Engineering
Computational Engineering Analysis - AERO2463
Assignment 4.2
Finite Differences and Differential Equations
Assessment:
The MATLAB Assignment 4.2 is worth 5%.
Objectives:
Solve a mechanics problem to calculate the displacement, strain and stress in a 1D elastic structure using the Finite Difference Method.
Submission Instructions:
The m-file should be named as: Last Name_Student ID_FiniteDifference.m (write your function in here)
Please note: You will have to include the same function name as the above file name inside your function file to run it.
AERO2463 Computational Engineering Analysis: Assignment 4, 2018
Problem:
Consider the example of a compression or tensile mechanical test. A sample one-dimensional rod or beam type structure is placed in the tensile test machine. One end is fixed whereas the other end is subject to an imposed displacement. This displacement generates strains and stresses in the structure.
We consider the 1D configuration described by the figure below:
We consider a static problem, where a specific displacement is imposed on a simple 1D rod structure. One end of the rod is fixed (zero displacement) at x=0, and a displacement of value D is imposed at the other end at x=L.
Objective: The aim is to determine the displacement field in the structure for a given displacement at the end point, as well as the strain and stress field, using a second order Finite Differences (FD) method.
The strain ( ) relation is given by the following equation for the current 1D problem:
The stress is then computed as:
where E is the Young’s modulus.
The 1D equation of equilibrium in the absence of an external force is given by. 0
which in 1D corresponds to
This last equation needs to be solved with the Finite Differences method. The structure of length L will be discretized (divided) into Nx elements (segments), as shown in the figure above. Here are some hints describing the intermediate steps required to solve this problem numerically.
AERO2463 Computational Engineering Analysis: Assignment 4, 2018
Construct a square matrix of dimension Nx × Nx to write the linear system .
Then, you can start programming in MATLAB.
Your function must produce 3 plots (displacement, strain and stress along the length of the rod structure) and return 2 vectors X and U, respectively, for the discretized domain and the displacement. Since the strain and stress are calcualted for each 1D segment (element), these can be plotted against the displacement at the upper node for that corresponding segment.
Assessment Criteria
Follow Us