27
Atatürk University Control Statements Atatürk University Prof. Dr. İrfan KAYMAZ Computer Programming in MATLAB Atatürk University Engineering Faculty Department of Mechanical Engineering

Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

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 Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements

Atatürk University

Control statements: Conditional statements: if, else, elseif, switch

Repetition statements: while, for

Page 3: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements

Atatürk University

You can also use disp command to display a message enclosed in apostrophes (called a string), or to display an array

‘disp’ command

To display a message and a value on the same line, use the following trick:

Page 4: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 1

Write a Matlab program taking the numerator and denominator from the keyboard, and writing «please enter a number other than zero» message if user enters zero to denominator, and then computing the ratio of them.

clear

N = input(‘enter numerator: ');

D = input(‘enter denominator: ');

if D==0

Disp(‘please enter a number other than zero‘)

else

ratio= N/D

end

Note that if user enters zero as denominator for a second time, D will be zero, and Matlab produces an error.

Page 5: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 2

Write a Matlab program taking the month number from keyborad and then displaying the day number of that month.

Footnote: if user enter a number other than the numbers from 1 to 12, the program will display «it takes 30 days» message. Please change it to display «please enter a number from 1 to 12» message.

clear

m= input(‘which month's day number would you like to learn (1-12)= ' );

if m==1 | m==3 | m ==5 | m==7 | m==8 | m==10 | m==12

disp(‘it takes 31 days‘)

elseif m==2

disp(' it takes 28 days‘)

else

disp(' it takes 30 days‘)

end

Page 6: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 3

Write a Matlab program that asks the user for a mid-term and final grade for a student and then determining the letter grade to the following scheme:

Condition Letter grade

--------- ------------

mean >= 90 A

75 <= mean < 90 B

60 <= mean < 75 C

45 <= mean < 60 D

others F

Page 7: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements

switch conditional statement can be used to construct a program that takes different actions depending on the value of an expression. Switch command changes the tasks among several cases, based on expression.

Control Statements

Conditional statements: if-end, switch-end

switch expression

case {expression1}

statements

case {expression1}

statements

.

.

otherwise ►optional

end

brace or curly paranthesis

Page 8: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 4

Write a Matlab program asking any number from 1 to 10 and then displaying whether the number is an even or odd.

num=input(‘enter a number from 1 to 10 :’);

switch num

case {1,3,5,7,9}

disp(‘that is an odd number’)

case {2,4,6,8,10}

disp(‘that is an even number’)

otherwise

disp(‘that is not between 1-10’)

end

Page 9: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 5

Write another Matlab program for the month example by using the switch command.

clc

clear all

m = input(which month's day number would you like to learn (1-12)= ');

switch m

case {1,3,5,7,8,10,12};

disp(' it takes 31 days ')

case {2}

disp(' it takes 28 days ')

case {4,6,8,9,11};

disp(' it takes 30 days ')

end

Page 10: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements

A loop is a structure that allows a group of commands to be repeated.

In many of programming applications, it is necessary to perform a procedure again and again. To perform this type of statements, a type of loop can be used.

In a loop structure, the program asks a question, and if the answer requires an action, it is performed and the original question is asked again until the answer is such that the action is no longer required.

Control Statements

Repetition statements (LOOPS): for-end, while-end

You can use for loop If you want to execute the statements for a

fixed, predetermined number of times.

You can use while loop If you want to execute the statements for an

undetermined number of times. A while loop repeats a group of

commands as long as a specified condition is true.

Page 11: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements for-end

SYNTAX

for variable= initaial value : end value

statement

statement

…..

end

Page 12: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 6

Write a Matlab program that computes the sum of the numbers from 1 to 20

clear

toplam = 0 ;

for x = 1:20

toplam = toplam + x ;

end

toplam

In algebra, x =x +1 is not a true statement, but in computer programming, it is used to replace the value in memory location named x with a new value equal to x+1.

Page 13: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements

clear

sayi=input(‘Enter a number=‘)

toplam=0 ;

carpim=1 ;

for i=1:sayi

toplam=toplam+i ;

carpim=carpim*i ;

end

toplam

carpim

Example 7

Write a Matlab program that computes the sum and product of the numbers from 1 to a number esked from user by keyboard.

- Be sure to supress intermediate calculations.

- Use ‘ctrl c’ to stop the calculation.

Page 14: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements fprintf

To control exactly what your output looks like, you should use the fprintf function.

The fprintf statement is much more flexible (and therefore more complicated!) than disp.

General form of fprintf command:

fprintf (‘text %g \n’, variable)

Text : text to be displayed

%g : Format specifier

\n : new line (skip to beginning of next line)

variables: a comma-separated list of variables to be displayed according to the location of format specifiers in the text

Page 15: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements fprintf

Some of other format specifiers

%d or %i : Base 10 values

%e : exponential notation

%f : fixed point notation

%c : single character

%s : string of characters

>> T=100;

>> fprintf('The temperature is %g °C \n', T)

The temperature is 100 °C

Page 16: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 8

Write a Matlab program that displays the following table

Number Team

---------------------

1. Fenerbahçe

2. Fenerbahçe

3. Fenerbahçe

4. Fenerbahçe

5. Fenerbahçe

fprintf(NUMBER TEAM\n')

fprintf('-------------------\n')

for i=1:5

fprintf('%g. Fenerbahçe\n',i)

end

Page 17: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 9

Write a Matlab program that displays the numbers from 1 to 10 and squares of

them in two column.

Before writing the program, examine the followings.

balance = 12345;

rate = 0.09;

interest = rate * balance;

balance = balance + interest;

fprintf( 'Interest rate: %g New balance:%g\n',rate, balance )

Interest rate: 0.09 New balance:13456

Page 18: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

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 the packages heavier than 2 kg, 0.5 TL should be added to the base price for every 1 kg. If the packages is 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 computes the price according to the above-mentioned scheme and then prints the following table for 1:50 kg pacages.

(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 19: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements while-end

A while loop repeats a group of commands as long as a specified condition is true.

Syntax:

while expression

statements

end

Page 20: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 10

Write a Matlab program that computes the summation of the number from 5 to 10.

a=4;

T=0

while a<10

a=a+1;

T=T+a;

end

Page 21: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 11

Write a Matlab program that prints your name 10 times.

a=0 while a<10 a=a+1; disp('MATLAB') end

Don’t forget that you can stop the calculation with ‘ctrl c’

If you write an infinite loop into a program and want to stop it,

use ctrl-c.

Page 22: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 12

Write a Matlab program that computes the root of the following equation (first degree) according to the flowchart. A and B coefficients must be entered from keyboard. If user enters zero as A (as long as A is zero), entering must be repeated. Ax+B=0

A=input('input A : '); while A==0 A=input('input A : '); end B=input('input B : '); x=-B/A; fprintf(' root = %g \n',x)

= e

h

Page 23: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 13

Let a=10 and b=7. Write a Matlab program that computes the number of repetition to reach the summation of a and b to 100000 level by increasing a and b two times for each repetition.

a=10;b=7;counter=0; t=0; while t<100000 counter=counter+1; a=2*a; b=2*b; t=a+b; end fprintf('%g \n',counter)

Page 24: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements break

break command terminates the execution of a for or while loop.

Statements in the loop that appear after the break statement is not executed.

for j=2:6 if j==4 break end end fprintf(‘j= %g \n”, j) disp(‘loop ended’)

Page 25: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 14

Nested for loops: for loops can be nested inside each other.

clc

clear

t=0;

for i = 1:5

t = 0;

for j = 1:5

t = t + j;

end

c = t * i;

end

Page 26: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements Example 15

Write a MATLAB program that prints the multiplication table.

for a=1:10

for b=1:10

c=a*b;

fprintf(‘%g * %g = %g\n’, a, b, c)

end

end

Page 27: Computer Programming in MATLAB - Atatürk Üniversitesimuhserv.atauni.edu.tr/makine/ikaymaz/Ematlab/...CONDITIONAL_STATEMENTS… · Write a Matlab program that computes the sum of

Atatürk University

Control Statements mod

mod(X,0) ----- X

mod(X,X) ----- 0

mod(X,Y) ----- if x<y, x

if x>y, remainder of x/y division

mod(3,0)

ans =

3