created at April 8, 2021

Solving ODEs

to solve Ordinary Differential Equation system:

First define all variables and functions that will be used in ODE block (however, you can do it later). As example we will solve Newton's 2-nd order equation of damping forced oscillations:

Equation should be rewritten in such way that derivative of maximal order stays alone in equation's left side, like this:

Now define mass, stiffness and damping coefficients, and external force:

m = 2.5

C = 1.4e3

b = 2.3

A = 5.2

omega = 50*pi

f(x) = A*sin(omega*x)

Now start creating ODE block by using ode(name, variable) function, where name is identification name of this ODE block, and variable is independent integration variable. Execute:

ode(s1, t)

then enter equation:

x'' = (1/m)*(-C*x - b*x' + f(x))

then execute end command to finish block definition:

end

define vector containing integration variable values:

t = < 0: .001 : 5 >

Create vector with initial conditions. Since we have one equation of 2-nd order in the block, vector with init. conditions should contain two elements (for initial displacement and initial velocity). Execute:

X0 = .12

V0 = -1.5

x0 = < X0, V0 >

and finally you can solve equation by using odesolve(name, var, x0) command. Execute:

R = odesolve(s1, t, x0)

now R stores all calculated values in table (R is a 2D array). First column contains integration variable t, second column contains displacement x, and third column contains velocity v. Now you can get second column in following way:

x = vector(R[ ,2])

Another useful command is explode(name, array), which creates vectors from array with results automatically:

explode(s1, R)

and finally plot results:

plot(t, x)

In the end worksheet should look like that:

If you need to modify equations in block, type modify(blockName) command, then enter equation again and type end.