Ask Your Question
4

arrange an expression in powers of a variable

asked 2018-08-08 10:20:08 +0200

ali gravatar image

updated 2018-08-08 10:34:40 +0200

tmonteil gravatar image

I have the following code:

f0 = function('f0')(x)
f1 = function('f1')(x)
var('ep')
y = f0+ep*f1
de=ep*diff(y,x,2)+diff(y,x)
expand(de)

which gives the output:

ep^2*diff(f1(x), x, x) + ep*diff(f0(x), x, x) + ep*diff(f1(x), x) + diff(f0(x), x)

How can I rearrange this expression in powers of "ep" parameter? i.e

ep^2*diff(f1(x), x, x) + ep*( diff(f0(x), x, x) + diff(f1(x), x) ) + diff(f0(x), x)

Then I want to get the coefficient for each power (which is a differential eq) and then pass it to the desolve.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2018-08-08 19:29:14 +0200

Mafra gravatar image

updated 2018-08-08 19:38:11 +0200

Alternatively, you can use collect():

sage: de.collect(ep)
ep^2*diff(f1(x), x, x) + ep*(diff(f0(x), x, x) + diff(f1(x), x)) + diff(f0(x), x)

To get the coefficient of ep^2 you can do:

sage: de.coefficients(ep)[2][0]
diff(f1(x), x, x)

To get all the coefficients at once:

sage: de.coefficients()
[[diff(f0(x), x), 0],
 [diff(f0(x), x, x) + diff(f1(x), x), 1],
 [diff(f1(x), x, x), 2]]

If you define

sage: epcoeff = [de.coefficients(ep)[i][0] for i in range(len(de.coefficients()))]

you can access the coefficient of ep^n using epcoeff[n], so that you can feed it to desolve etc.

edit flag offensive delete link more
1

answered 2018-08-08 10:48:51 +0200

tmonteil gravatar image

updated 2018-08-08 10:50:25 +0200

You can use the power series expansion as follows:

sage: de.series(ep)
(diff(f0(x), x)) + (diff(f0(x), x, x) + diff(f1(x), x))*ep + (diff(f1(x), x, x))*ep^2 + Order(ep^20)

And then truncate it to remove the big oh:

sage: de.series(ep).truncate()
ep^2*diff(f1(x), x, x) + ep*(diff(f0(x), x, x) + diff(f1(x), x)) + diff(f0(x), x)
edit flag offensive delete link more

Comments

1

Thanks! How to access the coefficients of each power? If you noticed, I am trying to use Sage for asymptotic analysis of DE's. Is there any guide/doc for this topic?

ali gravatar imageali ( 2018-08-09 14:17:29 +0200 )edit

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: 2018-08-08 10:20:08 +0200

Seen: 569 times

Last updated: Aug 08 '18