You are on page 1of 3

Pharos University in Alexandria Fall 2022-2023

Faculty of Engineering
Department of Basic Sciences

EB103 (Engineering Mathematics 3)


MATLAB 3_Level 3 _Session 6_ 1st Order Differential Equations

 Objective:
- Revision for the differentiation and the partial derivatives.
- Solution of ordinary differential equations (1st order)

An ordinary differential equation (ODE) contains derivatives of dependent variables with


respect to the only independent variable. If y is a dependent variable and x is an independent
variable, the solution of an ODE is an expression y(x). The order of the derivative of a
dependent variable defines the order of an ODE.

When solving an ODE, use symbolic functions to specify dependent variables.


For example, syms y(x) creates the symbolic function y of x, and the variable x. Construct an
ODE by using diff to denote derivatives and == to create an equation. If an ODE has initial
or boundary conditions, specify them as additional equations. Then use dsolve to solve an
ODE. If you do not specify initial or boundary conditions, the solutions will contain
integration constants, such as C1, C2, and so on.

Syntax (1):

dsolve(eqn): solves the ordinary differential equation eqn. Here eqn is a symbolic equation
containing diff to indicate derivatives. When representing eqn as a symbolic equation, you must create a
symbolic function, for example y(x) as mentioned before. Here x is an independent variable for which
you solve an ordinary differential equation.

Note: Because D indicates differentiation, the names of symbolic variables must not contain D.

Example (1):
Solve the following Differential Equations:
dy
a) = ex - y
dx
dx x
b) ln x =
dy y
dy
c) x 4 - y2 + y 2 x2 + 3 =0
dx
d) x d y + ( y + x 3 ) d x = 0
Page 1 of 3
Pharos University in Alexandria Fall 2022-2023
Faculty of Engineering
Department of Basic Sciences

Note: For this syntax, you can enter the differential equation to be solved in the form of
“dy/dx == equation or dx/dy= = equation” if needed.

Solution:

MATLAB commands Output In Command Window


syms y(x)
dsolve(diff(y)== exp(x-y)) ans = log(C5 + exp(x))
syms x(y) ans =
dsolve(diff(x)== x/(y*log(x))) exp(2^(1/2)*(C7 + log(y))^(1/2))
exp(-2^(1/2)*(C7 + log(y))^(1/2))
or or
ans =
dsolve(log(x)*diff(x)==x/y) exp(2^(1/2)*(C18 + log(y))^(1/2))
exp(-2^(1/2)*(C18 + log(y))^(1/2))
syms y(x) ans =
dsolve(diff(y)== -x*sqrt(4- -(4*2^(1/2)*C1*(x^2 + 3/2)^(1/2) -
y^2)/(y*sqrt(2*x^2+3))) 2*x^2 - 4*C1^2 + 13)^(1/2)/2
(4*2^(1/2)*C1*(x^2 + 3/2)^(1/2) -
2*x^2 - 4*C1^2 + 13)^(1/2)/2
2
-2
syms y(x)
dsolve(diff(y)==-(y+x^3)/x) ans = C12/x - x^3/4

Syntax (2):
dsolve(eqn,cond): Solves the ordinary differential equation eqn with the initial or boundary
condition cond. Define initial and boundary conditions using symbolic equations, such as y(a) == b ,
here a and b are constants.

Example (2):
Solve the following Differential Equations:
a) dy/dx=a/ y  y , y(a)=1

b) dy/dx=a/ ( y 2  1) , y(a)= 2
c) dy/dx=x*y , y(0)=2
d) dy/dx+4 y(x)= e  x , y(0) = 1

Page 2 of 3
Pharos University in Alexandria Fall 2022-2023
Faculty of Engineering
Department of Basic Sciences

Note: Specifying the initial condition lets you eliminate arbitrary constants, such as C1, C2,...
Solution:

MATLAB commands Output In Command Window


syms a y(x) ans =
dsolve(diff(y) == a/sqrt(y) + y, y(a) == 1) (exp((3*x)/2 - (3*a)/2 + log(a + 1)) - a)^(2/3)
syms a y(x) ans =
dsolve(diff(y) == a/(y^2 + 1),y(a)==2) ((3*a*x)/2 - (3*a^2)/2 + (((3*a*x)/2 - (3*a^2)/2
+ 7)^2 + 1)^(1/2) + 7)^(1/3) - 1/((3*a*x)/2 -
(3*a^2)/2 + ((- (3*a^2)/2 + (3*x*a)/2 + 7)^2 +
1)^(1/2) + 7)^(1/3)
syms y(x) ans =
y(x) = dsolve(diff(y,x) == x*y, y(0) == 2) 2*exp(x^2/2)
syms y(x) ans =
dsolve(diff(y) + 4*y == exp(-x), y(0) == 1) exp(-x)/3 + (2*exp(-4*x))/3

* (Classwork Activity): Write an M-file that accepts from the user the differential equation to be solved
and displays the result and try the program on any problem from example 1.
© By Engineering Mathematics Group

Page 3 of 3

You might also like