0% found this document useful (0 votes)
52 views

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

This document discusses MATLAB functions and programming. It introduces functions, how they take inputs and produce outputs. Examples are provided of simple functions to calculate cubes, motion equations, and user-defined functions. The document also covers user input/output, formatted output using fprintf and sprintf, and reading/writing data to and from files like Excel sheets and wav audio files.

Uploaded by

PratyushAgarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

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

This document discusses MATLAB functions and programming. It introduces functions, how they take inputs and produce outputs. Examples are provided of simple functions to calculate cubes, motion equations, and user-defined functions. The document also covers user input/output, formatted output using fprintf and sprintf, and reading/writing data to and from files like Excel sheets and wav audio files.

Uploaded by

PratyushAgarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CE 205-MATLAB for Civil Engineers

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)

is typed in command window

ans =
64
>>

is the output from the function

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)

User-Controlled Input and Output


EDU>> p=input('enter something\n','s');
enter something
12345
EDU>> p
p=
12345
EDU>> p=input('enter something\n','s');
enter something
how are you
EDU>> p
p=
how are you

User-Controlled Input and Output


EDU>> p=input('enter something\n');
enter something
123
EDU>> p
p=
123
EDU>> p=input('enter something\n');
enter something
hola
??? Error using ==> input
Undefined function or variable 'hola'.
enter something

User-Controlled Input and Output


EDU>> disp('This is just a text to show')
This is just a text to show
EDU>>
EDU>> x=[1 2 3 4]
x=
1 2 3 4
EDU>> disp(x)
1 2 3 4
EDU>>

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

Special Format Commands


Format
Command
\n
\r
\t
\b

Resulting Action
Linefeed
carriage return(similar to linefeed)
tab
backspace

Type Field Format


Type Field
%f
%e
%d
%g
%c
%s

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)

Width Field/Precision Field


EDU>> t
t=
15.345678901199999
EDU>> fprintf('There are %5.3f students\n',t)
There are 15.346 students
Note: %5.3f specifies the minimum total width

available to display your result is 5 digits, 3 of which


are after the decimal point.

Formatted Output-sprintf
sprintf function is similar to fprintf function. The

difference is instead of sending the formatted string to


screen, sprintf assigns it to a name as well.
i.e:
EDU>> r=sprintf('There are %5.3f students\n',t)
r=
There are 15.346 students
EDU>> r
r=
There are 15.346 students

Reading and Writing Data from Files


EDU>> A=xlsread('trying.xlsx')
A=
1 2 3 4 5 -1
3 0 -6 -7 2 3
-4 1 6 2 3 5
EDU>> M =M=A(:,2:5)
2
0
1

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)

You might also like