2

In NumPy, I'm trying to represent differential equations of the form: y' = p(t)y + g(t), where p(t) is an nxn matrix and and g(t) is an nx1 matrix. Something like:

y' = [[1,5], [2,1]] + [[e^t], [1]]

I know how to represent matrices in NumPy, but how would I represent matrices that contain variables (for example, 2t or e^t)?

1 Answer 1

1

A 'variable' in this sense (as in, y is a function of t) should probably be represented by a 1d array of that variable's domain. This would increase the dimension of your array (making it (n, n, m) where m is the size of your domain (length of t).

If you plan on using a scipy ode solver, then you write it as a function, so instead of

t = np.arange(0, 10, .1)
y' = [[1,5]*len(t), [2,1]*len(t)] + [[np.exp(t)], [1]*len(t)]

you need to do something like:

def yderiv(t):
    return [[1,5], [2,1]] + [[np.exp(t)], [1]]
Sign up to request clarification or add additional context in comments.

10 Comments

I'm not sure if e^t does what you expect it to. Did you mean something like np.exp(t)?
@Hooked yeah. The matrix can contain variables.
Thanks @Hooked, you're probably right. I just copied from above.
@askewchan yes I do mean that. I'm trying to solve ODEs on my own without SciPy using NumPy. Maybe use SciPy integration but I need solve the actual ODE alone and not using a library. To use a 1d array of that variable's domain, would it be like: `g = [2t, 1, 2], where the last element 2 is the size? I'm afraid I don't get it.
numpy.poly1d represents a polynomial. A matrix [[np.exp(t)], [1]] is not a polynomial, however. np.exp(t) throws an error when used alone since t is not defined.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.