assignment 2€¦ · assignment 2.1 application of functions: root finding using fixed point...

12
School of Engineering Computational Engineering Analysis AERO2463 Assignment 2 2.1 Application of Functions: Root Finding using Fixed Point Iteration 2.2 Matlab Functions: Surface Derivatives and Plotting

Upload: others

Post on 01-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

School of Engineering

Computational Engineering Analysis

AERO2463

Assignment 2

2.1 Application of Functions: Root Finding using Fixed

Point Iteration

2.2 Matlab Functions: Surface Derivatives and Plotting

Page 2: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

School of Engineering

Computational Engineering Analysis -

AERO2463

Assignment 2.1

Application of Functions: Root Finding using Fixed

Point Iteration

Assessment: The MATLAB Assignment 2.1 is worth 5%.

Submission: Use the Matlab template provided on Canvas for MATLAB Assignment 2 You should submit TWO m-files. In the m-file code, you must write your name and student

ID as a text line (comment) in both m-files. Use the templates provided to you through Canvas.

The m-files should be named as:

Last Name_Student ID_fixed_point_solve.m (write your function in here)

Last Name_Student ID_fixed_point_demonstration_script.m (write your script in here)

You must NOT rename the files. Include your last name and student ID in the .m file name.

Please note: You will have to include the same function name as the above file name

inside your function file to run it.

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

You should not submit any code related to the Tutorial 2 Material, only the

Assignment

Page 3: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

AERO2463 Computational Engineering Analysis: Assignment 2

2

Objective Finding the root of a function using the Fixed Point Iteration method.

Background A brief review of the fixed-point iteration method is provided here. A point x satisfying the following relation is called a fixed point (or invariant point) of the function g:

The generalized problem 0 can be converted to a fixed point problem by simply adding x to both sides; thus . Fixed point iteration is a method of finding a fixed point of a function. We simply start with an initial guess for x and calculate the following series:

This iteration will converge to a fixed point only for a certain range of initial guesses. But the advantage over the bisection method is that we do not need to know two points that bracket the root beforehand. A limitation of fixed point iteration is that it will only converge if, at the fixed point:

| | 1 If the original problem does not satisfy this constraint, we can usually convert it to a problem that does by a change of variables.

Task An overview of your task is as follows; implementation details are given on the next page:

1. Write a MATLAB FUNCTION that will perform fixed point iteration on a given mathematical function, and plot the successive values of so the user can observe the convergence visually.

2. Write a MATLAB SCRIPT that will use this function to explore the convergence behavior of fixed point iteration using a modified version of the parachute problem.

Page 4: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

AERO2463 Computational Engineering Analysis: Assignment 2

3

Implementation

Implementation of FUNCTION: Your function MUST take the following input arguments, in this order: g The function g(x) to find the fixed points of initguess initial guess of x maxiter Maximum number of iterations before terminating the root finding tolerance Consider the problem is solved (i.e., root is found) when successive guesses or

approximations of x differ by less than this value (relative accuracy). plot_or_not Flag to indicate whether to plot the successive guesses. Your function MUST return the following output arguments, in this order: result The final guess of x Niter The number of iterations taken (this will vary between 1 and maxiter, depending on how fast the solution converges).

If plot_or_not is 1, the function MUST produce a plot of x vs. iteration number. Each

point on the plot MUST be a blue cross. The plot axes MUST be labeled. If plot_or_not is not 1, then NO PLOT should be produced at all.

Your function MUST check to ensure positive values have been provided for maxiter and tolerance, if not then an error MUST be thrown.

If the iteration “blows up” at any point (i.e. x becomes infinity or not-a-number), an error MUST be thrown. Hint: Use in-built functions isnan(x) and isinf(x).

If the iteration does not converge to the root within the maximum number of iterations specified, an error MUST be thrown.

You MUST break out of the iteration loop when the convergence criterion is satisfied. YOU MUST NOT include any user interaction in your function.

