Lecture Note 10 Fall 22 23
Lecture Note 10 Fall 22 23
This lecture note includes some updates performed by Asst. Prof. Dr. Neyre Tekbıyık Ersoy for
ENGI316 course.
Advanced Methods in Displaying Output Data
The disp function
• This function is often combined with the functions num2str (convert a number to a
string) and int2str (convert an integer to a string) to create messages to be displayed in
the Command Window.
• For example, the following MATLAB statements will display “The value of pi = 3.1416” in
the Command Window.
• The first statement creates a string array containing the message, and the second
statement displays the message.
fprintf(format,data)
The easiest way to produce two-dimensional character arrays is with the char function.
This function will automatically pad all strings to the length of the largest input string.
Function strvcat concatenates two or more strings vertically, automatically padding the
strings to make a valid two-dimensional array. This function produces the following result:
» c = strcmp(str1,str2)
c =
0
» c = strcmpi(str1,str2)
c =
1
» c = strncmp(str1,str3,2)
c =
1
Categorizing Characters Within a String
There are three functions for categorizing characters on a character-by-character
basis inside a string:
a = isletter(mystring)
a = isspace(mystring)
a = isstrprop(mystring,'digit')
a = isstrprop(mystring,'lower')
PowerPoint to accompany
Introduction to MATLAB
for Engineers, Third Edition
William J. Palm III
syms x;
Creating Multiple Variables
To create several symbolic variables use
the syms command as follows:
syms K T P0;
P = P0*exp(K*T)
E = sym('m*c^2')
Manipulating symbolic
expressions
expand()
factor()
collect()
simplify()
expand()
expand() is used to expand an expression
by expanding the products of factors in an
expression.
Expand the numerator of y:
numerator=
2*(x + 3)^2
expand(numerator)
expand(numerator
ans=
2*x^2 + 12*x + 18
factor()
The factor() command is used to factor an
expression into a product of terms.
factor(denominator)
ans=
[ x + 3, x + 3]
simplify()
The simplify() command uses the Maple
simplification algorithm to simplify each
part of an expression.
Enter the expression:
ans=
- a^2 + 3*a + 9
solve()
The solve() function sets an expression
equal to zero, then solves the equation
for its roots.
E1=x^2 - 9
solve(E1)
ans=
3
-3
Hands on
ans =
1/2/a*( -b + (b^2 - 4*a*c)^(1/2))
1/2/a*( -b - (b^2 - 4*a*c)^(1/2))
ans =
-(b*x + c)/x^2
x= y= z=
-2 5 -6
Note that the solve function produces symbolic output. You can
change the output to numerical values with the double() command.
Systems of equations
Systems of equations
Systems of equations
Substitution with subs()
The subs() command allows you to
substitute a symbol with another symbol or
assign a number to a variable.
syms a b c x y;
quadratic = a*x^2 + b*x + c;
yquadratic = subs(quadratic,x,y)
yquadratic =
a*y^2 + b*y + c
Multiple substitutions
You can use the subs() command to do
multiple substitutions in one command.
This is done by grouping the variables and
their substitutes (other variables or
numerical values) in braces.
subs(symbolic_function, {substitutant}, {substitute})
ans =
27
Plotting symbolical functions
Plotting symbolic functions in MATLAB is
done with the ezplot() set of commands.
The syntax of ezplot() when y is a function
of x is:
ezplot(y)
x 2-2 x+3
ezplot(y) 40
30
20
10
0
-6 -4 -2 0 2 4 6
x
Plotting symbolical functions
Symbolic Plot Types
ezplot Function plotter if z is a function of x
ezplot(z)
ezmesh Mesh plotter if z is a function of x and y
ezmesh(z)
ezmeshc Combined mesh and contour if z is a function of x and y
plotter ezmeshc(z)
ezsurf Surface plotter if z is a function of x and y
ezsurf(z)
ezsurfc Combined surface and contour if z is a function of x and y
plotter ezsurfc(z)
ezcontour Contour plotter if z is a function of x and y
ezcontour(z)
ezplot3 3-D parametric curve plotter if x is a function of t
if y is a function of t
if z is a function of t
ezplot3(x,y,z)
Finding the Limits of a function-
f(x)
Mathematical Operation MATLAB Command
limit(f)
limit(f,x,a) or
limit(f,a)
limit(f,x,a,'right')
limit(f,x,a,'left')
Differentiation
Symbolic Differentiation
Uses trapezoidal
integration to compute
the integral of y with
respect to x, where the x = linspace(0,pi,10);
trapz(x,y) y = sin(x);
array y contains the trapz(x,y)
>>fun = @(x,y)x.*y^2;
>>A = dblquad(fun,1,3,0,1)
9-10
Triple Integrals
A = triplequad(fun, a, b, c, d, e, f) computes the triple integral
of f(x,y, z) from x = a to b, y = c to d, and z = e to f. Here is an example using an
anonymous function to represent f(x,y,z) = (xy -y2)/z.
clc;clear all
F = @(x,y,z) x.^2+y.^2+z.^2;
9-11 Mtotal = triplequad(F,0,2,-1,1,-4,4)