Code covered by the BSD License  

Highlights from
Environmental Modeling

image thumbnail

Environmental Modeling

by

 

20 Jul 2007 (Updated )

Introduction to Modelling and Simulation for Environmental Problems

newtondemo
function newtondemo
% Demonstration of Newton method for single variable functions     
%    using MATLAB                    
%
%   $Ekkehard Holzbecher  $Date: 2006/05/03 $
%--------------------------------------------------------------------------
toll = 1.e-7;      % tolerance
nmax = 20;         % max. no. of iterations 
x = 2.5;           % initial guess

err = toll+1; nit = 0;
while (nit < nmax & err > toll),
   nit = nit+1;
   F = f(x);
   DF = fderiv (x); 
   dx = -F/DF;
   err = abs(dx);
   x = x+dx
end
display (['Zero obtained after ' num2str(nit) ' iterations:']);
x

function F = f(x)
F = cos(x);

function DF = fderiv(x)
DF = -sin(x);

Contact us