Search code examples
matlabintegral

Error using integral: A and B must be floating-point scalars


I want to evaluate the simple example of integral

a = max(solve(x^3 - 2*x^2 + x ==0 , x)); 
fun = @(x) exp(-x.^2).*log(x).^2;
q = integral(fun,0,a)

and the error is

Error using integral (line 85)
A and B must be floating-point scalars.

Any tips? The lower limit of my integral must be a function, not a number.


Solution

  • The Matlab command solve returns symbolic result. integral accepts only numeric input. Use double to convert symbolic to numeric. As your code is written now, already max should throw an error due to symbolic input. The following works.

    syms x;
    a = max(double(solve(x^3 - 2*x^2 + x)));
    fun = @(x) exp(-x.^2).*log(x).^2;
    q = integral(fun,0,a)
    

    Output: 1.9331.

    the lower limit of my integral must be a function, not a number

    integral is a numeric integration routine; the limits of integration must be numeric.