MATLAB Answers


How can I shrink the time of getting the results please?

Asked by Avan Al-Saffar on 28 May 2015
Latest activity Commented on by Avan Al-Saffar on 9 Jun 2015
I run the following code for 13 days until I got the results so could anyone advise me if there is any truck that I can use to shrink the time please? Because I need to run the code for different values!
function FisherN01analyticalsmallomega
b = 2.5
t =(0:0.1:10000);
y = logisticOscilanalytical(t,c,y0,b,a);
Y = @(T) interp1(t,y,T);
g = @(t) ( (b.*sin(b.*t) + cos(b.*t).* (2.*Y(t)./c)).^2./(a.^2.*sin(b.*t).^3 .* (Y(t) - (Y(t).^2).^2))) );
t= (0:10:10000);
N = length(t);
JT = zeros(1,N);
for i = 1:N
JT = integral(g,0,t(1,i),'ArrayValued',true);
% function y = logisticOscilanalytical(t, c, y0, b, a)
% ee = exp( (a/ b) * (1- cos( b*t)));
% y = (-c.* y0.* ee)./ ( (y0 - c) - y0.* ee);
% end


I've formatted the code, because it was not readable. Please use the "{} Code" button. Do not post code which is commented out - this confuses the readers.

Sign in to comment.

1 Answer

Answer by Mike Hosea
on 1 Jun 2015
Edited by Mike Hosea
on 1 Jun 2015
 Accepted Answer

That code never gave you results because it isn't working--there is a syntax error. Specific suggestions are therefore hard to provide. Some more or less generic suggestions would be:
  • Debug your code using a smaller t range.
  • Avoid the 'ArrayValued' option unless your function is really array-valued. Using that option with a scalar-valued problem avoids the need to make your integrand handle array inputs, but it slows INTEGRAL significantly. If only compensating for an integrand that can't handle array inputs (and ONLY if the integrand can't handle array inputs), consider using ARRAYFUN, e.g. integrate
g1 = @(t)arrayfun(g,t);
  • If you're going to use an interpolated function (this will affect accuracy negatively), supply the abscissas as 'Waypoints'. In this example you'll need to save the original t values (they are overwritten in the supplied code).
q = integral(g1,a,b,'Waypoints',t);
  • If you want to generate a table of results, consider only integrating between the tabulated points and adding to an accumulator variable as you go, i.e.
JT = zeros(1,N);
s = 0;
for i = 2:N
q = integral(g,t(1,i-1),t(1,i));
s = s + q;
JT(i) = s;
% The above could be JT(i) = JT(i-1) + q; to do without s.
  • It looks like your integrand has a singularity when sin(b*t) == 0. You'll have to integrate between those values. Unfortunately, they can't just be 'Waypoints', and there is no guarantee that INTEGRAL will be able to handle the singularity.

  1 Comment

Many thanks, your suggestion about 'Waypoints' solve my problem completely.

Sign in to comment.