From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: How to optimise numerical integration?
Date: Sat, 18 Sep 2010 19:56:04 +0000 (UTC)
Organization: STFC Rutherford Appleton Laboratory
Lines: 23
Message-ID: <i735gj$782$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1284839764 7426 (18 Sep 2010 19:56:04 GMT)
NNTP-Posting-Date: Sat, 18 Sep 2010 19:56:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2508071
Xref: comp.soft-sys.matlab:671140


I would like to calculate the integral of exp(-t^2)sin(t) from 0 to t and make the following calculation as efficient as possible. Currently each point of the t vector has to be evaluated individually inside the 'for loop' which takes a long time.

Does anybody know how to vectorise the 'for loop' or any other method which would beat the current code in calculation time? I've got the optimisation toolbox and the NAG toolbox available if that's of any use.

Here is the code:
t = 0:0.001:5
tic; P = integral(t); toc;
Elapsed time is 3.766351 seconds. (2 GHz intel)

integral.m file:
function [ P ] = integral( t ) 
for i = 1:length(t)  %<---- problem%
  P(i) = quad(@integrand, 0, t(i));
  function [ res ] = integrand( s )
  res = exp(- s.^2 ) .* sin( s ); 

Thanks for any help,