Ask Your Question
3

An issue with integrals involving absolute value

asked 2025-06-02 17:05:02 +0200

RL Pouso gravatar image

updated 2025-06-02 21:44:39 +0200

slelievre gravatar image

I have been using SageMathCell for a while and some things that used to work some months before seem to have changed and do not work any longer. In particular, I found the following problem when computing elementary integrals.

Can anyone explain me why "a" yields a correctly computed result but "b" is not computed at all? What should I do to get "b" correctly computed?

x, n = SR.var('x, n')
assume(n, 'integer')
a = integral(x^2*cos(n*x), x, -pi, pi)
b = integral(abs(x)*cos(n*x), x, -pi, pi)
print('a=', a)
print('b=', b)
edit retag flag offensive close merge delete

Comments

Welcome to Ask Sage. Thank you for your question.

slelievre gravatar imageslelievre ( 2025-06-02 21:45:05 +0200 )edit

I think the integration has some issues with absolute values. I forced my absolute value function, and the result is wrong:

def myabs(x):
    if x<0:
        return -x
    else:
        return x

x, n = SR.var('x, n')
assume(n, 'integer')
a = integral(x^2*cos(n*x), x, -pi, pi)
b = integral(abs(x)*cos(n*x), x, -pi, pi)
c = integral(myabs(x)*cos(n*x), x, -pi, pi)
print('a=', a)
print('b=', b)
print('c=', c)

a= 4*pi*(-1)^n/n^2
b= integrate(abs(x)*cos(n*x), x, -pi, pi)
c= 0
tolga gravatar imagetolga ( 2025-06-04 08:22:33 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2025-06-08 14:02:13 +0200

achrzesz gravatar image

To obtain b one can use the additivity of the integral

var('n')
assume(n, 'integer')
i1=integral(abs(n*x)*cos(n*x), x,-pi,0)
i2=integral(abs(n*x)*cos(n*x), x, 0,pi)
b=i1+i2
i1.simplify_full(), i2.simplify_full(), b.simplify_full()


(((-1)^n - 1)/abs(n), ((-1)^n - 1)/abs(n), 2*((-1)^n - 1)/abs(n))

If the integrand is even, then i1=i2, but the method should work for more general functions.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2025-06-02 17:05:02 +0200

Seen: 63 times

Last updated: Jun 08