computer programming in matlab - atatürk...

21
Atatürk University M-Files and Control Statements Atatürk University Prof. Dr. İrfan KAYMAZ Computer Programming in MATLAB Atatürk University Engineering Faculty Department of Mechanical Engineering

Upload: others

Post on 28-May-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements

Atatürk University

Prof. Dr. İrfan KAYMAZ

Computer Programming in

MATLAB

Atatürk University Engineering Faculty

Department of Mechanical Engineering

Page 2: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements

Atatürk University

M-File (script file) file is a text file containing Matlab commands, one per line (or separated by a comma or semicolon).

In order for Matlab to recognize your script file, it must be saved with .m

extension and it must be saved in a location within the Matlab search path.

Script files consist of a collection of Matlab commands.

From now on, write a script for every exercise!

Opening M-files

M-Files

Page 3: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements

Necessity for M-Files If there are a lot of variables, it will be very complicated to assign all the

variables in the command window. it will be easy to change the program in m-file. M-files can be easily reopened and edited.

M-Files

Name of M-Files The rules for script file names are the same as those for MATLAB variable

names. M-file must be in the current folder. Name of the M-file does not contain any Turkish letter. Name of the M-file can not be the Matlab functions like (pi, exp, sin…). Any variable name which was used in m-file must not be given for a file

name.

Page 4: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Current Directory

To run a script file, it is necessary that

current directory is set to the directory

in which the script is saved.

To change the current directory, click

on the browse button (…)

Page 5: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements First M-file

To run a script file, make sure

that the file is saved into the

current directory and then click

on the run icon.

(Please note that the file name

is first.m)

Page 6: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements input(‘’)

Input command enables us to assign a value to a varaible.

Whatever comment is writen between the quotation marks is displayed to the screen, and the user must enter an appropriate value.

X=input(' Comment ');

x= input('Input the value of x: ')

Input the value of x :

x =

10

Page 7: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements input(‘’)

After running the input(‘’) function, you must input a value from the command line.

By using input(‘’) function, you can make your program interactive by asking the user to enter a value which is used in your program.

Example: R = input(‘Please enter the radius of circle: ‘) After the user enters the value from the keyboard, the software assign that value to the variable called R. Note: MATLAB program is case-sensitive.

Page 8: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Example 1

Write a Matlab program to calculate the area and perimeter of a circle by entering the radius of circle from the keyboard.

R = input(' Enter the radius of circle ');

area=pi*R^2;

perimeter=2*pi*R;

area

perimeter

Solution:

Note that the area and perimeter variables are at the end of program and there is no semicolon after them to print the results.

If you put the % sign in front of any line in your program, that line will not be executed. This text is normally used to include comments in your code such as:

% The purpose of this routine is to compute

% the value of ...

Page 9: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Example 2

Write a Matlab program to calculate the value of the following equation by entering x and y from the keyboard.

4

10

32 3yx

)(log

1)ln(yxyxyxy)F(x,

y

yx

y

x

Note: Variable names do not contain punctuations except underscore.

Page 10: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Matlab Operators

There are two main operators in Matlab; Relational and Logical operators

Relational operators are used to compare variables.

Relational operators perform comparisons between two elements or element-by-element comparisons between two arrays.

The result of a comparison is either TRUE (1) or FALSE (0)

If the relation is true, then elements set to logical 1 (true), and If the relation is not true, then elements set to logical 0 (false).

1- Relational Operators

Page 11: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Relational Operators

== Equal to

~=

Not equal to

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

a==b → 0

a==c → 1

a~=b → 1

a~=c → 0

b>a → 1

a>c → 0

b>=a → 1

c>=b → 0

a<b → 1

a<c → 0

a<=b → 1

b<=c → 0

a=5, b=6, c=5

Page 12: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Relational Operators

When we control the equality, we use double == symbols.

When we assign a value to any variable, we use single = symbol.

For example, if you write 3=5 into the command line, MATLAB gives an error. But if we write 3==5 to the command line, it means a question: is 3 equals to 5 ? The result is false, it produces “0” as an answer.

>> 3==5

ans =

0

>> >> 3=5

??? 3=5

|

Error: The expression to the left of the

equals sign is not a valid target for an

assignment.

Page 13: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Logical Operators

Logical operators are used to combine variables.

There are four logical operators;

► "AND"

► "OR"

► "XOR"

These operators are ralated with two relational operators,

binary

► "NOT" This operator is related with one relational operator,

unary

2- Logical operators

Page 14: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Logical Operators

Name Symbol Example with

symbol Text description

NOT ~ ~A True if A is false

AND & A & B True if both A and B are true

OR | A | B True if either A or B (or both!) are true

XOR True if either A or B (but NOT both!) are

true

The following table shows the results of four different pairs of input values to the logical operators above:

A B ~A ~B A & B A | B xor(A, B)

0 0 1 1 0 0 0

0 1 1 0 0 1 1

1 0 0 1 0 1 1

1 1 0 0 1 1 0

Page 15: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Logical Operators

Example:

>> k=4; m=5;

>> (k>6) & (m<8)

ans = 0

>> (k>6) and (m<8) |

Error: Unexpected MATLAB expression.

If you want to write

0<=x<9 in Matlab, type

(0<=x) & (x<9)

or

x>=0 & x<9

but not,

x=>0 & x<9

Operator Name

& AND

| OR

xor XOR

~ NOT

A = [0 0 pi eps] and B = [0 -2.4 0 1],

C = xor(A,B)

C =

0 1 1 0

Page 16: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements

if conditional statement can be used to control conditionally the execution of statements.

As a result of a comparison, selected blocks of program are executed or skipped.

Control Statements

Conditional statements: if-end, switch-end

if expression 1. statement; 2. statement; end

1 2 3 if expression 1. statement; else 2. statement; end

if expression 1. statement; elseif expression 2. statement; else 3. statement; end

There are three different usage:

Page 17: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Example 3

Write a Matlab program that asks the user for an x and y value and then computes

the following functions according to the following scheme:

2

23/1

lnlog)(0

2)(0

x

xxxfx

yxe

yxxfx

x

Page 18: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Example 4

Write a Matlab program that asks the user for an x value and then computes the

following functions according to the following scheme:

2xF(x)

xF(x)

1 < = x < 10

x > =10

Page 19: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Example 5

x>y, A1= (x-y)

x=y, A2= (x+y)7

else, A3= x+y

Write a Matlab program that asks

the user for an x and y value and

then computes the following

functions according to the

following scheme:

x=input('x value=');

y=input('y value=');

if x>y

A1=sqrt(x-y)

elseif x==y

A2=(x+y)^7

else

A3=x+y

end

Page 20: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Homework 1

In a cargo company, a pricing policy according to the package weight is as follows;

Base price is 5 TL for packages up to 2 kg. For packages heavier than 2 kg, 0.5 TL should be added to the base price for every 1 kg. If the packages are heavier than 35 kg, 10 TL should be added to the total price. The packages heavier than 50 kg are not accepted because of worker’s health.

Write a Matlab program that asks the user for package weight and then computes the price according to the above-mentioned scheme.

(Assume that the scale readings are only integer values)

Weight (kg) Price (TL)

1 5

2 5

3 5.5

4 6

… …

50 39

Page 21: Computer Programming in MATLAB - Atatürk …muhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/lecture...Atatürk University M-Files and Control Statements M-File (script file) file is

Atatürk University

M-Files and Control Statements Next Lecture

Control statements - Conditional statements: if, else, elseif, switch - Repetition statements: while, for