Professional Documents
Culture Documents
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
Introduction
MATLAB programming language is built around
functions.
A function takes an input argument from the user, and
generates an output to a program, or code where it is
called.
function output=my_function_name(x)
Above, the name of the function is
my_function_name, and the name assigned final
result, or answer isoutput, and the name of variable,
or user input argument is x.
Example
function Result=my_func_cube(x)
% Entered numbers third cube is calculated
Result=x^3;
>> my_func_cube(4)
ans =
64
>>
Example
function [dist,vel,accel]=motion(t)
% This function calculates the distance, velocity, and
%acceleration of a particular car for a given value of t
% assuming all 3 parameters are initially 0
accel=(1/2)*t;
vel=(t^2)/4;
dist=(t^3)/12;
EDU>> [dist,vel,accel]=motion(10)
distance =
83.3333
EDU>> motion(10)
velocity =
ans =
25
83.3333
acceleration =
EDU>>
5
Examples
Example 6.1: For each function below, write a function
in a Matlab editor .
i. f(x,y)=10*x-2*y
ii. f(x,y)=x*e^y
iii. f(x)=sin(x)+cos(x)
iv. f(x,y,z)=x*e^(y/z)
Formatted Output-fprintf
EDU>> t=15.3456789012
t=
15.345678901199999
EDU>> fprintf('There are %f students\n',t)
There are 15.345679 students
EDU>> fprintf('There are %e students\n',t)
There are 1.534568e+001 students
Resulting Action
Linefeed
carriage return(similar to linefeed)
tab
backspace
Result
fixed-point notation
exponential notation
decimal notation
whichever is shorter %f, or %e
character information(displays one character at a time)
strings of characters(displays the entire string)
Formatted Output-sprintf
sprintf function is similar to fprintf function. The
3 4 5
-6 -7 2
6 2 3
EDU>> xlswrite('trying_1',M)
EDU>> M
M=
2 3 4 5
0 -6 -7 2
1 6 2 3
EDU>>
EDU>> [data,fs]=wavread('trying.wav');
EDU>> sound(beta,fs)