Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872# -*- coding: utf-8 -*-1"""2Created on Sat Jul 05 09:54:39 201434@author: rlabbe5"""67from __future__ import division, print_function8import matplotlib.pyplot as plt9from scipy.integrate import ode10import math11import numpy as np12from numpy import random, radians, cos, sin131415class BallTrajectory2D(object):16def __init__(self, x0, y0, velocity, theta_deg=0., g=9.8, noise=[0.0,0.0]):17theta = radians(theta_deg)18self.vx0 = velocity * cos(theta)19self.vy0 = velocity * sin(theta)2021self.x0 = x022self.y0 = y023self.x = x2425self.g = g26self.noise = noise2728def position(self, t):29""" returns (x,y) tuple of ball position at time t"""3031self.x = self.vx0*t + self.x032self.y = -0.5*self.g*t**2 + self.vy0*t + self.y03334return (self.x +random.randn()*self.noise[0], self.y +random.randn()*self.noise[1])35363738class BallEuler(object):39def __init__(self, y=100., vel=10., omega=0):40self.x = 0.41self.y = y42omega = radians(omega)43self.vel = vel*np.cos(omega)44self.y_vel = vel*np.sin(omega)45464748def step (self, dt):4950g = -9.8515253self.x += self.vel*dt54self.y += self.y_vel*dt5556self.y_vel += g*dt5758#print self.x, self.y59606162def rk4(y, x, dx, f):63"""computes 4th order Runge-Kutta for dy/dx.64y is the initial value for y65x is the initial value for x66dx is the difference in x (e.g. the time step)67f is a callable function (y, x) that you supply to compute dy/dx for68the specified values.69"""7071k1 = dx * f(y, x)72k2 = dx * f(y + 0.5*k1, x + 0.5*dx)73k3 = dx * f(y + 0.5*k2, x + 0.5*dx)74k4 = dx * f(y + k3, x + dx)7576return y + (k1 + 2*k2 + 2*k3 + k4) / 6.7778798081def fx(x,t):82return fx.vel8384def fy(y,t):85return fy.vel - 9.8*t868788class BallRungeKutta(object):89def __init__(self, x=0, y=100., vel=10., omega = 0.0):90self.x = x91self.y = y92self.t = 09394omega = math.radians(omega)9596fx.vel = math.cos(omega) * vel97fy.vel = math.sin(omega) * vel9899def step (self, dt):100self.x = rk4 (self.x, self.t, dt, fx)101self.y = rk4 (self.y, self.t, dt, fy)102self.t += dt103print(fx.vel)104return (self.x, self.y)105106107def ball_scipy(y0, vel, omega, dt):108109vel_y = math.sin(math.radians(omega)) * vel110111def f(t,y):112return vel_y-9.8*t113114solver = ode(f).set_integrator('dopri5')115solver.set_initial_value(y0)116117ys = [y0]118while brk.y >= 0:119t += dt120brk.step (dt)121122ys.append(solver.integrate(t))123124125def RK4(f):126return lambda t, y, dt: (127lambda dy1: (128lambda dy2: (129lambda dy3: (130lambda dy4: (dy1 + 2*dy2 + 2*dy3 + dy4)/6131)( dt * f( t + dt , y + dy3 ) )132)( dt * f( t + dt/2, y + dy2/2 ) )133)( dt * f( t + dt/2, y + dy1/2 ) )134)( dt * f( t , y ) )135136def theory(t): return (t**2 + 4)**2 /16137138from math import sqrt139dy = RK4(lambda t, y: t*sqrt(y))140141t, y, dt = 0., 1., .1142while t <= 10:143if abs(round(t) - t) < 1e-5:144print("y(%2.1f)\t= %4.6f \t error: %4.6g" % (t, y, abs(y - theory(t))))145146t, y = t + dt, y + dy(t, y, dt)147148t = 0.149y=1.150151def test(y, t):152return t*sqrt(y)153154while t <= 10:155if abs(round(t) - t) < 1e-5:156print("y(%2.1f)\t= %4.6f \t error: %4.6g" % (t, y, abs(y - theory(t))))157158y = rk4(y, t, dt, test)159t += dt160161if __name__ == "__main__":1621/0163164dt = 1./30165y0 = 15.166vel = 100.167omega = 30.168vel_y = math.sin(math.radians(omega)) * vel169170def f(t,y):171return vel_y-9.8*t172173be = BallEuler (y=y0, vel=vel,omega=omega)174#be = BallTrajectory2D (x0=0, y0=y0, velocity=vel, theta_deg = omega)175ball_rk = BallRungeKutta (y=y0, vel=vel, omega=omega)176177while be.y >= 0:178be.step (dt)179ball_rk.step(dt)180181print (ball_rk.y - be.y)182183'''184p1 = plt.scatter (be.x, be.y, color='red')185p2 = plt.scatter (ball_rk.x, ball_rk.y, color='blue', marker='v')186187plt.legend([p1,p2], ['euler', 'runge kutta'])188'''189190