Group Abstract Group Abstract

Message Boards Message Boards

How can I extract part of a function from NDSolve?

Posted 3 years ago

So I have a linear differential equation which depends on a function f(x) which is piecewise defined, let's say in regions A, B, C. Due to the form of f(x), it's easy to find a basis for the solutions in regions A and C, but in B no closed form solutions exist as far as I know. What I want to do is try different initial conditions in A, solve the full equation with NDSolve, and then pull out the results in region C. I know the basis so I'd like to know exactly which linear combination of that basis gives the particular solution in region C.

EDIT: some sample code of an example I threw together

f[x_] = Piecewise[{{x^3 + 9, 0 < x < 2}}, 1]
ode = g''[x] + f[x]*g[x] == 0
soln[x_] = 
 g[x] /. NDSolve[{ode, g[0] == 0, g'[0] == 1}, g[x], {x, -20, 20}]
Plot[soln[x], {x, -20, 20}]

The soln[x] for x>2 should be in the form fSin[x] + gCos[x] and I want to compute the constants f and g. Is there a way to extract this from the NDSolve output?

POSTED BY: Bryan Foo
4 Replies

Try this:

Plot[{soln[x], soln[2] Cos[x - 2] + soln'[2] Sin[x - 2]}, {x, -20, 20}]

Hints: $\cos 0 = 1$ and the derivative of $\sin x$ at $x = 0$ is also $1$. A solution is uniquely determined by a function value and a derivative value at a single value of $x$.

POSTED BY: Michael Rogers
Posted 3 years ago

Thank you, I know how to get the solution if it's relatively simple like this; I gave a simple example just for sake of example, but is there code that does this numerically for more general cases where there might not be such a straightforward closed-form solution in terms of the boundary conditions?

POSTED BY: Bryan Foo

Hi, I cannot imagine such a case. Do you have an example? I don't understand what you mean by "simple" or "more general". The above works for any linear ODE (with suitable coefficients), and the desired form expressed in the question, $f \sin x + g \cos x$, with $f,g$ constants, implies the ODE is linear.

A slightly more general formula, still for a linear ODE, would be

LinearSolve[{{a[x0], b[x0]}, {a'[x0], b'[x0]}}], {soln[x0], soln'[x0]}] . {a[x], b[x]}

where a[x] and b[x] are linearly independent solutions. It's more general in that it does not depend on a[x] and b[x] having 0/1 for both their and their derivatives' values at a particular point.

POSTED BY: Michael Rogers
Posted 3 years ago

This is exactly what I was looking for. By more general I just meant for a basis such that that solving for the coefficients analytically is difficult (for instance if the ODE is of higher order); I somehow completely forgot that this can be reduced into a matrix equation. Thank you!

POSTED BY: Bryan Foo
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard