# Ode45 second order matlab

#### I. The build-in matlab function ode45

matlab can be used to solve numerically second and higher order ordinary differential equations subject to some initial conditions by transfering the problem into equivalent 2 x 2 system of ordinary differential equations of first order. A function for numerical solution of such systems is, for example, $$\texttt{ode45}$$ . Documentation for $$\texttt{ode45}$$ states the standard usage format. One can get more advanced information by including more input/output parameters, but we start with a simplest case.

[Tout, Yout] = ode45(ODEFUN,TSPAN,Y0)

Deciphering this definition, we see that $$\texttt{ode45}$$ ;

• gives pairs $$\texttt{(t, y(t))}$$ values as output vectors $$\texttt{(Tout, Yout)}$$
• needs a function $$\texttt{ODEFUN}$$ that can either be a matlab function file or an anonymous function. It is important that it is defined in such a way that it returns a column vector for arguments $$\texttt{(t, y(t))}$$ .
• needs a $$\texttt{TSPPAN}$$ vector of time domain we are interested to solve our equation in. The function will solve the equation numerically by choosing an appropriate time-step.
• needs the initial condition $$\texttt{Y0}$$

• Example. We present four equations of first, second, and third order to demonstrate how ode function works. To solve a differential equation, the symbolic function y(t) must be defined using the syntax "syms" as such:

Now that y(t) is defined, the dsolve function can be used. The input of dsolve is
dsolve("differential equation", "initial condition").
To express derivatives, the diff function is used. The input of the diff function is
diff("variable being differentiated", "variable that the first variable is being differentiated with respect to").
Standard boolean operators such as "==" denoting "equal to" may be used. The following line of code solves the differential equation y' = y*t^2 for y, with the initial condition y(0) = 2 y(t) = 2*exp(t^3/3)

