关键词 > Python代写

Python Lab-13: Plotting and Using NumPy Arrays.

发布时间:2022-04-21

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Python Lab-13: Plotting and Using NumPy Arrays.

1. Write a Python program script to compute f(x) and f’(x) for the given decaying sine function on the domain interval , where a = 0.25, and b = 1. Use arrays and NumPy functions to do the calculations.

· The program will write a comma separated values (CSV) data file with three columns of comma separated data consisting of the domain values, function values, and the derivative values: x, f(x), and f’(x) respectively. File extension is csv. You may use savetxt from numpy.

· Use scientific notation when writing your floating point values into the data file: as in %16.8e rather than %16.8f.

· Use 201 equally spaced data points on the specified domain interval .

· NumPy has functions for creating and manipulating arrays. Use NumPy to compute the function values; use the supplied num_deriv UDF (user defined function) to compute the first derivative.

, , a = .25, b = 1.

2. Write a Python script to read the data file from the previous problem.

· Read the data values from the file into three NumPy arrays. You may use loadtxt from numpy and array slicing.

· Plot the function and its derivative. Use different colors for each function curve.

· Provide this plot title: f(x) = exp(-0.25*x)*sin(x) and f’(x)

· Include a legend labeling the color for f(x) and f’(x).

· Label the horizontal axis as the Time.

· Label the vertical axis as the Amplitude.

3. Write a Python program script to compute f(x), f’(x) and f”(x) on the domain interval for the given Guassian Normal Distribution function. Let the arithmetic mean,  μ = 0 ; and, the standard deviation σ  = 1.

,

· Use 1001 equally spaced data points along the domain interval.

· Compute the domain values on the specified interval and store them in an array.

· Compute the range values y = f(x). Use NumPy tools to compute the function.

· Estimate the first derivative. Use the attached UDF.

· Estimate the second derivative. Use the attached UDF with the first derivative vector as the input. Note this is not the most accurate way to estimate the second derivative, but it works fine if the delta-x is small enough.

· Write four columns of output into the CSV file: x,  f(x) , f’(x) and f”(x

4. Write a Python program script to:

· Read all the data generated for the Gaussian Normal distribution into NumPy arrays.

· plot the domain and f(x), f’(x), and f”(x).

· Annotate the plot with a title, legend, and labels for both the vertical and horizontal axes. Label the vertical axis as the Range, and the Horizontal axis as the Domain.

5. Modify the previous program to make three separate Figures for the: (1) function, (2) derivative, and (3) 2nd derivative. Label the plots and axes. For example, plt.figure(1) creates a new figure 1.

6. Submit your plots, script and data files to Harvey-Blackboard for grading. PDF screenshots are acceptable for plots.

# Add this UDF to the beginning of your code

import numpy as np

def num_deriv(yvec,dx):

    # input: array of y values on domain of uniformly spaced points

    # input: dx spacing between x domain point

    # return: array of numerical first derivative values

    npts = len(yvec)

    ypr = np.zeros(xvec.shape)       # intialize all 0

    ypr[0] = (yvec[1]-yvec[0])/dx    # ypr of leftmost point, forward deriv

    for i in range(1,npts-1):        # ypr of middle points, central deriv

        ypr[i]=(yvec[i+1] - yvec[i-1])/(2*dx)  

    ypr[-1]= (yvec[-1]-yvec[-2])/dx  # ypr of rightmost point, backwards deriv

    return(ypr)