CE 205-MATLAB For Civil Engineers Irfan Turk Fatih University, 2013-14
CE 205-MATLAB For Civil Engineers Irfan Turk Fatih University, 2013-14
CE 205-MATLAB For Civil Engineers Irfan Turk Fatih University, 2013-14
Irfan Turk
Fatih University, 2013-14
Done Work
Example
abs(x)
abs(-3)=3
sqrt(x)
sqrt(16)=4
sign(x)
sign(-33.3)=-1
rem(x,y)
rem(10,4)=2
exp(x)
exp(3)=20.0855
log(x)
log(10)=2.3026
log10(x)
log10(100)=2
nthroot(-8,3)=-2
Rounding Functions
Function Done Work
round(x) Rounds x to the nearest integer
Rounds x to the nearest integer
fix(x)
toward zero
Rounds x to the nearest integer
floor(x) toward negative infinity
Rounds x to the nearest integer
ceil(x)
toward posotive infinity
Example
round(-8.6)=-9
fix(-8.6)=-8
floor(12.32)=12
ceil(12.32)=13
Discrete Mathematics
Function
factor(x)
gcd(x,y)
lcm(x,y)
rats(x)
primes(x)
isprime(x)
Done Work
Finds the prime factors of
Finds the greatest common denominator of
x and y
Finds the least common multiple of x and y
Represents x as a fraction
Finds all the prime numbers less than x
Checks if x is prime. If it is, the function
returns 1, if not returns 0
Example
factor(12)=2 2 3
gcd(10,15)=5
lcm(2,5)=10
rats(1.5)=3/2
primes(10)=2 3 5 7
isprime(10)=0
Done Work
Example
max(x)
[a,b]=max(x)
min(x)
mean(x)
Finds the mean (or average value) in a vector x x=[1 5 2], mean(x)=3.000
median(x)
x=[1,5,4]; median(x)=4
mode(x)
x=[1,3,4,4]; mode(x)=4
sum(x)
x=[1,3,4,4]; sum(x)=12
prod(x)
x=[1,3,4,4]; prod(x)=48
Examples
Example 3.1: For a given x=[ -1 2 3 0 4 2 0], find each
value of the functions from the previous table.
Example 3.2: What is the output of factor(8), rats(1.2),
isprime(11), round(-5.689) ?
Sorting Values
Function
sort(x)
sort(x,'descend')
size(x)
numel(x)
std(x)
var(x)
rand(n)
rand(n,m)
complex(m,n)
Done Work
Example
Sorts the elements of a vector x in an
ascending order
x=[1,4,2,3];sot(x)=1 2 3 4
Sorts the elements in each column in
x=[1,4;2,3];sot(x,'descend')=4 3
descending order
21
Finds the number of rows and columns in
a vector x
x=[1 2 3;4 5 6];size(x)=2 3
Finds the total number of elements in a
matrix x
x=[1 2 3;0 -2 1]; numel(x)=6
Finds the standard deviation of the
values in a vector x
x=[1,5,3]; std(x)=2
Finds the variance of the data in x
Returns an nby n matri with numbers
between 0 and 1
Returns an nby m matri with numbers
between 0 and 1
Creates a complex number with reel part
as m, and imaginary part as n
x=[1,5,3]; var(x)=4
rand(1)=0.8117
rand(3,4)=?
complex(6,2)=6+2i
Miscellaneous Functions
Function Done Work
abs(x)
Finds the absolute value of x
Returns the largest possible
realmax floating point number in Matlab
Returns the largest possible
intmax integer number in Matlab
Occur for overflow or dividing by
Inf
0
NaN
When calculation is undefined
Example
abs(-2)=2
realmax=1.7977e+308
2147483647
5/0=Inf
0/0=NaN
Simple If Command
if comparison
statement
end
Example 3.3: Write a program that asks your favorite
number. If it is 8, display nice number on the screen.
Hint: Use input function.
statement
elseif comparison
statement
else
statement
end
Example 3.4: Write a program asks your age and tells you
your school type as no school, elementary, middle school,
high school, and university.(0-5/6-10/11-13/14-17/18-or
higher)
Example
Example 3.5: Write a program that rolls a dice; and
prints the number on the screen.
Example 3.6: Write a program that picks a number less
than 5. And try to guess it. The computer prints on the
screen as your guess is greater, your guess is
smaller, or yes you got it.
Example 3.7: Write a program that picks 100 numbers
randomly; and tells you which number occurs most.
Then it plots the numbers on a bar graph.
Hint: For bar graph use bar(variable) command.