#! /usr/bin/perl -w # This file is part of ff3d - http://www.freefem.org/ff3d # Copyright (C) 2003 Laboratoire J.-L. Lions UPMC Paris # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # $Id: while.pl,v 1.1 2003/08/04 09:26:42 ArceneL Exp $ # Checking the "while" keyword. require 'check.ph'; Run(<< 'EOF'); // A simple optimization problem: finding a such that exp(a) = e. // Result should be 1. double e = 2.7182818; double a = 0; double step = 10; double error = 1; double i = 0; while(abs(error) > 1e-6){ // Measurable error error = exp(a) - e; // Step by step increase if((error < 0 && step < 0) || (error > 0 && step > 0)){ // Change search direction step = -step; // Reduce step size progressively if(step > 0) step = step / 2; } // One step in the right direction a = a + step; i = i+1; cout << "step " << i << ", a = " << a << "\n"; }; cout << "a = " << a << "\n"; cout << "error = " << error << "\n"; EOF NumCheck('^a = ',1,0.001); NumCheck('^error = ',0,1e-6); Report();