Question

In: Computer Science

Using python programming language, compare the four tasks. 1. For the following exercises you will be...

Using python programming language, compare the four tasks.

1. For the following exercises you will be writing a program that implements Euler’s method (pronounced ‘oy-ler’). It may be the case that you have not yet encountered Euler’s method in the course. Euler’s method can be found in Chapter 10.2 of your notes, which gives a good description of why this algorithm works. But for our purposes, we only need to know the algorithm itself - so there’s no need to worry if you are yet to understand it.

Euler’s method is used to approximate a function when only the derivative and a particular value is known. Given an initial value y and a corresponding x value, a derivative f’(x), and a step size h, the next value for y is calculated using the following formula:

next y = current y + f'(x)*h

If you want to calculate more steps, just reuse the formula but with your new y value. The new x value is also updated to x+h.

Task: To start, write a function called fdash(x), that returns the value of the derivative at x. The formula for the derivative which we will be using as a placeholder is:

fdash = 0.05x * e^(0.05x)

Round the output to 5 decimal places. Do not print anything, call your function, or ask for any input.

2. Recall the formula for Euler’s method is:

next y = current y + f'(x)*h

Task: Write a function called onestep(x, y, h) that calculates the next y value using Euler’s method with the given x, y and h values. The derivative is the same as used in the previous exercise. Copy paste your function from the previous exercise and use it in your onestep function.

Round the output to 5 decimal places. Do not print anything, call your onestep function, or ask for any input. You should have two functions defined in your code (onestep and fdash).

3. Recall that after one step of Euler’s function is calculated, the new x value is also updated to x+h. Note that it happens in that orde.r First the next y value is found, then the corresponding x value is updated.

Task: Write a function called eulers(x, y, h, n) that uses euler’s method n times with the given x, y and h values. It will return the final y value. Remember to copy paste your onestep and fdash functions. You are encouraged to use them in your eulers function, but it is up to you.

The fdash and onestep functions already round their output, so you will not need to do any rounding inside the eulers function.

Hint: One way of implementing this function is to use a while loop.

If you need help with Euler’s method:

The following example of Euler’s method is given in case you do not understand how it works with multiple steps:

F dash: 2x + 2

Initial x: 1

Initial y: 4

Step size (h): 0.5

Number of steps (n): 3

Step 1:

Fdash = 2*1 + 2 = 2 + 2 = 4

Value of y at step 1: new y = 4 + 4*0.5 = 4 + 2 = 6

New x value = 1 + 0.5 = 1.5

Step 2:

Fdash = 2*1.5 + 2 = 3 + 2 = 5

Value of y at step 2: new y = 6 + 5*0.5 = 6 + 2.5 = 8.5

New x value = 1.5 + 0.5 = 2

Step 3:

Fdash = 2*2 + 2 = 4 + 2 = 6

Value of y at step 3: new y = 8.5 + 6*0.5 = 8.5 + 3 = 11.5

New x value = 2 + 0.5 = 2.5

So our final value for y is 11.5 after 3 steps (at x = 2.5). For interest, one function with this derivative is y = x**2+2*x+1. At x = 1, y is equal to 4, like in this example. At x = 2.5 (which is our final x value), the value of y is 12.25. This is pretty close to our approximated value of 11.5.

4. An asteroid has been spotted travelling through our solar system. One astrophysicist has predicted that it will collide with Earth in exactly 215 days, using a new method for modelling trajectories. The researcher has asked you and others to verify their claim using a variety of methods. You have been tasked with modelling the asteroid’s trajectory using Euler’s method.

For an asteroid to collide with Earth, it needs to have the same x, y, and z coordinates as the Earth in 215 days (x, y, and z represent the three dimensions. Because solar systems are three dimensional!). If any of the final predicted x, y, and z values are different to the Earth’s, then that will mean the asteroid will not collide with Earth in 215 days.

It has already been proven that the asteroid will have the same x and z coordinates as Earth in 215 days. So, you only need to worry about the y dimension.

The equation for the change in y at day t after the asteroid was discovered is:

y'(t) = (-0.05t+5)*exp(0.002t)*0.05

In other words, this equation is the derivative of y(t). The starting y position of the asteroid is 160.195 million kilometers, which occurs at t = 0. The y position of Earth in 215 days (t = 215) is 150 million kilometers.

Task: Write a function called will_it_hit(h), that returns the number 1 if the asteroid is going to hit Earth in 215 days, and 0 otherwise - along with the Euler’s method prediction of the asteroid’s y poistion.