Test your function by giving it the anonymous function found in the template file fixed_point_demonstration_script.m, which corresponds to a modified version of the parachute problem. The plot produced by the function, when applied to the modified parachute problem, should appear something like (but not exactly like) the following. As you can see the successive values of x are asymptotically approaching the fixed point.

To assist with marking, you MUST adhere to ALL of the following requirements.

Failure to do so will result in a penalty of 20% of the total possible mark.

Page 5: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

AERO2463 Computational Engineering Analysis: Assignment 2

4

Implementation of SCRIPT:

Your SCRIPT should take the modified parachute function provided in the template, and plot the number of iterations required for convergence versus initial guess, over the range of initial guesses -200:10:200.

You MUST use a tolerance of 1e-6. You MUST use a maximum number of iterations of 500. If the solution does not converge for a particular initial guess x, you MUST plot a red circle

at (x,0). Hint: You may use try-catch to detect when convergence is not reached. If the solution does converge, you MUST plot a blue circle at (x, N_iterations). Hint: You

may use the continue command to step out of the catch statement and continue your

graphing. X and Y axes MUST be labeled. YOU MUST NOT include any user interaction in your script. There MUST NOT be any uncaught potential errors when your script runs.

IMPORTANT NOTE: The function provided in the template is a MODIFIED version of the

parachute problem; a change of variables has been performed to ensure that it satisfies the convergence theorem, and it has already been converted to the form x = g(x) (or, if you prefer, d = g(d)). The change of variables was simply to divide the value of d by 10 (so we are actually solving for the distance in “tens of meters”).

You must not convert the solution back to meters; Just find the fixed point of the function as given.

0 10 20 30 40 50 600

20

40

60

80

100

120

Iteration number

x

Page 6: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

AERO2463 Computational Engineering Analysis: Assignment 2

5

The final plot produced by your script should look something like (but not exactly like) the following.

Assessment Criteria Correct implementation of fixed point iteration function 40% Correct handling of errors in function 20% Correct implementation of script 40% Failure to follow coding standard -20%

−200 −150 −100 −50 0 50 100 150 2000

10

20

30

40

50

60

70

80

Initial guess

Num

ber

of it

erat

ions

req

uire

d

Page 7: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

School of Engineering

Computational Engineering Analysis -

AERO2463

Assignment 2.2

Matlab Functions: Surface Derivatives and Plotting

Assessment: Assignment 2.2 is worth 5%.

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

Page 8: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

AERO2463 Computational Engineering Analysis: Assignment 2

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

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:

[minmax1Z,minmaxDX,minmaxDY] =

LastName_StudentID_surface_derivative(xlims,ylims,nx,ny,myfun)

Page 9: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

AERO2463 Computational Engineering Analysis: Assignment 2

Function Input Arguments:

MATLAB Variable

Type Explanation

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 Variable

Type Explanation

minmax1Z

2×2 matrix

First row must contain the x and y position of the function’s minimum.

Second row must contain the x and y position of the function’s maximum.

minmaxDX

2×2 matrix

First row must contain the x and y position of the minimum of the function’s partial derivative with respect to x.

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

minmaxDY

2×2 matrix

First row must contain the x and y position of the minimum of the function’s partial derivative with respect to y.

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 10: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

AERO2463 Computational Engineering Analysis: Assignment 2

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 2 3 4 5 6 7

xlims = [-3; 3]; ylims = [-3; 3]; nx = 25; ny = 25; myfun = @(x,y) x.^3 .* y; [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 11: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

AERO2463 Computational Engineering Analysis: Assignment 2

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

Maximum

Minimum

Page 12: Assignment 2€¦ · Assignment 2.1 Application of Functions: Root Finding using Fixed Point Iteration Assessment: The MATLAB Assignment 2.1 is worth 5%. Submission: Use the Matlab

AERO2463 Computational Engineering Analysis: Assignment 2

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%