Ask Your Question

arrange an expression in powers of a variable

asked 2018-08-08 03:20:08 -0500

ali gravatar image

updated 2018-08-08 03:34:40 -0500

tmonteil gravatar image

I have the following code:

f0 = function('f0')(x)
f1 = function('f1')(x)
y = f0+ep*f1

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

answered 2018-08-08 12:29:14 -0500

Mafra gravatar image

updated 2018-08-08 12:38:11 -0500

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

answered 2018-08-08 03:48:51 -0500

tmonteil gravatar image

updated 2018-08-08 03:50:25 -0500

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


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 07:17:29 -0500 )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


Asked: 2018-08-08 03:20:08 -0500

Seen: 38 times

Last updated: Aug 08