Why I get wrong in integral function?

2 ビュー (過去 30 日間)
JIANGWEN DONG
JIANGWEN DONG 2021 年 2 月 5 日
回答済み: Steven Lord 2021 年 2 月 5 日
fun = @(x) ((x.^3)+x+1)/((x^2)+1)^2;
f = integral(fun, 1,6);

回答 (2 件)

Walter Roberson
Walter Roberson 2021 年 2 月 5 日
fun = @(x) ((x.^3)+x+1)./((x.^2)+1).^2;

Steven Lord
Steven Lord 2021 年 2 月 5 日
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.

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by