Question: [Python in Jupyter] How does the run time depend on step size? Use the %timeit magic command on your err function for a few step

[Python in Jupyter]

How does the run time depend on step size? Use the %timeit magic command on your err function for a few step sizes to compare. By what factor does runtime increase if step size is halved?

err function:

def err(stepSize): y0 = array([1,0]) # displaced 1m from equilibrium position x = linspace(0,100, int(100/stepSize)) y = forwardEuler(fspring,y0,x) #using function from before return max(abs(cos(x) - y[:,0]))

forwardEuler function: def forwardEuler(f,y0,tout): nsteps = tout.shape[0] y = zeros((nsteps,y0.shape[0])) y[0,:] = y0 for i in range(0,nsteps-1): h = tout[i + 1] - tout[i] y[i+1,0] = y[i,0] + h*f(y[i,:],t)[0] y[i+1,1] = y[i,1] + h*f(y[i,:],t)[1] return y

Edit:

Code to run graph showing stepsize vs error

errVecFunc = np.vectorize(err) stepSizes = logspace(-3,-1,100) loglog(stepSizes,errVecFunc(stepSizes), label = "err vectorized")

legend(loc="upper left") xlabel("step size") ylabel("error")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!