# How to solve differential equations on Matlab and Octave

To solve a differential equation on Matlab and Octave use the instruction **dsolve()**

**dsolve(eq, cond)**

The first parameter (eq) is the differential equation.

The second parameter (cond) is the initial condition.

**Note**. In Octave the dsolve() function requires the installation and loading of the Symbolic module.

## Examples

**Example 1**

Define a y (x) function as a symbol using the command **syms**

syms y(x)

Define the differential equation y '' - y = 0 of the second order in a variable

Write the derivatives of the function y (x) using the command **diff(f,n)**

eq = diff(y,x,2) - diff(y,x,1) == 0

Solve the differential equation using the command **dsolve(eq)**

S = dsolve(eq)

The general solution of the differential equation is

y(x)=c1 + c2e^x

The result is assigned to the variable S.

**Example 2**

Define the function symbol via the command **syms**

syms y(x)

Define the differential equation

eq = diff(y,x,2) - diff(y,x,1) == 0

Define the initial condition

cond = y(0) == 3

Solve the differential equation with respect to the initial condition using the command **dsolve(eq,cond)**

S = dsolve(eq,cond)

The solution of the differential equation is

y(x)=3e^x

The result is assigned to the variable S.

**Report us an error or send a suggestion to improve this page**