Nonlinear differential equations are differential equations where the dependent variable or any of its derivatives are not linear. They are generally more difficult to solve, but Matlab can solve these too! It is important to note that even if there are initial conditions, nonlinear differential equations can have more than one solution. matlabwill list all solutions regardless. In the following example, a function x(t) is a solution of the nonlinear differential equation (x' + 2x)^2 = 3 subject to the initial conditions x(0) = 0. Notice there are two solutions.

x(t) =
3^(1/2)/2 - (3^(1/2)*exp(-2*t))/2
(3^(1/2)*exp(-2*t))/2 - 3^(1/2)/2

To solve a second order ODE with initial conditions, a second function must first be defined to represent the derivative of the initial function you're solving for. This function is then given a value in the initial conditions.
Additionally, to denote a second derivative, the diff function gains a third argument equaling the independent variable to show that the first argument is being differentiated again. To clarify, the first argument is differentiated with respect to all subsequent arguments to get the output.
In this example, the equation y'' = sin(3*x) - y is solved for y with theinitial conditions y(0) = 1 and y'(0) = 1. Finally, we apply simplify command to make the end result look nicer.

y(x) =
cos(x) + sin(x)/2 - (cos(x)^2*sin(x))/2

Finally, to solve third order ODEs, the same logic is used. A third symbolic function representing the second derivative of the solution
function must be introduced to properly define the initial conditions. In our the last example, the differential equation u''' = u is solved for u with the initial conditions u(0) = 1, u'(0) = -1, and u''(0) = 0. Notice that the diff function can also be written with the dependent variable.

u(x) =
exp(-x/2)*cos((3^(1/2)*x)/2) - (3^(1/2)*exp(-x/2)*sin((3^(1/2)*x)/2))/3

Now to practice, try solving the differential equation y''*x^2 + x*y' = y with the initial conditions y(0)=0 and y'(0)=1. The answer should be y = x

Example. We consider a simple RLC circuit in series that is modelled by the differential equation

$\frac{{\text d}^2 i}{{\text d} t^2} + 2\alpha \, \frac{{\text d} i}{{\text d} t} + \omega^2 i(t) =0,$ where $$i(t)$$ represents the current traveling through the circuit as a function of time, t. The coefficients $$\alpha$$ and $$\omega^2$$ incorporate the constants used in a resistor, inductior, and a capacitor. Additionally, $$\alpha$$ and $$\omega^2$$ represent two distinct types of angular frequency, the fotmer is known as the neper frequency and the latter is known as the angular resonance frequency. In other words, a higher $$\alpha$$ value will increase resistance in the circuit and a smaller value of $$\alpha$$ will have the opposite effect on the circuit. Therefore, the harmonic oscillations of the current through the circuit decay with increase values of $$\alpha$$ and approach zero.

First we define simplecircuit function

Then we solve the equation

Sours: https://www.cfm.brown.edu/people/dobrush/am33/Matlab/ch4/part4.html
home
PDF (letter size)
PDF (legal size)

### Using Matlab ode45 to solve diﬀerential equations

Nasser M. Abbasi

May 30, Compiled on May 20,  at pm

### Contents

2 description
3 Simulation
4 Using ode45 with piecewise function
5 Listing of source code

1. first_order_ode.m.txt
2. second_order_ode.m.txt
3. engr80_august_14__2.m.txt
4. engr80_august_14_m.txt
5. ode45_with_piecwise.m.txt

### 2 description

This shows how to use Matlab to solve standard engineering problems which involves solving a standard second order ODE. (constant coeﬃcients with initial conditions and nonhomogeneous).

A numerical ODE solver is used as the main tool to solve the ODE’s. The matlab function ode45 will be used. The important thing to remember is that ode45 can only solve a ﬁrst order ODE. Therefore to solve a higher order ODE, the ODE has to be ﬁrst converted to a set of ﬁrst order ODE’s. This is possible since an $$n$$ order ODE can be converted to a set of $$n$$ ﬁrst order ODE’s.

Gives a ﬁrst order ODE $\frac{dx}{dt}=f(x,t)$

An example of the above is $$\frac{dx}{dt}=3 e^{-t}$$ with an initial condition $$x(0)=0$$. Here is the result of solving this ODE in Matlab. Source code is first_order_ode.m.txt

To solve a second order ODE, using this as an example. $\frac{d^{2} x}{dt^{2}}+5 \frac{dx}{dt}- 4 x(t) = \sin (10\ t)$

Since ode45 can only solve a ﬁrst order ode, the above has to be converted to two ﬁrst order ODE’s as follows. Introduce 2 new state variables $$x_{1},x_{2}$$ and carry the following derivation

$\left . \begin{array} [c]{c}x_{1}=x\\ x_{2}=x^{\prime }\end{array} \right \} \overset{\text{take derivative}}{\rightarrow }\left . \begin{array} [c]{c}x_{1}^{\prime }=x^{\prime }\\ x_{2}^{\prime }=x^{\prime \prime }\end{array} \right \} \overset{\text{do replacement}}{\rightarrow }\left . \begin{array} [c]{l}x_{1}^{\prime }=x_{2}\\ x_{2}^{\prime }=-5x^{\prime }+4x+\sin \left ( 10t\right ) \end{array} \right \} \rightarrow \left . \begin{array} [c]{l}x_{1}^{\prime }=x_{2}\\ x_{2}^{\prime }=-5x_{2}+4x_{1}+\sin \left ( 10t\right ) \end{array} \right \}$

The above gives 2 new ﬁrst order ODE’s. These are

$\begin{array} [c]{l}x_{1}^{\prime }=x_{2}\\ x_{2}^{\prime }=-5x_{2}+4x_{1}+\sin \left ( 10t\right ) \end{array}$

Now ode45 can be used to solve the above in the same way as was done with the ﬁrst example. The only diﬀerence is that now a vector is used instead of a scalar.

This is the result of solving this in Matlab. The source code is second_order_ode.m.txt

### 3 Simulation

Now ode45 is used to perform simulation by showing the solution as it changes in time.

Given a single degree of freedom system. This represents any engineering system whose response can move in only one direction. A typical SDOF (single degree of freedom) is the following mass/spring/damper system.

The ﬁrst step is to obtain the equation of motion, which will be the second order ODE. Drawing the free body diagram and from Newton’s second laws the equation of motion is found to be

$m x'' + c x' + k x = f( \omega _f t)$

In the above, $$\omega _f$$ is the forcing frequency of the force on the system in rad/sec.

The response of the system (the solution of the system, or $$x(t)$$) is simulated for diﬀerent parameters.

For example, the damping $$c$$ can be changed, or the spring constant (the spring stiﬀness) to see how $$x(t)$$ changes. The forcing function frequency $$\omega _f$$ can also be changed.

The following deﬁnitions are used in the Matlab code.

Natural frequency of the system $$\omega =\sqrt{\frac{k}{m}-\left (\frac{c}{2m}\right )^2}$$

Damping ratio $$\varsigma =\frac{c}{c_r}$$ where $$c$$ is the damping coeﬃcient and $$c_r$$ is the critical damping. $c_{r} = 2 \sqrt{k m}$

When $$c>c_{r}$$ the system is called over damped. When $$c<c_r$$ the system is called underdamped

The following example runs a simulation showing the eﬀect of changing the damping when the forcing function is a step function. The response to a step function is a standard method used to analyze systems.

### 4 Using ode45 with piecewise function

ode45 can be used with piecewise function deﬁned for the RHS. For example, given $$x''(t)-x(t)=c$$ where $$c=1$$ for $$0<=t<1$$ and $$c=20$$ for $$1<=t<2$$ and $$c=3$$ for $$2<=t<=3$$, the following code example shows one way to implement the above.

ode45_with_piecwise.m.txt

### 5 Listing of source code

#### ode45_with_piecwise.m

Sours: https://wwworg/my_notes/matlab_ODE/index.htm

## ODE45 - Solving a system of second order differential equations

syms A1(t) A2(t) S(t) x(t) T Cl(t) Cr(t) Y

A11 = diff(A1,1)

A12 = diff(A1,2)

A21 = diff(A2,1)

A22 = diff(A2,2)

S2 = diff(S,2)

X2 = diff(x,2)

Eq1 = X2 == (*A1)- (((3*)+()+(21*)))/(((21+))*A12 -(((3*)/(21+3))*A22));

Eq2 = Cl == ((((*)*(21+3))+(((*(^2))+)/))*X2)+(((*((*(^2))+))/2*)*S2)+((***(21+3))*A11)+(*3***A22);

Eq3 = Cr == ((((*)*(21+3))+(((*(^2))+)/))*X2)-(((*((*(^2))+))/2*)*S2)+((***(21+3))*A11)+(*3***A22);

Eq4 = S2 == (Cl-((((*)*(21+3))+(((*(^2))+)/))*X2)-((***(21+3))*A11)-(*3***A22))/(((*((*(^2))+))/2*));

Eq5 = X2 == (*A2) - (2**A12) - ((((3*()^2)+)/(3*))*A22);

Eq6 = A12 == (*A1)-(((2**(21+3))/((4*3*(^2))+(21*(^2))))*X2)-(((2*3**)/((4*3*(^2))+(21*(^2))))*A22);

Eq7 = A12 == (*A2)-((1/(2*))*X2)-((((3*(^2))+)/(2*3**))*A22);

Eq8 = A22 == (*A1)-(((2**(21+3))/(2*3**))*X2)-((((*3*(^2))+(21*(^2)))/(2*3**))*A12);

Eq9 = A22 == (*A1)-(((3*)/((3*(^2))+))*X2)-((((*3*(^2))+(21*(^2)))/(2*3**))*A12);

Eq10 = S2 == (Cr-((((*)*(21+3))+(((*(^2))+)/))*X2)-((***(21+3))*A11)-(*3***A22))/-((((2*)/(*((*(^2))+)))));

[VF,Sbs] = odeToVectorField(Eq1,Eq2,Eq3,Eq4,Eq5,Eq5,Eq6,Eq7,Eq8,Eq9,Eq10)

coupled_vanderpol = matlabFunction(VF, 'Vars',{T,Y})

%x0 x. A1 A1. A2 A2. S S.

t0 = [,0,0,,0,0,0];

tspan = [0,10];

[t,v_z] = ode45(@(T,Y)coupled_vanderpol(T,Y),tspan,t0);

figure

plot(t, v_z)

grid

how to scape this error ? if anyone can tell

## Solving a 2nd order ODE with

### Initial value problem

We consider an initial value problem for a 2nd order ODE:

and we want to find the solution y(t) for t in [0,4].

We first have to rewrite this as a 1st order system: Let and , then we obtain

Now we can define a vector valued function f(t,y) and an initial vector y0.

f = @(t,y) [y(2); t+y(2)-3*y(1)]; t0 = 0; y0 = [1;-2];

### Solve IVP with

returns a vector of t values and an array: each row of contains the values for and .

Note that is the approximation for and is the approximation for .

gives the values for , gives the values for . Therefore we can plot the function with

T = 4; [ts,ys] = ode45(f,[t0,T],y0); fprintf('y(T) = %g, y''(T) = %g\n',ys(end,1),ys(end,2)) disp(' t y1 y2') disp([ts,ys]) plot(ts,ys(:,1),'b') title('Solution y(t) of IVP') xlabel('t'); grid ony(T) = , y'(T) = t y1 y2 0 1 -2 4
Sours: http://www.math.umd.edu/~petersd//html/ode45_demo2.html

## Order ode45 matlab second

Lot, but I dont belong to them in any way. You could make a good career in our company - come, in a year and a half you will be in charge. Of the department, three thousand bucks clean, I will support you. " And she was completely amazed to hear my arguments about the affairs of science, service to the state that raised and taught me, vocation.

And other lofty things.

Numerical Solution of Systems or Higher Order ODEs with ode45 in MATLAB

She fucks excellently - Grisha knows how to choose, test. He doesn't hang noodles on his ears, but he answers the questions asked. Polite and short. Letting know that sometimes it's better to suck than talk. It sucks skillfully and in a variety of ways, then my weapon sticks into my throat, then it sticks out my cheeks, my hands are not idle either.

### You will also like:

Swallow it. Together with him I fall into the abyss of pleasure. My head is spinning in this fall. Everything is blurring. I fly, I fall-ada-yu.

1078 1079 1080 1081 1082