## Python Newton’s Method Code

This assignment presents you with the code structure and ideas for solving an optimization problem related to computing the amount of drag on an airplane wing, as a function of a proxy variable for altitude, velocity and weight.

Specified on the Moodle assignment page. Late submissions will automatically be marked as late and NOT GRADED. Please make sure you submit your assignment far in advance of the stated deadline, so that you avoid losing credit due to bad networks, etc. It is recommended that you start early and if you have questions, ask them in the course Moodle Discussion forum. If you wait until the last minute to ask questions, it’s very likely that nobody will answer you in time. It is also recommended that you turn in something, even if it’s not complete. Even if you weren’t able to complete the assignment, taking the time to explain what you tried and what problems you faced will likely prevent you from getting a zero. Grading: Grading is somewhat subjective in this assignment, on a scale of 0 to 10, roughly equivalent to a standard grading scale where 10 would be an A+ (Excellent), 9 would be a B+/A- (Good), 8 would be a C+/B- (Fair), etc. In this particular assignment, if you do all that is asked of you, you should receive a 10. If you neglect, or poorly explain 2 or more minor items, you should receive a 9, and so on. This assignment consists of several goals

● To provide you with experience in using Python to solve a 1D optimization problem ● To provide you with additional experience in the use of Taylor Series derived finite

difference approximations of first and second derivatives (so that you can solve the optimization problem with Newton’s Method)

● To provide you with additional opportunities to assess your solutions rather than just “doing them” and hoping they’re right

● To force you into a programming structure whereby you can easily change the problem simply by changing constants. In the last program many of you wrote your equations in terms of hard-coded numbers that were prone to error, and were difficult to modify if problem parameters changed. I want to start getting you out of this way of setting up problems. The more you can leave problems in terms of variables rather than hard-coded numbers, the more flexible your code will be.

The deliverable that you should plan on submitting is a PDF-format formal report on the activities described below and, optionally, an IPython Notebook file. Increasingly, some students have been copy/pasting huge amounts of poorly-formatted code and data and dumping them into reports which are sometimes poorly organized and hard to read. You should make every effort to make this a professional-looking report that clearly demonstrates that you know what you’re doing. I am not a mind-reader, and my philosophy is that the more effort you place into showing me, professionally, that you

have done the assignment (or, clearly explained problems that prevented you from doing so), the easier it is for me to give you a good grade. If you make me struggle and “guess” about whether you really got it, then your chances of a good grade diminish. As I’ve told some students, your goal in doing these assignments should be to “help me to help you” with a good grade and constructive feedback.

Problem 6.1: Computing the velocity of wind over an airplane wing that minimizes its drag, given a specified value of air density ratio and weight of the aircraft supported by the wing This problem uses the scenario outlined in Problem 16.28 of your textbook. In the problem, you are provided with an equation that approximates the drag force on an airplane wing as a function of the air density, weight supported by the wing, and velocity of airflow across the wing. The “drag” on an airplane wing, as shown in the following equation and graphic, is a function of the friction of air across the wing, and the lift provided by the wing. The three variables are

● σ – a vertical coordinate used in many atmospheric equations, representing the ratio of air density at a specified elevation compared to air density at the surface. So, sigmaσ has a value of 1.0 at the surface, and approaches 0.0 at the top of the atmosphere. You can think of this as a measure of altitude, but also as a measure of the density of air molecules

● W – weight of the aircraft ● V – velocity of air across the wing

As the graph shows, the drag associated with friction decreases with air velocity, and the drag associated with lift increases with air velocity. There is a point where the total drag is a minimum, and that’s what you will compute in this problem, for a specified altitude (σ value) and weight.

You should use Newton’s Method to find this minimum, which requires that you have the first and second derivatives of the drag function, D(σ, V, W). In this case, even though you might be able to find them analytically, I want you to use finite difference approximations of these derivatives. The reasons for this is that I want you to have a chance to review their use, and be impressed with how they may be used in codes in place of computing derivatives analytically. I have provided you with a code structure, and much, but not all, of the code you will need to do this problem. The code is available at http://borealscicomp.com/Academic/HU/CISC600-LP18/Assign06/DragTemplate.py It consists of five functions

● main() – Set up variables, call an optional plotting routine and a function that computes the value of V that results in minimum drag for constant values of σ and W. This function is complete, and all you need to do is uncomment the call to the plotting routine when you want to generate the plots

● D(s, V, W) – computes the drag as a function of σ, V and W – you will need to implement and test this

● dDdV(s, V, W) – computes the first derivative of the drag function as a function of σ, V and W – you will need to implement this with a central finite difference formulation, and test it

● d2DdV2(s, V, W) – computes the second derivative of the drag function as a function of σ, V and W – you will need to implement this with a central finite difference formulation (be sure to use a second derivative formula in terms of the drag function, not a first derivative formula in terms of the first derivative of the drag), and test it

● plot_drag_vs_v(s, V, W) – given scalar σ and W, and a NumPy array of V values (for your horizontal axis), along with properly defined functions for drag and its first and second derivatives, plots each of the curves. This has been fully implemented for you.

● newton_min(v0, s, W) – Given an initial guess, v0, of the velocity that minimizes drag, along with σ and W, uses Newton’s Method to estimate the value of v that will minimize the drag function. It also should print a neat table that shows the iterations. I have given

http://borealscicomp.com/Academic/HU/CISC600-LP18/Assign06/DragTemplate.py

you some guidance in this function, but you will have to implement the actual computations.

Here’s what you should do with this code

● Implement, plot and test the function D(). Verify that it looks reasonable ● Implement, plot and test the function dDdV(). Verify that it looks reasonable and

explain why you think it’s correct ● Implement, plot and test the function d2DdV2(). Verify that it looks reasonable and

explain why you think it’s correct ● Implement and test the function newton_min(). Again, verify that results are

reasonable and explain why you think they’re correct. ● Repeat the exercise using a different value of σ and W. You should be able to do

this very easily, because I, your favourite scientific computing professor, have written code that makes it easy! Please consider this strategy in the next problem.

Problem 6.2: Computing the air density ratio that minimizes the drag of an airplane wing, given the velocity of air across the wing and the weight of the aircraft. This is identical to Problem 6.1, except now you are going to write a new program that analyzes the minimum drag as a function of varying σ and constant V and W. The purpose of this problem is to force you to understand what you actually did in Problem 6.1 and map it to a similar, but different-enough problem. You may, of course, use the same code structure I’ve given you for the previous problem, but it should be completely YOUR OWN. I want to see comments and variable names that clearly apply to THIS problem, and weren’t just copied over from the last one. If I see evidence that you just copied the last program and neglected to rewrite comments and variable names where applicable, you will likely lose points. To complete this problem, you should

● Provide your complete source code (you may do this in IPython Notebook if you want)

● Go through the same derivation and testing of functions you did in Problem 6.1 and, again, explain why you think your results are correct. You are writing a code to analyze air flow over a wing, and you want to be able to assure your client that your results are correct before they build the wing!

● After you’ve assured that it all works for a single problem (a specified V and W), repeat the computations for another combination of V and W. If you followed my strategy from the previous problem, you should find this very easy to do.