Question
Python - please help with my code that i've gotten so far from numpy import * import math, decimal t0 = float (input('Enter initial t
Python - please help with my code that i've gotten so far
from numpy import * import math, decimal
t0 = float (input('Enter initial t value, t0 = ')) tend = float(input('Enter final time value, tend = ')) y0 = float(input('Enter initial y value, y0 = ')) MaxN = int(input('Enter Max Steps value, MaxN = ')) print ('t0, y0, tend, MaxN = {:4f} {:4f} {:4f} {:4f}'.format (t0, y0, tend, MaxN)) dt = (tend-t0)/MaxN print('delta t, step size = (:4f)'.format(dt))
print ('MaxN (tn) (yn) yexact ERRn ERRmax) print ('==== ====== ====== ======== ====== ========')
def euler (fcn, y0, t0,tend,dt): Yn = y0 tn = t0 ERRmax = 0 for n in range (0, MaxN): Yn = Yn + dt*fcn(tn,Yn) tn = tn + dt yexact = tn*tn + 1 ERRn = abs(yexact - Yn) ERRmax = max(ERRmax,ERRn) print ('(:i.0f) (:12.6f) (:12.6f) (:12.6f) (:12.6f) (:12.6f)'.format (t0, y0, tend, MaxN)) def fcn(tn, Yn) return 2*tn
Implement the Eule scheme for the scalar IVP y' (t) f(t, y (t)) to t end r y (to) Yo The code should read (and prin out) he initial poin to yo he final time tend t and the number of steps to be executed Nsteps The evaluation of f (t,y) should be done in a Function Subprogram FCN tn, Yn) which returns the value Fn f (tn, Yn) Calculate the time-step At (t to) Nsteps, implement the Euler time-stepping end At t for n 0,1 Nsteps Yo with timesteps tn to n At r n-0,1 Nsteps r and output the pairs tn, Yn (and the exact solution when known see below NOTE: No arrays are actually needed i. Debug your code on the trivial problem y (t) 2t, 0 t 2, y(0) 1, by calculating Yexact (tn) n tn 1, and the error ERRn ABS (Yexact Yn) At each step output tn. Yn, Yexactn, ERRn. and calculate the worst overall erro ERRmax MAX ERRmax, ERRn. output ERRmax at the end of the run Before you type in the code, you should write it on paper, and execute the steps by hand calculator for, say, steps 4, to make sure the logic is right Then enter the code and check that it finds the same values Then try Nsteps 10 Note that since the RHS of this ODE is just a function of t, the solution is simply the integral of f(t) 2t, so in fact your code is performing numerical integration usually called quadrature Hhat quadrature rule does it amount to (rect trapezoidal? Simpson?) tange? 2 Test your code on y' (t) y, 0Step 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