3

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.

2 Answers2

2

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.

0

Check values of a by mouse over in breakpoint or removing the ; from the end of the line so it prints a. Based on the error, a is not a scalar float. You might need another max() or double() statement to transform the vector to a single value.

Solve Help : http://www.mathworks.com/help/symbolic/solve.html

Integral Help : http://www.mathworks.com/help/ref/integral.html

LawfulEvil
  • 2,267
  • 24
  • 46
  • Using == in solve is correct syntax. `=` would throw an error, illegal assignment. –  Apr 22 '15 at 18:44
  • You are looking at MuPad help page. [Here's Matlab function help page for solve](http://www.mathworks.com/help/symbolic/solve.html). –  Apr 22 '15 at 18:48
  • LOL. My bad. You can leave off the == 0 part entirely, as it is assumed. – LawfulEvil Apr 22 '15 at 18:51