# Integrate piecewise function with change of variable

I would like to integrate a piecewise defined function while operating a change of variable. I start by defining the function and another variable involved in the change of variable:

phi(x)   = piecewise([([-1,1], (1-abs(x))*(1-abs(x))*(1+2*abs(x)))]);
phi(x)   = phi.extension(0);
h=pi/n;
h=h.n();


What I would like to do is integrate the function phi(x/h-1) between 0 and pi so I try it and results in

integral(phi(x/h-1),x,0,pi)
ValueError: substituting the piecewise variable must result in real number


So I then try to use another variable which I try to define to be 'real'

t=var('t')
assume(t,'real');
integral(phi(t/h-1),t,0,pi)


but it results in the same error... Now I try the "lambda" method since it worked when calling the plot function with the same change of variable; but fail again

integral(lambda t: phi(t/h-1),t,0,pi)
TypeError: unable to convert <function <lambda> at 0x16d71f140> to a symbolic expression


Now I try to use another integration method with definite_integral but get the same errors, only different for the "lambda" method

definite_integral(lambda x: phi(x/h-1),x,0,pi)
TypeError: cannot coerce arguments: no canonical coercion from <type 'function'> to Symbolic Ring


Is there any way around this? I really do not know what else to try...

edit retag close merge delete

Sort by » oldest newest most voted

This has to do with the implementation of piecewise, I think.

I can get a numerical integral to work by doing the following:

phi(x)=(heaviside(x+1)-heaviside(x-1))*(1-abs(x))*(1-abs(x))*(1+2*abs(x))
numerical_integral(phi(x/h-1),0,pi)


Another option would be to stop the integral at the point at which the function is zero.

phi2(x)=(1-abs(x))*(1-abs(x))*(1+2*abs(x))
integral(phi2(x/h-1),x,0,2*pi/n)

more

@calc314 This is awesome... Had I thought about Heaviside for defining my piecewise functions, many of my problems in Sage would have been solved instantly... Thanks