Question

Using Python: Create a code to generate 1001 values between 0 and 10 (essentially, start from...

Using Python: Create a code to generate 1001 values between 0 and 10 (essentially, start from 0 and use increment 0.01) and for each value ? ∈ [0,10] calculate sin(?) and the derivative of ?(x) = sin(?) using the central difference method: ????(?)/?? = ?(?+0.01)−?(?−0.01) 2∙0.01 . Output the result in a file using the print command: print (?, ???(?) , ????(?), ??? =′ ′, ??? =′\ ? ′ , ???? = ??) Recall that the output file must be opened initially (and named: ff=open(…) ), and the closed ( ff.close() ) at the end of your script. Use excel to draw a graph of your result ????(?) vs. ? and convince yourself that you essentially obtained the ???(?) function. Note that the values for ? can be generated in a loop: for ? in range(1001): ? = ?·0.01. Also, to avoid having numbers with 16 decimal places (as the result of ???ℎ. sin(?) function) round ???(?) , ????(?) to 4 decimal places; utilize round command as: sinx_round=round( ???ℎ. sin(?),4), etc.

Homework Answers

Answer #1

Here is the code to do so, i have used numpy to do the mathematical operations:

Here is the text file generated:

This is only a sample view, you can check it for yourself, it will be saved where you have stored the python notebook.

To check if the data resembles a cosine graph, we will plot it in python itself:

It resembles a cosine graph, so it is correct.

The codes are well commented and easy to understand, if the answer helped you please upvote and if you have any doubts please comment i will surely help. please take care of the indentation while copying the code. Check from the screenshots provided.

Code:

import numpy as np
# open the file
f= open("data.txt","w+")

# generate linearly spaced data
x = np.linspace(0,10,1001)
# using list comprehension ot generate a list having sin(x) for all x
# and using the list comprehension ot generate a list having derivative
# for sin(x), using the finite difference model
sinx = [round( np.sin(i), 4) for i in x]
derivate_sinx = [round( (np.sin(i+0.01) - np.sin(i-0.01))/0.02 , 4) for i in x ]
# write the data to the file
print(x, sinx , derivate_sinx, sep='', end ='\n' , file=f)
# close the file
f.close()

here is the code to plot the graph:

import matplotlib.pyplot as plt

x = np.linspace(0,10,1001)
sinx = [round( np.sin(i), 4) for i in x]
derivate_sinx = [round( (np.sin(i+0.01) - np.sin(i-0.01))/0.02 , 4) for i in x ]
plt.plot(x, derivate_sinx)
plt.ylabel('d(sinx)/dx')
plt.xlabel('x')
plt.show()

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Q) a) create a matrix named as X of evenly spaced values from 0 to 50,...
Q) a) create a matrix named as X of evenly spaced values from 0 to 50, with an increment of 10. b) a) create a matrix named as Y of evenly spaced values from 500 to 1000, with an increment of 3. c)a) create a matrix named as Z, the value of the 1st row is from 1 to 10, 2nd row from 2 to 20 with increment of 2, 3rd row 3 to 12. using subplot divide the window...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT