Path: news.mathworks.com!not-for-mail
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$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1284839764 7426 172.30.248.37 (18 Sep 2010 19:56:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 18 Sep 2010 19:56:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2508071
Xref: news.mathworks.com comp.soft-sys.matlab:671140

Hi,

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));
end
  function [ res ] = integrand( s )
  res = exp(- s.^2 ) .* sin( s ); 
  end
end

Thanks for any help,
Alexander