# Documentation

## Solve a System of Differential Equations

`dsolve` can handle several ordinary differential equations in several variables, with or without initial conditions.

### Solve System of Differential Equations

Solve the system of linear first-order differential equations

`$\begin{array}{l}\frac{df}{dt}=3f+4g,\\ \frac{dg}{dt}=-4f+3g.\end{array}$`

First, create the symbolic functions `f(t)` and `g(t)`, and then declare the equations.

```syms f(t) g(t) eqn1 = diff(f) == 3*f + 4*g; eqn2 = diff(g) == -4*f + 3*g;```

Solve the system by using `dsolve`. The `dsolve` function returns the solutions as elements of the structure `S`.

`S = dsolve(eqn1, eqn2)`
```S = g: [1x1 sym] f: [1x1 sym]```

To return `f(t)` and `g(t)`, access the elements of `S`.

```fSol(t) = S.f gSol(t) = S.g```
```fSol(t) = C2*cos(4*t)*exp(3*t) + C1*sin(4*t)*exp(3*t) gSol(t) = C1*cos(4*t)*exp(3*t) - C2*sin(4*t)*exp(3*t)```

Alternatively, store `f(t)` and `g(t)` directly by providing the output arguments as a vector.

`[fSol(t) gSol(t)] = dsolve(eqn1, eqn2)`
```fSol(t) = C2*cos(4*t)*exp(3*t) + C1*sin(4*t)*exp(3*t) gSol(t) = C1*cos(4*t)*exp(3*t) - C2*sin(4*t)*exp(3*t)```

Specifying initial conditions allows `dsolve` to find the values of constants.

Specify initial conditions `f(0) == 0` and ```g(0) == 1```, and solve the equations. `dsolve` replaces the constants with their values.

```c1 = f(0) == 0; c2 = g(0) == 1; [fSol(t) gSol(t)] = dsolve(eqn1, eqn2, c1, c2)```
```fSol(t) = sin(4*t)*exp(3*t) gSol(t) = cos(4*t)*exp(3*t)```

Visualize the solutions by using `fplot`.

```fplot(fSol) hold on fplot(gSol) grid on legend('fSol','gSol','Location','best') ```

### Solve Differential Equations in Matrix Form

You can solve differential equations in matrix form by using `dsolve`.

Consider the system of differential equations

`$\begin{array}{l}\frac{dx}{dt}=x+2y+1,\\ \frac{dy}{dt}=-x+y+t.\end{array}$`

The system can be represented in matrix form as

`$\left[\begin{array}{c}x\text{'}\\ y\text{'}\end{array}\right]=\left[\begin{array}{cc}1& 2\\ -1& 1\end{array}\right]\left[\begin{array}{c}x\\ y\end{array}\right]+\left[\begin{array}{c}1\\ t\end{array}\right].$`

Let

`$Y=\left[\begin{array}{c}x\\ y\end{array}\right],A=\left[\begin{array}{cc}1& 2\\ -1& 1\end{array}\right],B=\left[\begin{array}{c}1\\ t\end{array}\right].$`

The relation is now Y′ = AY + B.

Define these matrices and the matrix equation.

```syms x(t) y(t) A = [1 2; -1 1]; B = [1; t]; Y = [x; y]; eqn = diff(Y) == A*Y + B```
```eqn(t) = diff(x(t), t) == x(t) + 2*y(t) + 1 diff(y(t), t) == t - x(t) + y(t)```

Solve the matrix equation by using `dsolve`.

`[xSol(t) ySol(t)] = dsolve(eqn)`
```xSol(t) = 2^(1/2)*exp(t)*cos(2^(1/2)*t)*(C2 + (exp(-t)*(4*sin(2^(1/2)*t) +... 2^(1/2)*cos(2^(1/2)*t) + 6*t*sin(2^(1/2)*t) + 6*2^(1/2)*t*cos(2^(1/2)*t)))/18) +... 2^(1/2)*exp(t)*sin(2^(1/2)*t)*(C1 - (exp(-t)*(4*cos(2^(1/2)*t) -... 2^(1/2)*sin(2^(1/2)*t) + 6*t*cos(2^(1/2)*t) - 6*2^(1/2)*t*sin(2^(1/2)*t)))/18) ySol(t) = exp(t)*cos(2^(1/2)*t)*(C1 - (exp(-t)*(4*cos(2^(1/2)*t) -... 2^(1/2)*sin(2^(1/2)*t) + 6*t*cos(2^(1/2)*t) -... 6*2^(1/2)*t*sin(2^(1/2)*t)))/18) - exp(t)*sin(2^(1/2)*t)*(C2 +... (exp(-t)*(4*sin(2^(1/2)*t) + 2^(1/2)*cos(2^(1/2)*t) +... 6*t*sin(2^(1/2)*t) + 6*2^(1/2)*t*cos(2^(1/2)*t)))/18) ```

Simplify the solution by using `simplify`.

```xSol(t) = simplify(xSol(t)) ySol(t) = simplify(ySol(t))```
```xSol(t) = (2*t)/3 + 2^(1/2)*C2*exp(t)*cos(2^(1/2)*t) + 2^(1/2)*C1*exp(t)*sin(2^(1/2)*t) + 1/9 ySol(t) = C1*exp(t)*cos(2^(1/2)*t) - t/3 - C2*exp(t)*sin(2^(1/2)*t) - 2/9```

To find the value of constants, specify initial conditions. When specifying equations in matrix form, you must specify initial conditions in matrix form too. Otherwise, `dsolve` throws an error.

Specify initial conditions `f(0) == 2` and ```g(0) == -1``` in matrix form, and solve the equations. `dsolve` replaces the constants with their values.

```C = Y(0) == [2; -1]; [xSol(t) ySol(t)] = dsolve(eqn, C)```
```xSol(t) = (2*t)/3 + (17*exp(t)*cos(2^(1/2)*t))/9 - (7*2^(1/2)*exp(t)*sin(2^(1/2)*t))/9 + 1/9 ySol(t) = - t/3 - (7*exp(t)*cos(2^(1/2)*t))/9 - (17*2^(1/2)*exp(t)*sin(2^(1/2)*t))/18 - 2/9```

Visualize the solutions by using `fplot`.

```clf fplot(ySol) hold on fplot(xSol) grid on legend('ySol','xSol','Location','best') ```