Differential and Integration
Differential and Integration
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
s=
>> pretty(s)
(a x2 + b x + c)1/2
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.
ezplot(s)
Examples:-
f= 1000
400
200
-6 -4 -2 0 2 4 6
x
>> r= ezplot('x^2/9+y^2/4=1',[-3,3,-2,2])
1.5
r=
174.0082 1
0
y
-0.5
-1
-1.5
-2
-3 -2 -1 0 1 2 3
x
a. integration
Where:-
var:- the integration of expression with (several symbolic variables ) the integration is
carried out with respect to the variable var.
>> syms x y
>> y=atan(x)
y=
atan(x)
>> int(y)
ans =
>> int(y,x,2,7)
ans =
>> 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);
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:- ( ) ( )
>> syms x y t
>> s=exp(x^4);
>> 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);
>> diff(R)
ans =
10*y*cos(3*t)
>> diff(R,t)
ans =
-15*y^2*sin(3*t)
>> diff(s,2)
ans =
12*x^2*exp(x^4) + 16*x^6*exp(x^4)
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
k=
-2 the solution has two values. They are assigned to k, which is a column
>> 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 =
>> % the equation T=0 is solved for the variable x, which is the default variables.
>> M=solve(T,a)
M=
-(5*b*x + 20)/x^2
( )
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:-
56
MATLAB ENGINEERING // SECOND YEAR//CHEMICAL ENG. DEPT.
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: ( )
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
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.
b/2
By Pythagorean theorem
as:-
h/2
( ) ( ) ( )
h
By solving eq.(1): ( ) ( ) )⇒ ⇒ ⇒ √
>> 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 =
hs =
3^(1/2)*R
-3^(1/2)*R
54