# Solving Differential Equations In MatLab® Effectively

In most engineering problems and situations, the governing mathematical facts are differential equations. In most cases, solving differential equations can be uneasy. So, Matlab® provides very useful tools to solve these differential equations.

YOU CAN LEARN MatLab® IN MECHANICAL BASE; Click And Start To Learn MatLab®!

You can solve many differential equations in Matlab® by using the ‘dsolve()’ command. You can solve differential equation systems also.

Here, we explain how to solve differential equations in Matlab® with the ‘dsolve()’ command with various coding examples below. Check these coding examples that are executed in the Matlab® command window, to understand the syntax and the use of the ‘dsolve()’ command.

## How To Use ‘dsolve()’ Command In MatLab®?

```
>> s = dsolve('Da+t+a=sin(t)+t')
s =
C2*exp(-t) - (2^(1/2)*cos(t + pi/4))/2
>>
```

In the example above, you can see the most basic use of the ‘dsolve()’ command in Matlab®. In this one line of code, we inserted our differential equation inside the ‘dsolve()’ command. We used quotes also.

There are two variables that one of them is ‘a’ and the other one is ‘t’. Actually, ‘a’ is the function that we want to find, and ‘t’ is the variable of this function.

### Defining Differential Expressions In ‘dsolve()’ Command

This differential expression inside this equation is define with ‘D’ which means the first derivative of ‘a’, with respect to ‘t’. You can define the higher degrees of derivatives with ‘D2’ and ‘D3’.

If you hit the ‘Enter’ key to execute code, you will see the answer in the command line like above. As you see in the example above, the answer is the general solution of the differential equation. You can find special solutions by applying boundary conditions.

### Defining Boundary Conditions For A Differential Equation In MatLab®

```
>> s = dsolve('D3a=cos(2*t)','D2a(0)=1','Da(0)=0','a(0)=-1')
s =
t/4 - sin(2*t)/8 + t^2/2 - 1
>>
```

In the example above, we defined a differential equation that includes the third degree of differential. So, if we define the second degree, first degree, and normal boundary conditions, we can find a special solution.

What we did in the example above, we defined these boundary conditions respectively, after the definition of the differential equation inside the ‘dsolve()’ command.

### Solving Differential Equation Systems In Matlab®

```
>> a=dsolve('3*Dx+sin(2*t)=0','Dy=t+1','x(0)=1','y(0)=2')
a =
struct with fields:
y: [1×1 sym]
x: [1×1 sym]
>> a.y
ans =
t^2/2 + t + 2
>> a.x
ans =
cos(2*t)/6 + 5/6
>>
```

In some cases, you can have differential equations systems that include more than one variable and more than one differentiation.

In the example above, we defined two differential equations for each ‘x’ and ‘y’ variable, with respect to ‘t’. And we defined boundary conditions for these two systems.

If you execute the code above, you will see the answers in matrix form in the command window. To see the results inside these vectors separately, type ‘a.y’ and ‘a.x’ to see the results and solutions for each variable.

## Conclusion

Solving ordinary differential equations in Matlab® with the ‘dsolve()’ command is very simple like above.

Do not forget to leave your comments and questions below about the use of the ‘dsolve()’ command in Matlab® below.

If you want further coding examples about the ‘dsolve()’ command in Matlab®, inform us in the comments.

This article is prepared for completely educative and informative purposes. Images used courtesy of Matlab®

Your precious feedbacks are very important to us.