The Earth will be considered ‘hit’ if the asteroid gets within 0.01 million kilometers of Earth (so abs(earthy - asteroidy)<=0.1). The input for the function is h, which indicates the step size in days. So h = 0.5 indicates that there are two steps of Euler’s method per day, resulting in 215/0.5 = 530 steps of Euler’s method.

As with the previous exercises, the output for the fdash and onestep functions should be rounded to 5 decimal places.

Use your code from the previous exercises. The only thing you will need to change is fdash. We have written tests for each of your functions (fdash, onestep, eulers, will_it_hit), to help you understand where errors might be. Good luck!

Note: All units for the y location of the asteroid should be in millions of kilometers. For example, will_it_hit(5) should give output (0, 151.72445).

Solutions

Expert Solution

#Task 1

def fdash(x):
return round((0.05*x + math.exp(0.05*x)),5) #return 0.05x+e^0.05x rounded to 5 decimal digits

#Task 4

def asteroid(x): #method to estimate asteroids trajectory
return round(((-0.05*x + 5)*math.exp(0.002*x)*0.05),5)

#Task 2

def onestep(x,y,h): #run one step of euler
return round((y + asteroid(x)*h),5) #change asteroid(x) to fdash(x) if you want to test it

#Task 3

def eulers(x,y,n,h): #run euler's method n times
for i in range(n):
y = onestep(x,y,h) #update y everytime
x = x + h #add h to x every time
return y

#Task 4

def will_it_hit(h): #method to find if it will hit
steps = round(215/h) #find number of steps
asteroidy = eulers(0,160.195,steps,h) #find y position of asteroid after 215 days
if(abs(asteroidy - 150) <= 0.01): #if distance between asteroid and earth less than 0.01 million km, it'll hit
return (1,asteroidy)
return (0,asteroidy)

#driver program
import math
print(will_it_hit(5))

Code screenshot for indentation help:

Sample run when h is 5:

In case of any doubt, drop a comment and I'll surely get back to you.

Please give a like if you're satisfied with the answer. Thank you.


Related Solutions

a summary explaining the basic understanding of the following programming concepts using a language of python:...
a summary explaining the basic understanding of the following programming concepts using a language of python: •Variables •Arithmetic and Logical operations •Sequential coding (Structured programming •Decision structure (If statements) •Repetition structure •Functions with some coding demos inside visual studio code python IDE which can be sent as screenshot. preferably a typed summary please which can be written into powerpoint pleaseeeee ???
Complete the following exercises using C programming language. Take screenshots of the code and its output...
Complete the following exercises using C programming language. Take screenshots of the code and its output where specified and paste them into in a well-labeled Word document for submission. Scenario Assume you are the CIO of an organization with three different IT department locations with separate costs. You want a program to perform simple IT expenditure calculations. Your IT expenditure target is $35,000 per site. Site expenditures: Site 1 – $35,000. Site 2 – $37,500. Site 3 – $42,500. Exercise...
Programming language to be used: Java Exercises Part 1) The Dog Class In the first part...
Programming language to be used: Java Exercises Part 1) The Dog Class In the first part of the lab, we are writing a class to represent a Dog. It should not have a main method. Dog needs fields for price (to purchase the dog), breed, name, and age. Use appropriate data types The class should have the following two kinds of Constructors: Constructor 1: Write a constructor that accepts a value for each of the fields Constructor 2: Write a...
Programming language is in python 3 For this project, you will import the json module. Write...
Programming language is in python 3 For this project, you will import the json module. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a...
Programming language is python 3 For this project, you will import the json module. Write a...
Programming language is python 3 For this project, you will import the json module. Write a class named NeighborhoodPets that has methods for adding a pet, deleting a pet, searching for the owner of a pet, saving data to a JSON file, loading data from a JSON file, and getting a set of all pet species. It will only be loading JSON files that it has previously created, so the internal organization of the data is up to you. The...
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
Using Python programming language, write a LONG piece of code that utilizes the do while function...
Using Python programming language, write a LONG piece of code that utilizes the do while function and the switch statement, please do not make It short, thank you.
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
Solve the following question by using python language In range of 1-10000 if the numbers are...
Solve the following question by using python language In range of 1-10000 if the numbers are divisible by n1 increase count by 1 , if the number is divisible by n2 increase count by 2, and if the number are divisible by n3 increase the count by 3 if none of the above conditions match for the number, increase count by the number. n1=10 +17 n2=50 +21 n3=100 +9
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT