0% found this document useful (0 votes)
105 views12 pages

Differential and Integration

The document discusses symbolic mathematics in MATLAB including: 1. Creating symbolic variables and expressions using syms and sym commands. 2. Performing operations on symbolic expressions such as integration, differentiation, solving equations, and plotting. 3. Key functions for manipulating symbolic expressions include sym, syms, int, diff, solve, ezplot, findsym, and double. 4. Integration is performed using int, differentiation using diff, and solving equations using solve. Plotting symbolic expressions uses ezplot.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views12 pages

Differential and Integration

The document discusses symbolic mathematics in MATLAB including: 1. Creating symbolic variables and expressions using syms and sym commands. 2. Performing operations on symbolic expressions such as integration, differentiation, solving equations, and plotting. 3. Key functions for manipulating symbolic expressions include sym, syms, int, diff, solve, ezplot, findsym, and double. 4. Integration is performed using int, differentiation using diff, and solving equations using solve. Plotting symbolic expressions uses ezplot.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

Differential and Integration


The differential and Integration is a part of a symbolic mathematics; so before explain
them must know some about symbolic mathematics.

Symbolic computation in MATLAB

In addition to numerical computation, MATLAB can also perform symbolic


computations. However, since, by default, variables are floating point, you must
explicitly indicate that a variable is intended to be symbolic. One way to do this is
using the syms command, which tells MATLAB that one or more variables are
symbolic.
1- Symbolic variables:- variables that don't have specific numerical values when the
operation is executed.
 Creating symbolic variables:- simple symbolic variables can be created in :-

Variable name=syms('string')
String:-a single litter or a combination of several letters (no spaces).

>> a= sym('a')
a=
a
-to create several symbolic variables in one command:-
>> syms y z d
The variables created by the syms command are not
>> y displayed automatically. Typing the name of the variables
y= shows that the variable was ceated.

y
>> syms a b; 2*a*b
ans =
2*a*b
-string can also be numbers
>> c=sym(2)
c=
2
You can do symbolic computations:

45
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

>> a=sqrt(c)
a=
2^(1/2)

Notice the difference between the above result and the following:
>> a=sqrt(2)
a=
1.4142

Consider the following two commands:


>> sin(sym(pi))
ans =
0
>> sin(pi)
ans =
1.2246e-016

2- Creating symbolic expressions:-

Symbolic expression is mathematical expression written in term of symbolic variables.

The form creating a symbolic expression is:-

Expression name = Mathematical Expression

>> syms a b c x y; f=a*x^2+b*x+c


f=
a*x^2 + b*x + c
>> g=2*a/3+4*a/7-6.5*x+x/3+4*5/3-1.5
g=
(26*a)/21 - (37*x)/6 + 31/6
*Know notice the difference between the following:-
Symbolic variable Numerical variables
>> a=sym(3);b=sym(5); >> c=3;d=5;
>> e=b/a+sqrt(2) >> f=d/c+sqrt(2)
e= f=
2^(1/2) + 5/3 3.0809

The pretty command:- display a symbolic expression in a format resembling the


mathematical format in which expressions are generally typed.Pretty(s)
44
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

>> syms a b c x; s=sqrt(a*x^2+b*x+c)

s=

(a*x^2 + b*x + c)^(1/2)

>> pretty(s)

(a x2 + b x + c)1/2

The find sym command and the default symbolic variables

The findsym command can be used to find which symbolic variables are presented in
an existing symbolic expression.

findsym(s) find(s,n)
Display the name of all the symbolic Display n symbolic variables which are in
variables (separated by comma) that are in expression (s) in the default order.
the expression (s) in alphabetical order.
>> syms x h w y d t
>> s=h*x^2+d*y^2+t*w^2
s=
t*w^2 + h*x^2 + d*y^2
>> findsym(s) >> findsym(s,5)
ans = ans =
d,h,t,w,x,y x,y,w,t,h
>> findsym(s,1)
ans =
x

The double command: - This command can be used to convert a symbolic expression
(objects)s to a numerical form.
>> h=10/3; k=sym(5);m=sym(7); p=k/(m+h)
p=
15/31
>> pn=double(p)

pn =
0.4839

45
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

Plotting symbolic expressions

To plot symbolic expressions , the ezplot command is used as the following:-

ezplot(s)

ezplot(s,[min,max]) min, Xmin:- domain of independent variable

ezplot(s,[Xmin,Xmax,Ymin,Ymax]) Ymin, Ymax:- domain of dependent variable

Examples:-

Plot the function f(x) = x4 + 2x3 − 7x2.


x4 + 2 x3 - 7 x2
>> syms f x
>> f=x^4+2*x^3-7*x^2 1200

f= 1000

x^4 + 2*x^3 - 7*x^2 800

>> ezplot(f) 600

400

200

-6 -4 -2 0 2 4 6
x

Plot y versus x given the relation:-


>> syms x y 2
x 2/9+y 2/4=1

>> r= ezplot('x^2/9+y^2/4=1',[-3,3,-2,2])
1.5
r=
174.0082 1

>> set(r,'color','black') 0.5

0
y

-0.5

-1

-1.5

-2
-3 -2 -1 0 1 2 3
x

Summary for symbolic commands

Functions for Manipulating Symbolic Expressions


sym Creates a symbolic variable.
syms Creates one or more symbolic variables.
46
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

ezplot Generates a plot of a symbolic expression.


findsym Finds the symbolic variables in a symbolic expression.
double Converts an expression to numeric form.
factor Factors an expression.
pretty Displays an expression in a form that resembles typeset mathematics.

a. integration

The command of integration is:- ( ) ( )

Where:-

S:-can either be a name of a previously created symbolic expression, or an expression


can be typed in for s.

var:- the integration of expression with (several symbolic variables ) the integration is
carried out with respect to the variable var.

 for definite integration the form of the command is:-


( ) ( ) where:-
a,b are the limits of integration, the limits can be numbers or symbolic variables.

>> syms x y

>> y=atan(x)

y=

atan(x)

>> int(y)

ans =

x*atan(x) - log(x^2 + 1)/2

>> int(y,x,2,7)

ans =

2*pi + atan(22049/1457) + log((5^(1/2)*50^(1/2))/50)

>> syms x y t

>> s=2*cos(x)-6*x;

>> int(s)
47
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

ans =

2*sin(x) - 3*x^2

>> int(x*sin(x))

ans =

sin(x) - x*cos(x)

>> R=5*y^2*cos(4*t);

>> int(R) Matlab integrate R with respect to y (default symbolic variable)

ans =

(5*y^3*cos(4*t))/3

>> int(R,t)

ans =

(5*y^2*sin(4*t))/4

b. Differentiation

Symbolic differentiation can be carried out using diff command. The form of the
command is:- ( ) ( )

Where:- s and var the same of integration command.

>> syms x y t

>> s=exp(x^4);

>> % use the diff (s) command to differentiate s.

>> diff(s)

ans =

4*x^3*exp(x^4)

>> diff((1-4*x)^3)

ans =

-12*(4*x - 1)^2

48
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

>> R=5*y^2*cos(3*t);

>> % matlab differentiate R with respect to y (default symbolic variable)

>> diff(R)

ans =

10*y*cos(3*t)

>> diff(R,t)

ans =

-15*y^2*sin(3*t)

>> %matlab obtain the second derivative of s

>> diff(s,2)

ans =

12*x^2*exp(x^4) + 16*x^6*exp(x^4)

Solving Algebraic equation

a. Solving a single equation

The solve function is used for solving algebraic equations. In its simplest form, the
solve command is:- ( ) ( )
>> syms a b x y z

>> h=solve(exp(2*z)-5) when the equation does not contain = sign; Matlab solve

h= equation eq=0

log(5)/2

>> %create the symbolic expression: x2-x-6

>> s=x^2-x-6; k=solve(s)

k=

-2 the solution has two values. They are assigned to k, which is a column

3 vector with symbolic objects.

>> solve('cos(2*y)+3*sin(y)=2')
56
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

ans =

pi/2

pi/6

(5*pi)/6

>> T=a*x^2+5*b*x+20;

>> solve(T)

ans =

-(5*b + 5^(1/2)*(5*b^2 - 16*a)^(1/2))/(2*a)

-(5*b - 5^(1/2)*(5*b^2 - 16*a)^(1/2))/(2*a)

>> % the equation T=0 is solved for the variable x, which is the default variables.

>> % use the solve(eq,var) command to solve T=0

>> M=solve(T,a)

M=

-(5*b*x + 20)/x^2

>> % M solved for the variable a.

b. Solving a system of equations:-The solve command can also be used for


solving a system of equations. ( ) or

( )

The rules of the above commands are the same rules for the solving a single eq.

