Mathematics Python program

Mathematics Python program

Math 382 Assignment 5

Note: Each problem counts 20 points (double).
Due: Nov 10, 2014
1. The functionnumpy.polyfit(x,y,n)will return the coecients of a least squares polynomial
t of degreen, similar to the t you did in homework set 4. To evaluate the t, use the
function numpy.polyval(fit,x)
import numpy as np
#… stuff in your program …
#… put x values into list xvals
#… put y values into list yvals
#… do a polynomial fit of degree n on the xvals and yvals
coefficients = np.polyfit(xvals, yvals, n) # get a degree n fit
# create a list of predicted values at all the xvals
yfits = [np.polyval(coefficients, x) for x in xvals]
Write a program that does the following:
(a) Reads a data le of comma-separated values. Each line will have a pair of values,
representing one (x;y) pair, that will have a comma (and possibly some additional
white space) between them. Use the data setpigs.csvprovided on moodle for this
exercise.
(b) Separate the data into two data sets of equal size. Pick the points in each data set
randomly. One way to do this is use
m=len(xvals)
rm = range(m)
choices=np.random.choice(rm, m)
Thenchoiceswill contain a randomly selected list of indices of length m/2 from the
integers 0,1,2,….,m-1. Then you can use a for loop or list comprehension to assign the
values to each data set.
(c) Perform polynomial ts of degree 2,3,…,25 on data set 1.
(d) Using the 25 polynomial ts you found in the previous step (or in the same loop as you
are nding the ts) calculate the root-mean-squared error between the predicted and
actual y value using the second data set, that is, the data that you did not use to nd
the ts. You will get one RMS number for each t, the RMS error for degree j is
error(j) =
v
u
t
1
n
n 1 X
i=0
(y
data;i y
polyval;i
)
2
(1)
wherey
polyval
is the polynomial t for degree j.
1
(e) Make a plot of error(j) vs j for j = 1;2;:::;25 Based on your results, determine the
optimum degree t.
Note: because of the randomness, you will get di erent answers if you run the program
multiple times. Before you make any conclusions you will want to run your programs
several times.
2. The Lorenz Oscillator is described by the equations
dx
dt
=(y(t) x(t))
dy
dt
=x(t) x(t)z(t) y(t)
dz
dt
=x(t)y(t) z(t)
where = 8:0=3:0,= 10:0, and= 28:0 are constants.
(a) Solve the Lorenz equations using odeintfor a time interval of [0, 50] using initial con-ditionsx(0) = 0, y(0) = 1, andz(0) = 0. Use at least 50,000 steps in your integration.
(b) Make a plots of x(t), y(t) and z(t) on a single graph. Plot each variable in a di erent
color, and put a legend on the plot labelling the three lines.
(c) Make a 3D line plot of the solution vector (x;y;z) as a function of time.
(d) Repeat part (a), but this time use the initial conditions x(0) = 0, y(0) = 1, and
z(0) = 10
10
.
(e) Using the results of part 2d, repeat the plot you made in part 2b. Make sure to use
the same colors you used previously. Compare your plot with the earlier plot. How are
they the same, and how are they di erent? Interpret your results. Now repeat the 3D
plot you made in 2c with the results of part 2d. Is there any di erence in these plots?

PLACE THIS ORDER OR A SIMILAR ORDER WITH US TODAY AND GET AN AMAZING DISCOUNT 🙂

© 2020 customphdthesis.com. All Rights Reserved. | Disclaimer: for assistance purposes only. These custom papers should be used with proper reference.