Technical Solutions
How can I generate a vectorized version of a piecewise-defined function for use in the QUAD or QUADL for functions?
Date Last Modified: Friday, June 26, 2009
| Solution ID: | 1-1A4J2 | |
| Product: | MATLAB | |
| Reported in Release: | R13 | |
| Platform: | All Platforms | |
| Operating System: | All OS |
Subject:
How can I generate a vectorized version of a piecewise-defined function for use in the QUAD or QUADL for functions?
Problem Description:
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))
Solution: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. |
|
|
Store

