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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started