How can I generate a vectorized version of a piecewise-defined function for use in the QUAD or QUADL for functions?
조회 수: 3 (최근 30일)
이전 댓글 표시
I have created the following function that represents a piecewise-defined function:
function y = pieces(x)
if (0<=x)&(x<=1)
y = 3*x - 2;
elseif (1<x)&(x<=2)
y = 2 - x;
else
y = 0;
end
However, when I specify this function to the QUAD function:
quad(@pieces,-5,5)
I get the following error:
??? Index exceeds matrix dimensions.
Error in ==> quad.m
On line 67 ==> if ~isfinite(y(7))
채택된 답변
MathWorks Support Team
2009년 6월 27일
The QUAD function expects the integrating function to input a vector and return a vector of the same size. The given function will accept a vector, but returns a scalar.
Update your function with the vectorized version of this piecewise-defined function which is:
function y = pieces(x)
I1 = (0 <= x) & (x < 1);
I2 = (1 <= x) & (x < 2);
I3 = ~I1 & ~I2;
y = x; % Create y same size as x
y(I1) = 3*x(I1) + 2;
y(I2) = 2 - x(I2);
y(I3) = 0;
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!