The output forms, (the solution of the system can have two different forms:-

Call array Structure array


Is an array in which each element can be Is an array in which the elements (called
an array. field) are addressed by textual field
The solve command in call array:- designators. The field of structure can be
[ ] ( ) arrays in different sizes and types.
>> syms x y t ( )
>> s=10*x+12*y+16*t;
AN:- name of structure
>> %use the solve command to solve the system
>> syms x y t
10x+12y+16t=0 & 5x-y=13t
>> s=10*x+12*y+16*t;
>> % output in call array with two cells named xt
56
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

and yt >> AN=solve(s,'5*x-y=13*t')


>> [xt yt]=solve(s,'5*x-y=13*t') AN =
xt =
x: [1x1 sym]
2*t
yt = y: [1x1 sym]
-3*t >> %type the address of the field x
The above example solved for x and y in terms of >> AN.x
t, since x and y are the first two variables in ans =
default order. 2*t (the content of the field: the solution
Hand calculating for x is displayed)
( )
>> AN.y
( )]
____________________________ ans =
( ) -3*t (the content of the field: the solution
( ) for y is displayed)
_____________________________
⇒ ( )
( )
⇒ ⇒
The system can be solved for different variables.
>> [xt yt]=solve(s,'5*x-y=13*t')
xt =
2*t
yt =
-3*t
>> [tx yx]=solve(s,'5*x-y=13*t',y,t)
tx =
x/2
yx =
-(3*x)/2
Example:- the equation of a circle in x-y plane with a radius R and it's center at point
(2,4) is given by:( ) ( ) the equation of a line in the plane is given
by: . Determine the coordinates of the points (as a function of R) where the
line intersects the circle. >> syms x y R
>> [xc yc]=solve('(x-2)^2+ (y-4)^2= >>COORD=solve('(x-2)^2+(y-4)^2= R^2','y=x/2+1')
R^2','y=x/2+1') COORD =
xc = x: [2x1 sym]
((4*R^2)/5 - 64/25)^(1/2) + 14/5 y: [2x1 sym]
14/5 - ((4*R^2)/5 - 64/25)^(1/2) >> COORD.x
yc = ans =
((4*R^2)/5 - 64/25)^(1/2)/2 + 12/5 ((4*R^2)/5 - 64/25)^(1/2) + 14/5
12/5 - ((4*R^2)/5 - 64/25)^(1/2)/2 14/5 - ((4*R^2)/5 - 64/25)^(1/2)
>> COORD.y
ans =
((4*R^2)/5 - 64/25)^(1/2)/2 + 12/5
12/5 - ((4*R^2)/5 - 64/25)^(1/2)/2

56
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

Solving an ordinary differential equation

An ordinary differential equation (ODE) can be solved symbolically with the dsolve
command. The command can be used to solve a single equation or a system of
equations.

A first order ODE is an equation used that contains the derivative of dependent
variable. If (t) is the independent variable and (y) is dependent variable, the equation
can be written in the form: ( )

For second order (ODE) ( )

The solution can be general (contains constants) or particular (the constants are
determined to have specific numerical values such that the solution satisfies specific
initial or boundary conditions.

dsolve command

General solution Particular solution


( ) ( ) First order ODE
 Any letter (lower or upper ( )
case),except (D) can be used for Higher order ODE
the dependent variable. ( )
 The default independent variable is  If the number of conditions is less
assumed in matlab (t). than the order of the equation, matlab
 D denoted differentiation. returns a solution that includes
 DY denoted constants of integration (c1,c2,and so
on).
 D2 denoted second derivative.  The argument 'var' is optional, and is
 D3 denoted third derivative and so used to define the independent
on. variable in the equation. If none is
 The variable in ODE is typed in entered, the default is (t).
the dsolve command to not have to Examples:-
be previously created symbolic
( )
variables.
>> dsolve('DY=4*t+2*Y') >> dsolve('Dy+4*y=60','y(0)=5')
ans = ans =
C3*exp(2*t) - 2*t – 1 15 - 10/exp(4*t)
>>dsolve('D2y-
2) 2*Dy+2*y=0','y(0)=1','Dy(0)=0')
>> dsolve('D2x+2*Dx+x'=0') ans =
ans = exp(t)*cos(t) - exp(t)*sin(t)
C8/exp(t) + (C9*t)/exp(t) >> %the answer can be simplified by
56
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

factor command
>> % the indepent variable is (t) default >> factor(ans)
>> %Matlab solve the eq.: ans =
exp(t)*(cos(t) - sin(t))
>> dsolve('Ds=a*x^2')
ans =
a*t*x^2 + C11
>> %Matlab solve the eq.:
>> dsolve('Ds=a*x^2','x')
ans =
(a*x^3)/3 + C13
>> %Matlab solve the eq.:

>> dsolve('Ds=a*x^2','a')
ans =
(a^2*x^2)/2 + C15
Example:-The bending resistance of rectangular beam of width (b) and height (h) is
proportional to beam's moment of inertia (I) defined by A rectangular beam
is cut of a cylindrical log of radius (R).Determine (b) and (h) (as a function of (R) such
that the beam will have maximum I.

Solution:- the problem is solved by the following these steps:-

1- Write an equation that relates (R),(h) and (b).


2- Derive an expression for (I) in terms of (h).
3- Take the derivative of (I) with respect to (h).
4- Set the derivative equal to zero and solve for (h).

b/2
By Pythagorean theorem
as:-
h/2

( ) ( ) ( )
h

By solving eq.(1): ( ) ( ) )⇒ ⇒ ⇒ √

The above steps are done using Matlab.

>> syms b h R
55
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.

>>b=sqrt(4*(R^2)-(h^2));

>>I=b*(h^3)/12

I=

(h^3*(4*R^2 - h^2)^(1/2))/12

>> ID= diff(I,h)

ID =

(h^2*(4*R^2 - h^2)^(1/2))/4 - h^4/(12*(4*R^2 - h^2)^(1/2))

>> hs= solve(ID,h)

hs =

3^(1/2)*R

-3^(1/2)*R

54

You might also like