Reputation: 169
Consider the following MWE in MATLAB:
f = @(t) integral(@(x) x.^2,0,t);
integral(f,0,1);
This yields the error
Error using integral (line 85) A and B must be floating-point scalars.
(and a bit more). How do I fix this? Is this even possible? I think the problem is the variable upper bound.
Upvotes: 0
Views: 410
Reputation: 19689
If you want to use integral
then set 'ArrayValued'
to true
otherwise t
would be an invalid end point in integral(@(x) x.^2,0,t)
. So it would be:
f = @(t) integral(@(x) x.^2,0,t);
integral(f,0,1,'ArrayValued',true)
% ans =
% 0.0833
Alternately, since you're doing double integration, so use the function dedicated for this purpose i.e. integral2
. For your example, it would be:
f = @(t,x) x.^2 ;
integral2(f,0,1,0, @(t) t)
% ans =
% 0.0833
If you have Symbolic Math Toolbox, you can also use int
as int(expr,var,a,b)
but it would be slower. For your case, it would be:
syms x t;
f = x.^2;
req = int(int(f,x,0,t),t,0,1); % It gives 1/12
req = double(req); % Convert to double if required
Upvotes: 1