ChE 263 Assignment #6 This homework assignment can be challenging for some students. Please plan on at least 4 hours to complete the assignment. Please start early so you can get the help you need. For Excel VBA, the Reading Material is Chapters 9 and 12 of the Chapra text. This is a reference and you do not need to read it if you feel you understand loops and functions well. To continue practicing defining variables, include “Option Explicit” (for VBA) at the beginning of every module for the following problems. 1. (Competency 5.4 - 25 points) Record what will be displayed when the following code is run in a worksheet entitled “Problem 1” in your answer worksheet. Do the problems by hand. You can check them by coding a subroutine or script if you desire. Excel VBA: For i = 7 To 4 Step -2 For j = -2 To 6 Step 5 MsgBox (“i = “ & i & “, j = “ & j) Next j MsgBox (“i = “ & i & “, j = “ & j) Next i MsgBox (“i = “ & i & “, j = “ & j) MATLAB: for i = 7:-2:4 for j = -2:5:6 disp(['i = ' num2str(i) ', j = ' num2str(j)]) end disp(['i = ' num2str(i) ', j = ' num2str(j)]) end disp(['i = ' num2str(i) ', j = ' num2str(j)]) Python: for i in range(7,4,-2): for j in range(-2,6,5): print 'i=' + str(i) + ' j=' + str(j) print 'i=' + str(i) + ' j=' + str(j) print 'i=' + str(i) + ' j=' + str(j) 2. (Competency 5.4 - 70 points) Gottfried Wilhelm Leibniz (b. 1646) was a great German diplomat, philosopher, and mathematician. As a philosopher he is best remembered for his conclusion that our universe is the best possible one God could have made. In math he invented calculus independently of Newton, and it is his notation that we use today. He also proved that: 1n 2n 1 4 n 0 Write a program, based upon Leibniz’ formula, that calculates π. The program should consist of the following: Use an InputBox (VBA), input (MATLAB), or raw_data (Python) to get the number of decimal digits of accuracy required. (i.e. an inputted value of 4 should result in accurate digits up to 3.1416 where digits following 6 do not need to be accurate) Initialize the sum and the counter (n) to zero. Use Do…Loop (VBA) or while (MATLAB/Python) statements to calculate each term and add it to the sum. Loop until the desired accuracy is obtained. Display the calculated approximation to π in a MsgBox (VBA), disp (MATLAB), or print (Python). Hint: o In VBA the exact number of can be calculated as: pi = 4 * Atn(1) o In MATLAB the exact number of can be displayed as pi() o Hint: Do not request more than 4 decimal digits of accuracy as the code may take too long to run. Try out your code up to 4 decimal digits to see if it works. If your code is stuck you can hit “Esc” to stop (VBA) or Ctrl-c (MATLAB / Python). After you have solved the problem, answer the following (for points). Briefly list which skills, learned in class, were used to solve the problem. Give an example of a problem you might run into some day where a do loop or for loop would come in handy. 3. (Competency 5.4 - 70 points) The infinite series for the cosine of x is 𝑛 𝑥 2𝑖 cos(𝑥) = ∑(−1)𝑖 (2𝑖)! 𝑖=0 Or, in the expanded form, 𝑥0 𝑥2 𝑥4 𝑥6 − + − +⋯ 0! 2! 4! 6! Write a function entitled Cosine, that uses this formula to calculate the value of cosine of x, given x and the number of terms, n. The function will call subroutine (Excel) or function (MATLAB/Python) CosEvaluate (something that you will also write) that will complete the calculation. This function accepts x and the number of terms desired (n) and returns the estimate of cos(x). Use a For/Next loop (VBA) or for loop (MATLAB/Python) to calculate the series. In a worksheet or program entitled “Problem3”, test the convergence of the series as a function of n and determine how many terms are necessary for an accuracy of 10-6 when x is π/2, π/6, π/3, and π. cos(𝑥) = Hint (VBA): You can debug a function by putting a break point (F9) in the code and then typing in the function in excel and hitting enter. You can then use F8 to continue stepping through the program to help you debug. Hint (MATLAB): You can debug a function by putting a break point (F12) in the code and running the program by clicking the run button or with F5. You can then use F10 or F11 to continue stepping through the program to debug. Hint (Python): You can debug a function by opening the debugger from the Python Shell (menu Debug…Debugger). The next time you run the program it will enter into debug mode. Save your completed assignment as “lastname-firstname-HW#-Sect#.xlsm” (e.g. Smith-Thomas-HW6.xlsm). Remember to save as macro-enabled workbook! Email finished assignment to the TA. The assignment is due before the beginning of the assigned class period.
© Copyright 2024