Question

I am attempting to use Octave to solve for a differential equation using Euler's method.

The Euler method was given to me (and is correct), which works for the given Initial Value Problem,

y*y'' + (y')^2 + 1 = 0; y(1) = 1;

That initial value problem is defined in the following Octave function:

function [YDOT] = f(t, Y)

YDOT(1) = Y(2);
YDOT(2) = -(1 + Y(2)^2)/Y(1);

The question I have is about this function definition. Why is YDOT(1) != 1? What is Y(2)? I have not found any documentation on the definition of a function using function [YDOT] instead of simply function YDOT, and I would appreciate any clarification on what the Octave code is doing.

Was it helpful?

Solution

First things first: You have a (non linear) differential equation of order two which will require you to have two initial conditions. Thus the given information from above is not enough.

The following is defined for further explanations: A==B means A is identical to B; A=>B means B follows from A.

It seems you are mixing a few things. The guy who gave you the files rewrote the equation in the following way:

y*y'' + (y')^2 + 1 = 0; y(1) = 1; | (I) y := y1 & (II) y' := y2

(I) & (II)=>(III): y' = y2 = y1' | y2==Y(2) & y1'==YDOT(1) 

Ocatve is "matrix/vector oriented" so we are writing everything in vectors or matrices. Rather writing y1=alpha and y2=beta we are writing y=[alpha; beta] where y(1)==y1=alpha and y(2)==y2=beta. You will soon realize the tremendous advantage of using especially this mathematical formalization for ALL of your problems.

(III) & f(t,Y)=>(IV): y2' == YDOT(2) = y'' = (-1 -(y')^2) / y

Now recall what is y' and y from the definitions in (I) and (II)!

y' = y2 == Y(2) & y = y1 == Y(1)

So we can rewrite equation (IV)

(IV): y2' == YDOT(2) = (-1 -(y')^2) / y == -(1 + Y(2)^2)/Y(1)

So from equation (III) and (IV) we can derive what you already know:

YDOT(1) = Y(2)
YDOT(2) = -(1 + Y(2)^2)/Y(1)

These equations are passed to the solver. Differential equations of all types are solved numerically by retrieving the "next" value in a near neighborhood to some "previously known" value. (The step size inside this neighborhood is one of the key questions when writing solvers!) So your solver uses your initial condition Y(1)==y(1)=1 to make the next step and calculate the "next" value. So right at the start YDOT(1)=Y(2)==y(2) but you didn't tell us this value! But from then on YDOT(1) is varied by the solver in dependency to the function shape to solve your problem and give you ONE unique y(t) solution.

It seems you are using Octave for the first time so let's make a last comment on function [YDOT] = f(t, Y). In general a function is defined in this way:

function[retVal1, retVal2, ...] = myArbitraryName(arg1, arg2, ...)

Where retVal is the return value or output and arg is the argument or input.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top