HW4.ipynb - Colaboratory

pdf

School

University of California, Davis *

*We aren’t endorsed by this school

Course

115

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

7

Uploaded by teiser957

Report
Pratiyush Karki Eci 115 Homework # 4 Problem # 12.8: import numpy as np # Define the coefficient matrix A and the right-hand side vector b A = np.array([[13.442, 0, 0, 0], [-13.442, 12.252, 0, 0], [0, -12.252, 12.377, 0], [0, 0, -12.377, 11.797]]) b = np.array([750.5, 300, 102, 30]) # Solve for the concentrations using the matrix inverse c = np.linalg.solve(A, b) # Print the results print("Concentration in Lake Powell :", c[0]) print("Concentration in Lake Mead :", c[1]) print("Concentration in Lake Mohave :", c[2]) print("Concentration in Lake Havasu :", c[3]) Concentration in Lake Powell : 55.832465406933494 Concentration in Lake Mead : 85.74110349330721 Concentration in Lake Mohave : 93.11626403813523 Concentration in Lake Havasu : 100.2373484784267 # Given values w14_inverse = 0.084767 # Matrix element related to the concentration of the loading of Powell Lake target_concentration_havasu = 75 # Target chloride concentration in Lake Havasu # Calculate the reduction needed in Lake Powell's loading reduction_needed = (75 - w14_inverse * (750.5 + 300 + 102 + 30)) / -(w14_inverse) print(reduction_needed) 297.7217254356058 # Calculate the condition number using the column-sum norm condition_number = np.linalg.cond(A, 1) # Determine the number of suspect digits num_suspect_digits = np.log10(condition_number) print("Number of suspect digits:", num_suspect_digits) Number of suspect digits: 0.9367766453143918 Problem # 12.21: import numpy as np E24=25 # Define the coefficient matrix A and the right-hand side vector b A = np.array([[225, 0, -25, 0], [1, -1 - E24, 0, E24], [40, 0, -30, 0], [0, E42, 0, -1 - E42]]) b = np.array([3400, 0, 1200, 0]) # Solve for the concentrations using the matrix inverse c = np.linalg.solve(A, b) print("Steady-state concentrations:") print(f"Room 1 (Smoking Section): {c[0]} mg/m^3") print(f"Room 2 (Kids' Section): {c[1]} mg/m^3") print(f"Room 3 (Grill Section): {c[2]} mg/m^3") print(f"Room 4 (Intake Vents): {c[3]} mg/m^3") # (b) Calculate the percentages # Define the loads load smokers = 3400 E42=25
Steady-state concentrations: Room 1 (Smoking Section): 12.521739130434781 mg/m^3 Room 2 (Kids' Section): 6.383631713554991 mg/m^3 Room 3 (Grill Section): -23.304347826086953 mg/m^3 Room 4 (Intake Vents): 6.1381074168798 mg/m^3 Percentages in the Kids' Section: (i) Percentage due to smokers: 471.83% (ii) Percentage due to the grill: 166.53% (iii) Percentage due to intake vents: 0.00% Increase in concentration in the Kids' Section: 0.0 <>:60: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a com <>:62: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a com <>:60: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a com <>:62: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a com <ipython-input-10-5d48fa16bda9>:60: SyntaxWarning: list indices must be integers or slices, not tu A_new = np.array([[225, 0, -25, 0] <ipython-input-10-5d48fa16bda9>:62: SyntaxWarning: list indices must be integers or slices, not tu [40, 0, -30, 0] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-5d48fa16bda9> in <cell line: 60> () 58 59 # Update the matrix A with the new E24 ---> 60 A_new = np.array([[225, 0, -25, 0] 61 [ 150 , - 1 , 0 , 5 ], 62 [ 40 , 0 , - 30 , 0 ] TypeError : list indices must be integers or slices, not tuple SEARCH STACK OVERFLOW load_grill = 1200 load_intake_vents = 0 # No load in intake vents # Calculate the percentages total_load_kids_section = load_smokers + load_grill + load_intake_vents percentage_smokers = (load_smokers * c[1] / total_load_kids_section) * 100 percentage_grill = (load_grill * c[1] / total_load_kids_section) * 100 percentage_intake_vents = (load_intake_vents * c[1] / total_load_kids_section) * 100 print("Percentages in the Kids' Section:") print(f"(i) Percentage due to smokers: {percentage_smokers:.2f}%") print(f"(ii) Percentage due to the grill: {percentage_grill:.2f}%") print(f"(iii) Percentage due to intake vents: {percentage_intake_vents:.2f}%") # (c) If smoker and grill loads are increased # Update the loads load_smokers_new = 4000 load_grill_new = 6000 # Calculate the new concentrations b_new = np.array([3400, 0, 1200, 0]) c_new = np.linalg.solve(A, b_new) # Calculate the increase in concentration in the Kids' Section increase_in_concentration = c_new[1] - c[1] print("Increase in concentration in the Kids' Section:", increase_in_concentration) # (d) If mixing between areas 2 and 4 is decreased # Update the mixing parameters E24_new = ... # New value for E24 (5 m/hr) # Update the matrix A with the new E24 A_new = np.array([[225, 0, -25, 0] [150, -1, 0, 5], [40, 0, -30, 0] [0, E42, 0, - E42]]) # Solve for the new concentrations b_new_mixing = np.array([3400, 0, 1200, 0]) c_new_mixing = np.linalg.solve(A_new, b_new_mixing) # Calculate the change in concentration in the Kids' Section change_in_concentration = c_new_mixing[1] - c[1] print("Change in concentration in the Kids' Section due to decreased mixing:", change_in_concentration) Problem # 5.16:
import numpy as np import matplotlib.pyplot as plt # set constants g = 9.81 Q = 20 def B(y): return 3 + y def A(y): return 3*y + (y**2)/2 # set function def fn(y): return 1 - (Q**2)/(g*A(y)**3)*B(y) # a) graphically y = np.linspace(0.5, 3, 100) plt.plot(y, fn(y)) plt.plot(1.5, fn(1.5), 'ro') plt.grid(True) plt.xlabel('y') plt.ylabel('f(y)') plt.show() # b) bisection method print('\nBISECTION METHOD...\n') # initialization xl = 0.5 xu = 3 i = 0 error_a = 10 # plot roots initialization plt.plot(y, fn(y)) plt.xlabel('y') plt.ylabel('f(y)') plt.title('Roots in each iteration') plt.grid(True) while error_a >= 1 and i < 10: i += 1 # calculate and print root and function xr = (xl+xu)/2 print('\nFor the', i, 'iteration...') print('xl =', xl, ', f(xl) =', fn(xl)) print('xu =', xu, ', f(xu) =', fn(xu)) print('The root is', xr) print('The function value is', fn(xr)) # calculate and print error if i == 1: pass else: error_a = abs((xr-xr_prev)/xr)*100 print('Estimated error is', error_a, '%') # plot current root plt.plot(xr, fn(xr), 'o', label='Iteration {}'.format(i)) # change interval borders with the new root if fn(xl)*fn(xr) > 0: xl = xr else: xu = xr xr_prev = xr plt.legend() plt.show() # c) false position method print('\nFALSE POSITION METHOD...\n') # initialization xl = 0.5 xu = 3 i = 0 error_a = 10 # plot roots initialization plt.plot(y, fn(y)) plt.xlabel('y') plt.ylabel('f(y)') plt.title('Roots in each iteration') plt.grid(True) while error_a >= 1 and i < 10: i += 1 # calculate and print root and function xr = xu - (fn(xu)*(xl-xu))/(fn(xl)-fn(xu)) print('\nFor the', i, 'iteration...')
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
print('xl =', xl, ', f(xl) =', fn(xl)) print('xu =', xu, ', f(xu) =', fn(xu)) print('The root is', xr) print('The function value is', fn(xr)) # calculate and print error if i == 1: pass else: error_a = abs((xr-xr_prev)/xr)*100 print('Estimated error is', error_a, '%') # plot current root plt.plot(xr, fn(xr), 'o', label='Iteration {}'.format(i)) # change interval borders with the new root if fn(xl)*fn(xr) > 0: xl = xr else: xu = xr xr_prev = xr plt.legend() plt.show() # initialization xl = 0.5 xu = 3 error_a = 10 i = 0 # plot roots initialization plt.figure() x = np.linspace(xl, xu, 100) plt.plot(x, fn(x)) plt.xlabel('y') plt.ylabel('f(y)') plt.title('Roots in each iteration') plt.grid(True) plt.legend(['Function']) # false-position method while error_a >= 1 and i < 10: i = i + 1 # calculate and print root and function xr = xu - (fn(xu)*(xl-xu))/(fn(xl)-fn(xu)) print(f'\nFor the {i} iteration...') print(f'xl = {xl:.2f}, f(xl) = {fn(xl):.2f}') print(f'xu = {xu:.2f}, f(xu) = {fn(xu):.2f}') print(f'The root is {xr:.4f}') print(f'The function value is {fn(xr):.4f}') # calculate and print error if i == 1: pass else: error_a = abs((xr-xr_prev)/xr)*100 print(f'Estimated error is {error_a:.2f}%') # plot current root plt.plot(xr, fn(xr), 'o', label=f'Iteration {i}') # change interval borders with the new root if np.sign(fn(xr)) == np.sign(fn(xl)): xl = xr else: xu = xr xr_prev = xr plt.show()
BISECTION METHOD... For the 1 iteration... xl = 0.5 , f(xl) = -32.258214681590175 xu = 3 , f(xu) = 0.9005646220422442 The root is 1.75 The function value is 0.37890887000741 For the 2 iteration... xl = 0.5 , f(xl) = -32.258214681590175 xu = 1.75 , f(xu) = 0.37890887000741 The root is 1.125 The function value is -1.6127192822654295 Estimated error is 55.55555555555556 % For the 3 iteration... xl = 1.125 , f(xl) = -1.6127192822654295 xu = 1.75 , f(xu) = 0.37890887000741 The root is 1.4375 The function value is -0.18444484114723303 Estimated error is 21.73913043478261 % For the 4 iteration... xl = 1.4375 , f(xl) = -0.18444484114723303 xu = 1.75 , f(xu) = 0.37890887000741 The root is 1.59375 The function value is 0.15468446929521396 Estimated error is 9.803921568627452 % For the 5 iteration... xl = 1.4375 , f(xl) = -0.18444484114723303 xu = 1.59375 , f(xu) = 0.15468446929521396 The root is 1.515625 The function value is 0.0033830901170610606 Estimated error is 5.154639175257731 % For the 6 iteration... xl = 1.4375 , f(xl) = -0.18444484114723303 xu = 1.515625 , f(xu) = 0.0033830901170610606 The root is 1.4765625 The function value is -0.0853360168378583 Estimated error is 2.6455026455026456 % For the 7 iteration... xl = 1.4765625 , f(xl) = -0.0853360168378583 xu = 1.515625 , f(xu) = 0.0033830901170610606 The root is 1.49609375 The function value is -0.03976423544396046 Estimated error is 1.3054830287206265 % For the 8 iteration... xl = 1.49609375 , f(xl) = -0.03976423544396046 xu = 1.515625 , f(xu) = 0.0033830901170610606 The root is 1.505859375 The function value is -0.017897582206541696 Estimated error is 0.648508430609598 %
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help