Why I get wrong in integral function?

2 views (last 30 days)
JIANGWEN DONG
JIANGWEN DONG on 5 Feb 2021
Answered: Steven Lord on 5 Feb 2021
fun = @(x) ((x.^3)+x+1)/((x^2)+1)^2;
f = integral(fun, 1,6);

Answers (2)

Walter Roberson
Walter Roberson on 5 Feb 2021
fun = @(x) ((x.^3)+x+1)./((x.^2)+1).^2;

Steven Lord
Steven Lord on 5 Feb 2021
fun = @(x) ((x.^3)+x+1)/((x^2)+1)^2;
f = integral(fun, 1,6);
Because you are using the matrix power operator (^) instead of the element-wise power operator (.^) in several places as well as the matrix division operator (/) rather than the element-wise (./) your function fails to satisfy the requirement that the fun input argument section of the documentation page for the integral function states it must.
"For scalar-valued problems, the function y = fun(x) must accept a vector argument, x, and return a vector result, y. This generally means that fun must use array operators instead of matrix operators. For example, use .* (times) rather than * (mtimes). If you set the 'ArrayValued' option to true, then fun must accept a scalar and return an array of fixed size."
Either use the element-wise (array) operators instead of the matrix operators (like Walter suggested) or set the 'ArrayValued' option to true as in the "Vector-Valued Function" example on that documentation page.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!