Question: Please answer both questions using Python. 10-2 Problem 1 Python functions to create a data file (7 pts) (a) Define 2 Python functions that return
Please answer both questions using Python.

10-2 Problem 1 Python functions to create a data file (7 pts) (a) Define 2 Python functions that return respectively f(1) = sin(x+2) and g(1) (b) Define a Python function of 3 variables h(x, y, z) = 008(x + y2); We will then want to generate data using these functions and write it to a file. More specif- ically, we want to calculate 1, y = f(x), : = 9(1), h(x,y,z) for 1001 values of a uniformly spaced between 0 and 2x inclusive (i.e., including 0 and 2x among the 1001 points) Call the data file data.dat (we'll usually use dat extensions for data files). It should contain 4 columns, and you'll want to make sure that the numbers are aligned in nice columns (Hint: use format). Problem 2 Taylor (Maclaurin) series functions (8 pts) We want to write a Python script that can compute the sum of the Maclaurin series for sin(x), COS(X) and exp(x) for a given value of x and a given value of terms that need to be added. You will do this by writing 3 functions: sine_series, cosine_series and exp_series, respectively. However, you are not allowed to use the math factorial command nor the sum command, so you will need to write two separate functions for these that you can call inside your 3 functions. Once you have written these 3 functions, you can write 3 much shorter and simpler func- tions, sine_series_simple, cosine_series_simple and exp_series_simple that do the same but can make use of the math.factorial and sum commands, and make use of list comprehension to calculate things very simply "To check if your functions are working properly, you should pick any value of x and a large value of terms that need to be added (more than 20), and compute the sum of the sin(x). cos(x) and exp(x) using the 3 different functions and compare their results (which should be identical) to the simpler functions by printing these numbers with some explanation. 1/2 PHYS 162. Updated Spring 2021 HW 3. Functions in Python If you are not familiar with the Taylor series the one for cosine is used in "Lecture Notes 1: Introduction to Scientific Computing and Python." This equation shows the series of cosine (in this particular equation the sum goes to 10, but in general, one can leave the number of terms as "n"). As you saw in page 5 of the same set of notes, you can also use list comprehension to very easily do the sum for a given value of x and a given value of terms that need to be added. All three expansions are written below for references sin(x) = -1)+22+1 (1) (2j+1)! cos(2) (-192 (27)! (2) exp(x) (3) You are probably already familiar with these series. If not, please see equation (1) in "Lecture Notes 1: Introduction to Scientific Computing and Python." This equation shows the series of cosine (in this particular equation the sum goes to 10, but in general, one can leave the number of terms as "n"). As you saw in page 5 of the same set of notes, you can also use list comprehension to very easily do the sum for a given value of x and a given value of terms that need to be added 10-2 Problem 1 Python functions to create a data file (7 pts) (a) Define 2 Python functions that return respectively f(1) = sin(x+2) and g(1) (b) Define a Python function of 3 variables h(x, y, z) = 008(x + y2); We will then want to generate data using these functions and write it to a file. More specif- ically, we want to calculate 1, y = f(x), : = 9(1), h(x,y,z) for 1001 values of a uniformly spaced between 0 and 2x inclusive (i.e., including 0 and 2x among the 1001 points) Call the data file data.dat (we'll usually use dat extensions for data files). It should contain 4 columns, and you'll want to make sure that the numbers are aligned in nice columns (Hint: use format). Problem 2 Taylor (Maclaurin) series functions (8 pts) We want to write a Python script that can compute the sum of the Maclaurin series for sin(x), COS(X) and exp(x) for a given value of x and a given value of terms that need to be added. You will do this by writing 3 functions: sine_series, cosine_series and exp_series, respectively. However, you are not allowed to use the math factorial command nor the sum command, so you will need to write two separate functions for these that you can call inside your 3 functions. Once you have written these 3 functions, you can write 3 much shorter and simpler func- tions, sine_series_simple, cosine_series_simple and exp_series_simple that do the same but can make use of the math.factorial and sum commands, and make use of list comprehension to calculate things very simply "To check if your functions are working properly, you should pick any value of x and a large value of terms that need to be added (more than 20), and compute the sum of the sin(x). cos(x) and exp(x) using the 3 different functions and compare their results (which should be identical) to the simpler functions by printing these numbers with some explanation. 1/2 PHYS 162. Updated Spring 2021 HW 3. Functions in Python If you are not familiar with the Taylor series the one for cosine is used in "Lecture Notes 1: Introduction to Scientific Computing and Python." This equation shows the series of cosine (in this particular equation the sum goes to 10, but in general, one can leave the number of terms as "n"). As you saw in page 5 of the same set of notes, you can also use list comprehension to very easily do the sum for a given value of x and a given value of terms that need to be added. All three expansions are written below for references sin(x) = -1)+22+1 (1) (2j+1)! cos(2) (-192 (27)! (2) exp(x) (3) You are probably already familiar with these series. If not, please see equation (1) in "Lecture Notes 1: Introduction to Scientific Computing and Python." This equation shows the series of cosine (in this particular equation the sum goes to 10, but in general, one can leave the number of terms as "n"). As you saw in page 5 of the same set of notes, you can also use list comprehension to very easily do the sum for a given value of x and a given value of terms that need to be added
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
