I have an anonymous function in matlab: f=@(x) x/(1+x^4). I want to integrate it from 0 to 1 with quad, but for some reason mpower is complaining that it can’t take x^4 because “Inputs must be a scalar and a square matrix.” If I replace x^4 with x*x*x*x, I get “Error using ==> mtimes Inner matrix dimensions must agree.” Here’s the full error output.
??? Error using ==> mpower
Inputs must be a scalar and a square matrix.
Error in ==> intapprox>@(x)x/(1+x^4) at 4
f=@(x) x/(1+x^4);
Error in ==> quad at 77
y = f(x, varargin{:});
Error in ==> intapprox at 27
area = quad(f,0,1);
Use
f=@(x) x./(1+x.^4);i.e../and.^since you don’t want to do a matrix divide and power. Then I getWhich is consistent with the analytical value of pi/8.