Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions

Question

Were they made on a timely basis?

Answered: 1 week ago

Question

What is DDL?

Answered: 1 week ago