hw5 - HOMEWORK – 5 SUBROUTINES 1 Three circular...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: HOMEWORK – 5 SUBROUTINES 1. Three circular subdivisions need to be cleared for construction. The XYZ tree cutting company charges $100 per square feet to clear trees and to stabilize the area, and would charge $5 per linear ft to install fence in that area. Approximate radii of the sites are estimated to be 50, 75, and 100 ft. This problem will be solved as a part of the lab work. 5 2 2. Develop a visual basic program with subroutine to evaluate the integral I x ln( x)dx using the 1 trapezoidal rule. Use n = 10. This is your lab problem 2. Refer to lab for further details. The formula for computing an integral using trapezoidal rule is given by (check the class notes for derivation): n 1 f ( x0 ) 2 f ( xi ) f ( xn ) ba i 1 Use the following pseudo code to write the program: I 2 n . Get the inputs a, b and n Calculate the interval, h = (b‐a)/n Write a subroutine which takes the values of x as input and outputs fx Sub myfunction(x as Single, fx as Single) fx=x^2*log(x) End Sub Call the subroutine “myfunction” to evaluate the value of the function at x0 (a) and xn (b) Compute the value of f(x) at every xi (=a+h*i) and compute their sum (use loop) Compute I = h*(f(a)+2*sum+f(b))/2 Output result. 3. Develop a visual basic program for the above problem using a function to evaluate the integral 5 I x 2 ln( x)dx using trapezoidal rule. This is your lab problem 4. Also, see ppt slides for details. 1 4. Modify the above code with subroutine to solve the following problem: 1 x 0.1 (1.2 x)(1 e20( x 1) )dx . Try your program for n = 10, 20, 30. Compare your answer with the 0 analytical solution (true value = 0.602297) and calculate percentage error for each n. 1 5. The permutation, P(n,r) and combination, C(n,r) are given by the following formulae: P (n, r ) n! n! , C (n, r ) (n r )! r !(n r )! Write a Visual Basic program that evaluates the permutation and combination for a given n and r. The program should contain a subroutine that evaluates the factorial. Check your program for n = 10 and r = 4. 5 2 6. Develop a visual basic program with subroutine to evaluate the integral I x ln( x)dx using 1 Simpson’s rule. Check your program for n = 10, 16, 20 and calculate the percentage error. The formula for computing an integral using Simpson’s rule is given by (check the class notes for derivation): f ( x0 ) 4 f ( x1 ) f ( x2 ) f ( x2 ) 4 f ( x3 ) f ( x4 ) I 2h 2h ... 6 6 f ( xn 2 ) 4 f ( xn 1 ) f ( xn ) 2h 6 Use the following pseudo code to write the program: a) Get the inputs a, b and n b) Calculate the interval, h = (b‐a)/n c) Start a FOR loop from i =1 to n‐1 with step 2 d) Compute xi‐1 = a + h * (i ‐ 1), xi = a + h * i, xi+1 = a + h * (i + 1) e) Compute the value of function f(x) at xi‐1 ,xi ,xi+1. f) Compute the term= h * (f(xi‐1) + 4 * f(xi) + f(xi+1)) / 3 g) Compute the sum of the terms. h) Output the result. 7. Develop a visual basic program for Richardson integration using subroutines for the integral 5 I x 2 ln( x)dx . Check your program for n = 10, and calculate the error percentage. Your program 1 should contain one subroutine to evaluate the function and one subroutine to evaluate the area using the trapezoidal rule. Use the following logic in the main program: n1 = n n2 = 2 * n Call traprule(a, b, n1, area1) Call traprule(a, b, n2, area2) integration = (area2 * 4 - area1) / 3 Your traprule subroutine should have the logic to calculate the trapezoidal rule: 2 h = (bb - aa) / nn Call myfunction(aa, fa) Call myfunction(bb, fb) For i = 1 To nn - 1 x = aa + h * i Call myfunction(x, fx) fxSum = fxSum + fx Next i area = h * (fa + 2 * fxSum + fb) / 2 8. The electric company charges its customers according to the following rate schedule: 8 cents a kilowatt‐hour(kwh) for electricity used up to the first 300 kwh; 6 cents a kwh for the next 300 kwh (up to 600 kwh) 5 cents a kwh for the next 400 kwh (up to 1000 kwh) 3 cents a kwh for all electricity used over 1000 kwh Write a subroutine to calculate the total charge for each customer. Develop a visual basic program that calls this subroutine for the following data: Customer number 123 205 464 596 601 9. The head loss in a pipe is given by: H loss KWH used 47 1015 600 327 915 fLV 2 2 gD Where L is the length of the pipe, V is the velocity in the pipe, g is the gravitational constant, D is the diameter and f is the friction factor. The friction factor for laminar flow (Re <=2300) is given by: f 64 Re The friction factor for turbulent flow (Re>4000) is given by the following explicit approximation (Haaland equation): 6.9 / D 1.11 1 1.8log10 f Re 3.7 Where Re is the Reynolds number, ε is the roughness and D is the diameter of the pipe. a) Develop a visual basic program to calculate the head loss using subroutines. The subroutine should calculate the friction factor. (Note: log10 x 0.43429*ln x ). b) Run your program for Re = 500,000 and ε/D=0.004, D = 1.2ft, g = 32ft/s2, V=4.17ft/s; L = 100ft. c) Run your program for Re = 1200, D = 0.5ft, V = 0.024 ft/s, L = 50ft, g=32ft/s2 3 ...
View Full Document

This note was uploaded on 11/14/2011 for the course CE 3010 taught by Professor Clement during the Fall '09 term at Auburn University.

Ask a homework question - tutors are online