CE 205-MATLAB For Civil Engineers Irfan Turk Fatih University, 2013-14

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

CE 205-MATLAB for Civil Engineers

Irfan Turk
Fatih University, 2013-14

Using Built-In Functions


Most of the engineering computations require using

mathematical functions. Matlab has an extensive


library to allow you for using such functions.
Example: x=sqrt(9) % x =3
Example : x=[1,9,25]
b= sqrt(x) % b=[1 3 5]
Example : F=size(b)
% F=[1 2]
Example :[rows, coln]= size(F) % rows=1 coln=3
Example : Y=sqrt(sin(pi/2)) % Y = 1

Elementary Math Functions


Function

Done Work

Example

abs(x)

Finds the absolute value of x

abs(-3)=3

sqrt(x)

Finds the square root of x


Finds the real nth root of x. Function does not return
nthroot(x,n) complex results

sqrt(16)=4

sign(x)

Returns a value as one of -1,1, or 0

sign(-33.3)=-1

rem(x,y)

Computes the remainder of x/y

rem(10,4)=2

exp(x)

Computes the value of e^x

exp(3)=20.0855

log(x)

Computes ln(x)-the natural logarithm of x(the base is e)

log(10)=2.3026

log10(x)

Computes common logarithm ox x(to the base 10)

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

Data Analysis Functions


Function

Done Work

Example

max(x)
[a,b]=max(x)

Finds the largest value in a vector x


x=[1 5 2], max(x)=5
Finds the largest value in a vector x as a, and its
location as b
x=[1 5 2], [a,b]=max(x), a=5, b=2

min(x)

Finds the smallest value in a vector x

mean(x)

Finds the mean (or average value) in a vector x x=[1 5 2], mean(x)=3.000

median(x)

Finds the median elements of a vector x

x=[1,5,4]; median(x)=4

mode(x)

Finds the value which occurs most

x=[1,3,4,4]; mode(x)=4

sum(x)

Add the elements up in a vector x

x=[1,3,4,4]; sum(x)=12

prod(x)

Multiply the elements in a vector x

x=[1,3,4,4]; prod(x)=48

x=[1 5 2], min(x)=1

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.

If, elseif, else


if comparison

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.

You might also like