40

haseeb tariq

Embed Size (px)

Citation preview

SUBJECT: MATLAB

INSTRUCTOR:Mr. AFZAL

Group Leader:Saad Sharif Saleemi

ROLL No.UW-13-ME-BSc-002

DEPARTMENT OF MECHANICAL ENGINEERING

GROUP MEMBERS

Sohail Tariq UW-13-ME-BSc-004

Ramsha Humayun UW-13-ME-BSc-006

Ali Hamza UW-13-ME-BSc-008

Munawar Riaz UW-13-ME-BSc-010

Muhammad Umer UW-13-ME-BSc-012

Haseeb Tariq UW-13-ME-BSc-014

Yousaf Ali UW-13-ME-BSc-016

Maryam Arshad UW-13-ME-BSc-018

Talha Shahid UW-13-ME-BSc-020

TOPIC OF PRESENTATION

scripts and functions in

matlab

loop structure in matlab

Script in matlab

M-files

Functions

Built-in functions

User defined functions

Loops

Types of loops

Implementation of loops

What is Scripts in

matlab?

Scripts in matlab:

Script are m-files containing MATLAB

statements.

M-FILE:

MATLAB can execute a sequence of statements stored in

files. These are called M-files because they must have

the file type .m as the last part of their filename.

Much of our work with MATLAB will be in creating and

refining M-files. M-files are usually created using your

text editor or with MATLAB’s M-file.

There are two types of M-files: script files and function

files

Script files:

Scripts do not accept input arguments or return output arguments. They operate on data in the workspace.

Opening M-file

Go to NEW> M-FILE to start a new M-file.

Editor screen pops out.

Type this in editor:

b=4

a=b+4

save it as filename.m

execute to show result

“a=8”

We’ve just created a MATLAB script file.

Script files:

A script file consists of a sequence of normal MATLAB statements.

Script files are especially useful for analysis and design problems that

require long sequences of MATLAB commands.

With script file written using a text editor or word processor, the file can

be invoked by entering the name of the m-file, without the extension.

Statements in a script file operate globally on the workspace data.

Normally, when m-files are executing, the commands are not displayed on

screen. The MATLAB echo command can be used to view m-files while they

are executing.

When we invoke a script, MATLAB simply executes the

commands found in the file. Scripts can operate on existing

data in the workspace, or they can create new data on which to

operate. Although scripts do not return output arguments, any

variables that they create remain in the workspace, to be used

in subsequent computations. In addition, scripts can produce

graphical output using functions like plot.

Functions in

MatlabBuilt-in

and

user defined functions

Builtin Functions

Exponential

exp(x)

sqrt(x)

Logarithmic

log(x) natural logarithm ln

log10(x)

log2(x)

Builtin continued

numeric ceil(x) round to nearest integer towards +∞

fix(x) round to nearest integer towards 0

floor(x) round to nearest integer towards - ∞

round(x) round to nearest integer

sign(x) +1, 0 or -1

rem(x,y) Finds remainder of x/y

complex abs(x) absolute value

angle(x) in complex plane

conj(x)

imag(x)

real(x)

Builtin continued

Trigonometric and their inverse

cos(x) acos(x)

sin(x) asin(x)

tan(x) atan(x)

cot(x) acot(x)

csc(x) acsc(x)

sec(x) asec(x)

Builtin Functions continued

Hyperbolic and their inverse

cosh(x) acosh(x)

coth(x) acoth(x)

csch(x) acsch(x)

sech(x) asech(x)

sinh(x) asinh(x)

tanh(x) atanh(x)

Remarks

All trigonometric functions require the use of radians and not degrees

All built in functions can also be used with complex numbers.

Builtin Functions for vectors

max(x) returns largest value in vector x

[a,b] = max(x) returns largest value in a and index where

found in b

max(x,y) x and y arrays of same size, returns vector

of same length with lager value from corresponding positions in x and y

same type of functions are available for min

Builtin functions for vectors

sort(x)

mean(x)

median(x)

sum(x)

prod(x)

User defined functions

Similar to script files except that

temporary variables remain local

function use standardized input and output parameters

Script files good for quick work

Functions can be used over and over again

Matlab code for user defned

function

function z = fun(x,y)

X and y values are defined above in parameters of the function

z = 3*x + 6*y.^2 ;

Loops in

matlab

Loops

In computer programming, a loop is a sequence

of instruction s that is continually repeated until a

certain condition is reached. Typically, a certain process

is done, such as getting an item of data and changing it,

and then some condition is checked such as whether a

counter has reached a prescribed number.

Loops structure in Matlab

loops

While loop

Do while

Nested loop

For loop

Nested for loop

Loops

In Matlab, the loop must be completed by the word end.

For Loop

For loop is useful when the number of iterations that a

condition is run is known. for loop will be executed a fixed

number of times

While loops

A while loop will continue to be repeated until some

condition is satisfied While loop is useful when the number of

iterations is unknown.

For Loops

For loops require explicit values in order to

function. These values can be predefined or stated

within the loop.

The Matlab syntax is:

for value=start:counter:finish

[commands]

end

Example for for loop

y = 0;

for k = 1:5

y = y + k

end

For loop

The basic MATLAB loop command is for and it uses the idea of repeating an Operation for all the elements of a vector.

SIMPLE PROGRAM:

>> for j=1:4

j

end

OUTPUT:

j = 1

j = 2

j = 3

j = 4

Factorial program using for

loop

FACTORIAL PROGRAM:

fact = 1;

for i = 2:6

fact = fact * i

end

OUTPUT:

fact = 1;

i=2; fact = fact * i; At this point fact is equal to 2

i=3; fact = fact * i; At this point fact is equal to 6

i=4; fact = fact * i; At this point fact is equal to 24

i=5; fact = fact * i; At this point fact is equal to 120

i=6; fact = fact * i; At this point fact is equal to 720

While loop

Suppose we now want to repeat a loop until a certain

condition is satisfied.

This is achieved by making use of the MATLAB command

while, which has

the syntax.

SYNTAX:

while (condition)

commands...

end.

Example for while loop

Consider this loop:

k = 0;

while k < 10

k = k + 2

end

How many times will the loop be executed?

Initially, k = 0, so the loop is entered

Pass #1: k = 2, so execution continues

Pass #2: k = 4, so execution continues

Pass #3: k = 6, so execution continues

Pass #4: k = 8, so execution continues

Pass #5, k = 10, so k is not less than 10 and execution ends

While loop

count=0;

while count < 1000

disp(‘while loop’)

count=count+1

end

Nested while loop

I=1; N=10;

while I<=N

J=1;

while J<=N

A(I,J)=1/(I+J-1);

J=J+1;

end

I=I+1;

end

Do While Loop

In most computer programming languages, a do while

loop is a control flow statement that executes a block

of code at least once, and then repeatedly executes the

block, or not, depending on a given Boolean condition

at the end of the block

Syntax:-

while condition do

body

end_while

_while(condition, body)

Examples

Intermediate results of statements within a repeat and

while loop are not printed to the screen:

i := 1:

s := 0:

while i < 3 do

s := s + i;

i := i + 1;

end_while

example

Above, only the return value of the loop is displayed. Use

print to see intermediate results:

i := 1:

s := 0:

while i < 3 do

print("intermediate sum" = s);

s := s + i;

i := i + 1;

s

end_while

Any Questions??