# 计算物理学代写|INTRODUCTION TO COMPUTATIONAL PHYSICS PHYS105 University of Liverpool Assignment

0

Assignment-daixieTM为您提供利物浦大学University of Liverpool INTRODUCTION TO COMPUTATIONAL PHYSICS PHYS105计算物理学代写代考辅导服务！

## Instructions:

Computational physics is a subfield of physics that involves the use of computers to solve problems in physics that are too complex to be solved analytically. The use of computational techniques has become increasingly important in physics research and has led to many breakthroughs in our understanding of the physical world.

To get started with computational physics, it’s important to have a strong foundation in mathematics and programming. In terms of programming languages, Python is a popular choice for computational physics because of its ease of use and large user community.

There are many resources available online to help you learn computational physics. Some good places to start include:

• The Open Source Physics Project: This project provides a collection of open-source software tools and simulations for teaching and researching physics. They have a range of resources and materials available for learning computational physics, including tutorials and sample code.
• Coursera: Coursera offers a range of online courses in computational physics, including courses on Python programming and numerical methods for physics.
• GitHub: GitHub is a platform for collaborative software development, and there are many open-source repositories available on GitHub that contain code for solving physics problems. You can use these repositories to learn from existing code and contribute to the development of new code.
• Textbooks: There are many textbooks available on computational physics, including “Computational Physics” by Mark Newman and “An Introduction to Computational Physics” by Tao Pang.

I hope this information is helpful! Let me know if you have any other questions.

(a) Write a procedure that solves quadratic equations using the quadratic formula: It should take as arguments three numbers a, b, and c. It should print error messages if a is zero, or if the roots are complex. Otherwise it should print the two roots.

# a.) code for roots function
def roots(a, b, c):
#form of equation is a*x**2 + b*x + c = 0
#quadratic formula is   x = (-b + sqrt(b**2 - 4 * a * c))/(2 * a)
#                   or  x = (-b - sqrt(b**2 - 4 * a * c))/(2 * a)
#roots are complex when the discriminant (b**2 - 4*a*c) is negative
discriminant = b**2 - (4 * a * c)
if discriminant < 0:
return "Roots are complex"
return "x = "+str((-b + math.sqrt(discriminant)) / (2 * a))+" or x = "+\
str((-b - math.sqrt(discriminant)) / (2 * a))

# Test Cases (assertions are just to automate)
##print '\nTesting roots'
##
##print roots(1, 2, 1) #(x+1)^2: double root at x = -1
##assert roots(1, 2, 1) == 'x = -1.0 or x = -1.0'
##
##print roots(1, -2, -3) #(x+1)(x-3): roots at x = 3 or x = -1
##assert roots(1, -2, -3) == 'x = 3.0 or x = -1.0'
##
##print roots(2, 2, 2) #2x^2 + 2x + 2: complex roots
##assert roots(2, 2, 2) == 'Roots are complex'

(b) Modify your procedure to handle the case of complex roots.

# b.) same code, modified to handle complex roots
def roots(a, b, c):
discriminant = b**2 - (4 * a * c)
if discriminant < 0:
discriminant = discriminant + 0j
return "x = "+str((-b + discriminant**0.5) / (2 * a))+ " or x = " +\
str((-b - discriminant**0.5) / (2 * a)) # math.sqrt does not work

# Test Cases (assertions are just to automate)
##print '\nTesting roots'
##
##print roots(1, 2, 1) #(x+1)^2: double root at x = -1
##assert roots(1, 2, 1) == 'x = -1.0 or x = -1.0'
##
##print roots(1, -2, -3) #(x+1)(x-3): roots at x = 3 or x = -1
##assert roots(1, -2, -3) == 'x = 3.0 or x = -1.0'
##
##print roots(2, 2, 2) #2x^2 + 2x + 2: complex roots
##assert roots(2, 2, 2) == 'x = (-0.5+0.866025403784j) or x = (-0.5-0.866025403784j)'

###########################################
## 2.) procedure for evaluating polynomials
###########################################
def eval_poly(x, coeffs):
total=0 #will keep a running total of the sum
coeffs.reverse() # to put the low order coeffs first
for i in range(len(coeffs)):
total+=coeffs[i]*(x**i) #add the curent term to the total

# Test cases
##print '/nTesting Polynomial'
##
##print eval_poly(1,[1,2,3])
##assert eval_poly(1,[1,2,3]) == 6
##
##print eval_poly(2,[1,2,3,4])
##assert eval_poly(2,[1,2,3,4]) == 26


Write a procedure that evaluates polynomials. It should take two arguments. The first is a number $x$. The second is a list of of coefficients ordered from highest to lowest:
$$a_n, a_{n-1}, \ldots, a_2, a_1, a_0$$
Your procedure should return the value of the polynomial evaluated at $x$ :
$$a_n x^n+a_{n-1} x^{n-1}+\ldots+a_2 x+a_1 x+a_0$$

) procedure for evaluating polynomials
###########################################
def eval_poly(x, coeffs):
total=0 #will keep a running total of the sum
coeffs.reverse() # to put the low order coeffs first
for i in range(len(coeffs)):
total+=coeffs[i]*(x**i) #add the curent term to the total

# Test cases
##print '/nTesting Polynomial'
##
##print eval_poly(1,[1,2,3])
##assert eval_poly(1,[1,2,3]) == 6
##
##print eval_poly(2,[1,2,3,4])
##assert eval_poly(2,[1,2,3,4]) == 26

###########################################
## 3.) procedure to make change
###########################################
def make_change(cost, paid):
change=paid-cost #calculate the change due
bills={20:0,10:0,5:0,2:0,1:0} #dictionary that maps each bill to the amount of it
bill_list=bills.keys() #bill_list is a list of the available bills
bill_list.sort() #sort it in ascending order
bill_list.reverse() #now reverse it to be in descending order
#this is to make sure you get the smallest number of bills
for bill in bill_list:
while change >=bill:
bills[bill]+=1 #increment the amount of that bill
change-=bill #decrease the change by that bill

print "Change is:"
for bill in bill_list:
if bills[bill] == 1:
print bills[bill], bill, "dollar bill"
elif bills[bill] > 1:
print bills[bill], bill, "dollar bills"

#return bills #return the dictionary with amounts of each bill needed

make_change(1, 6)
make_change(4, 109)