Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: Optimizing Memory Allocation in a Simple, but Long Function Date: Mon, 25 Apr 2016 14:45:55 +0100 Lines: 55 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de m7mYCtULUZ7eWca7gYwb9gCe+uKV3vZLsvcLY/anFjBw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'binary': 0.05; 'method.': 0.05; 'smallest': 0.07; 'subject:Function': 0.07; 'subject:Long': 0.09; '2016': 0.16; 'accuracy,': 0.16; 'gregory': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'to:name:python list': 0.16; 'truncation': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'math': 0.20; 'seems': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'required.': 0.26; 'error': 0.27; 'message- id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; 'integrating': 0.29; 'relative': 0.30; 'guess': 0.31; 'subject:Simple': 0.33; 'changing': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'could': 0.35; 'level': 0.35; 'needed': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'say': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'term': 0.60; 'chance': 0.60; 'high': 0.60; 'your': 0.60; 'skip:n 10': 0.62; 'goal': 0.64; 'oscar': 0.84; 'doubling': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=rDur8AIIGrlilPuI+fdXMIJX2gnpQTY8PvCfEqjwmvE=; b=siTHmPVlyrjnOeoFrfZgg2MTReaugU8neMWLtoQvllJSmoGkj91KtvWPm5j2XqE5gA vaLSlWqXrpiiAs/SIupUise5771Hr768yMDxDgO54YklLgLvkaFBdGPfdHNUhiTjmzKL 09R5hMMZLYitDHGsOgH3HHtZaqO10d1W8UI+4J+VErcP6iD4HMdAb+Hi7UmmfeMNtLsR D1gp1ELFNAClpI3eeRERhMYqEJaeG4MOL0VQRyTvLhzMm+4gN97O3tIW5PqZZCpeN2GI j/jWmSdEVOlXixk9sjtnTVbVUDkRXTKeXuAHxYM3TYq2BDPbbZrv+GXoD7p4w6gICfAd qsBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=rDur8AIIGrlilPuI+fdXMIJX2gnpQTY8PvCfEqjwmvE=; b=X/gd3mPO+kwJ+XVHWDjoo74wVoSc0KEOpfPYbEcmMAWf7m39wPylfVgx9+s48G/uHE shNgoB0any+eQfPrxiEZLbb+qX+5Q0NtUohf3QsDX24jjY7N61ZgDFrNRGLI4PMOlGeK 3jWpHJkn6VJrWauV7IM0inz7bFI/j4nwbfjCuRamMpUUbJaSJuCQPUm85PMBS7LFpp2o mJdGONYH8yRxAxTWFNhtDHDoMTmLLKoMIkr9GyGn+kj4RvSTnNlcly4xIC/LIJ7Gr8RC 7mk8h7TJLe3LmVMo0gjQUJvwavz45Wtxk+d8Ctw1Ox658YBZCSd+Bi6o9Ctx16/jpGAW sd8w== X-Gm-Message-State: AOPr4FULWiQ4GFppDXJymzgV2UA+vUiOdL11mSL/KITM2zhSXLTi3umVB7WuCDYqpActhnd9u+n4v0JyzA/Bag== X-Received: by 10.25.218.1 with SMTP id r1mr14592578lfg.130.1461591974743; Mon, 25 Apr 2016 06:46:14 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:107603 On 25 April 2016 at 08:39, Gregory Ewing wrote: > Derek Klinge wrote: >> >> Also, it seems to me if the goal is to use the smallest value of n to get >> a >> particular level of accuracy, changing your guess of N by doubling seems >> to >> have a high chance of overshoot. > > > If you want to find the exact n required, once you overshoot > you could use a binary search to narrow it down. Also you can calculate the truncation error for Euler's method. Since f(t) = f(t0) + f'(t0)*(t - t0) + (1/2)f''(t0)*(t - t0)**2 + O((t - t0)**3) Euler's method just uses the first two terms so x[n+1] = x[n] + dt*f(x[n]) the next term would be (1/2)*f'(x[n])*dt**2 Since in your case f'(x) = x and dt = 1/N that's (1/2)*x[n]*(1/N)**2 As a relative error (divide by x[n]) that's (1/2)*(1/N)**2 Let's add the relative error from N steps to get N*(1/2)*(1/N)**2 = 1/(2*N) So the relative error integrating from 0 to 1 with N steps is 1/(2*N). If we want a relative error of epsilon then the number of steps needed is 1/(2*epsilon). That is to say that for a relative error of 1e-4 we need N = 1/(2*1e-4) = 1e4/2 = 5e3 = 5000. >>> import math >>> N = 5000 >>> error = math.e - (1 + 1.0/N)**N >>> relative_error = error / math.e >>> relative_error 9.998167027596845e-05 Which is approximately 1e-4 as required. -- Oscar