I'm trying to use the MATLAB integral()
function to integrate along a single parameter of a piecewise linear function that represents a time-varying value.
I would like to define an anonymous function based on the original function:
t1 = 1;
t2 = 2;
t3 = 4;
t4 = 5;
a0 = 1;
a1 = 2;
f = @(x) accel_profile(t1,t2,t3,t4,a0,a1,x);
and here is the accel_profile.m:
function value = accel_profile(t1,t2,t3,t4,a0,a1, t)
if t <= t1
value = a0;
return
elseif (t <= t2)
value = ((t-t1)/(t2-t1)) * (a1-a0) + a0;
return
elseif (t <= t3)
value = a1;
return
elseif (t <= t4)
value = ((t-t3)/(t4-t3)) * (a0-a1) + a1;
return
else
value = a0;
return
end
The problem is that when I exercise the following script:
t_list = 0:0.1:6;
q = zeros(1,length(t_list))
for i = 1:length(t_list)
q(i) = integral(f,0,t_list(i));
end
plot(t_list, q)
I get the following stack trace:
Error using integralCalc/finalInputChecks (line 515)
Output of the function must be the same size as the input. If FUN is an array-valued integrand, set
the 'ArrayValued' option to true.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
515 error(message('MATLAB:integral:FxNotSameSizeAsX'));
I'm running MATLAB 2015b on Windows 7.
See Question&Answers more detail:
os