In: Computer Science
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).
#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.