Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input...

49
Introduction to Matlab

Transcript of Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input...

Page 1: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Introduction to Matlab

Page 2: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Creating Matlab Scripts

Page 3: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Birth Year Program

• Get the user’s age as input

• Display the user’s birth year

• Use input() to get the age

• Use disp to display the year

Page 4: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Type in Your Program

Page 5: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Give Your Script a Name

Page 6: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Run Your Program

Page 7: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Program in Action

Page 8: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Search Path

Page 9: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Improve the Birth Year Program

• The call : “datestr(date, 10)” returns the current year in string format.

• Rather than the line: “2003 – age”, we could have done:

•currentYearStr = datestr(date, 10);

•currentYear = str2num(currentYearStr);

•birthYear = currentYear – age;

Page 10: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Number guessing program

Sample run :

Please pick a number between 10 and 99

Now add the two digits of your number

Substract the result from your number and enter the result: 36

Now subtract 4 from your number and enter the result: 42

The number you picked was 46

Page 11: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Number Guessing Program

% Finds out the two digit number the user is thinking of

disp('Please pick a number between 10 and 99');disp('Now add the two digits of your number');nineA = input('Substract the result from your number and enter the

result: ');

A = nineA / 9;nineAoneB = input(['Now subtract ' num2str(A) ... ' from your number and enter the result: ']);B=nineAoneB - 9 * A;disp(['The number you picked was ' num2str(10*A+B) ]);disp(['In other words, it was ' num2str(A) num2str(B) ]);

Page 12: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Computing Digits

• Given a number and digit number, display the value of that digit.

• Sample run:

Please enter a number 56789

Which digit do you want ? 2

Your digit is : 8

Page 13: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Computing Digits

% Gets a number from user and a digit no. starting with 1

% and returns the numeric value of that digit

n=input('Please enter a number ');

k=input('Which digit do you want ? ');

tenToK = 10^k;

firstK=rem(n,tenToK);

K=floor(firstK/ (tenToK/10));

disp(['Your digit is : ' num2str(K)]);

Page 14: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Getting String Input

>> str = input('Enter a string: >', 's');Enter a string: >so were the days of fall

>> disp(['The length of your string was : ' num2str(length(str)) ' characters ...']);The length of your string was : 24 characters ...

>> str = input('Enter a string: >');Enter a string: >having said all that,??? having said all that,Error: Missing operator, comma, or semicolon.

>> s = 'abcde';>> s(1)ans =a>> s(2)ans =b

Page 15: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Computing Digits, String Version

strn=input('Please enter a number ', 's');

k=input('Which digit do you want ? ');

location = length(strn) - k + 1;

disp(['Your digit is : ' strn(location)]);

Page 16: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

A better guessing program

• Sample run

Multiply your shoe size with 5 and press enter ...

Now add 50, press enter when ready ...

Now multiply the result with 20 an then add 1003 ...

Subtract your birth year from the final result and enter the value >5030

Your shoe size is : 50

Your age is : 30

Page 17: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

A better guessing program

input('Multiply your shoe size with 5 and press enter ...');

input('Now add 50, press enter when ready ...');

input('Now multiply the result with 20 an then add 1003 ...');

sizeAge = input('Subtract your birth year from the final result and enter the value >');

size = floor(sizeAge/100);

age=rem(sizeAge, 100);

disp(['Your shoe size is : ' num2str(size) ]);

disp(['Your age is : ' num2str(age) ]);

Page 18: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Some Other Problems

• Decide which letter grade a student should receive based on his overall grade

• Compute the tax of a sale item based on its type (food 5%, electronics 17%,…)

• Diagnose a patient …

Page 19: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Algorithms

• Systematic procedure that produces—in a finite number of steps—the answer to a question or the solution of a problem. The name derives from the Latin translation, Algoritmi de numero Indorum, of the 9th-century Muslim mathematician al-Khwarizmi's arithmetic treatise “Al-

Khwarizmi Concerning the Hindu Art of Reckoning.” (Brittanica) • A computable set of steps to achieve a desired result. .. The word comes

from the Persian author Abu Ja'far Mohammed ibn Mûsâ al-Khowârizmî who wrote a book with arithmetic rules dating from about 825 A.D. (NIST)

• An algorithm is a sequence of finite number of steps arranged in a specific logical order which, when executed, will produce a correct solution for a specific problem.

Page 20: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Algorithm Representations

– Flow Charts

– Pseudo-code• Pseudo-code is a semi-formal, English-like

language with a limited vocabulary that can be used to design and describe algorithms.

• The main purpose of a pseudo-code is to define the procedural logic of an algorithm in a simple, easy-to-understand manner for its readers, who may or may not be proficient in computer programming.

Page 21: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Another Definition for Pseudocode

• Pseudocode (pronounced SOO-doh-kohd) is a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled natural language rather than in a programming language. Pseudocode is sometimes used as a detailed step in the process of developing a program. It allows designers or lead programmers to express the design in great detail and provides programmers a detailed template for the next step of writing code in a specific programming language.

• Because pseudocode is detailed yet readable, it can be inspected by the team of designers and programmers as a way to ensure that actual programming is likely to match design specifications. Catching errors at the pseudocode stage is less costly than catching them later in the development process. Once the pseudocode is accepted, it is rewritten using the vocabulary and syntax of a programming language. Pseudocode is sometimes used in conjunction with computer-aided software engineering-based methodologies.

• It is possible to write programs that will convert a given pseudocode language

into a given programming language.

Page 22: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Algorithm Example: How to drink a glass of milk

1. Enter kitchen

2. Get Glass

3. Get milk from fridge

4. Fill glass with milk

5. Drink it!

Page 23: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Expand 1 & 3

1. Enter kitchen

1.1 walk to the kitchen door

1.2 go through door

1.3 walk into kitchen

2. Get Glass

3. Get milk from fridge

3.1 open fridge

3.2 get milk

3.3 close fridge

4. Fill glass with milk

5. Drink it!

<--- door may be closed !

Page 24: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

1. Enter kitchen

1.1 walk to the kitchen door

1.2 open door and go through it

1.3 walk into kitchen

2. Get Glass

3. Get milk from fridge

3.1 open fridge

3.2 get milk

3.3 close fridge

4. Fill glass with milk

5. Drink it!

What if door is already open !

Page 25: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Final Algorithm

1. Enter kitchen

1.1 walk to the kitchen door

1.2 if door is closed then

1.2.T open it

1.3 walk into kitchen

2. Get Glass

3. Get milk from fridge

3.1 open fridge

3.2 get milk

3.3 close fridge

4. Fill glass with milk

4.1 while glass not full

4.1.1 pour some milk into it

5. Drink it!

Page 26: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

If Statement: Single Option

false

conditionevaluated

statements

true

Pseudo-code :

if (condition)

statement1 statement2

Matlab:

if (condition)statement1statement2…

end

Page 27: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

If statement: two options

conditionevaluated

statementT

true false

statementF

Matlab:

if (condition)

statementT1

statementT2

else

statementF1

statementF2

end

Page 28: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Doctor’s Algorithmcheck patient’s temperature

Is temp. < 36?

Put on a sweater Is temp. > 39?

Take an aspirin Go home!

Yes

Yes

No

No

Page 29: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Doctor with Nested If-else

Pseudo-code:

if (temp < 36)put on a sweater

else if (temp > 39)

take an aspirinelse

go home

Matlab Way:

if (temp < 36)

put on a sweater

else

if (temp > 39)

take an aspirin

else

go home

end

end

Page 30: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Doctor with If-else & else if

Pseudo-code:

if (temp < 36)

put on a sweater

else if (temp > 39)

take an aspirin

else

go home

Matlab Way:

if (temp < 36)

put on a sweater

elseif (temp > 39)

take an aspirin

else

go home

end

Page 31: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Absolute Value Example

• Get value from user

• If the value is negative– Negate the value

• Display the value false

if value negative

negate value

true

get value from user

display value

Page 32: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Absolute Value Program

value=input('Enter a number : >');

if (value < 0)

value = -value;

end

disp(['The absolute value of your number is ' … num2str(value) ]);

Page 33: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Absolute Value for Complex Numbers

• get value from user

• if value is real– absValue = usual absolute of value

• else if real part of value is zero– absValue is usual absolute of imaginary part of value

• else– absValue is square root of sum of squares of real and

imaginary parts of value

• Display absValue as the result

Page 34: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Complex Absolute in More Detail

• get value from user• if value is real

– if value is negative• absValue is negative of value

– else• absValue = value

• else if real part of value is zero– absValue = imaginary component of value– if (absValue < 0)

• absValue = -absValue

• else– absValue is square root of sum of squares of real and imaginary

parts of value• Display absValue as the result

Page 35: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Some Remarks

• Note that we had a lot of flexibility in expressing same things: “if value is negative” , “if (value < 0)”; “=“ , “is”, “assigned to”…

• Usually you should follow consistent wording, but you are flexible as long as what you express is clear (unambiguous)

• Note tat we had flexibility in first 2 parts of the if statement as well.

• Indentation is crucial in pseudo-code

Page 36: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Without Indentationget value from userif value is realif value is negativeabsValue is negative of valueelseabsValue = valueelse if real part of value is zeroabsValue = valueif (absValue < 0)absValue = -absValueelseabsValue is square root of sum of squares of real and imaginary parts of

valueDisplay absValue as the result

Page 37: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Matlab Programvalue=input('Enter a number : >');absValue=0;if (isreal(value)) % or if imag(value) == 0 if (value < 0) absValue = -value; else absValue = value; endelseif (real(value) == 0) absValue=imag(value); if (absValue < 0) absValue = -absValue; endelse absValue = sqrt(real(value)^2 + imag(value)^2);enddisp(['The absolute value of your number is ' num2str(absValue) ]);

Page 38: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Matlab Program …

Page 39: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Testing…Enter a number : >3The absolute value of your number is 3>> absolute_complexEnter a number : >-3The absolute value of your number is 3>> absolute_complexEnter a number : >3iThe absolute value of your number is 3>> absolute_complexEnter a number : >-3iThe absolute value of your number is 3>> absolute_complexEnter a number : >3+4iThe absolute value of your number is 5>> absolute_complexEnter a number : >3-4iThe absolute value of your number is 5>> absolute_complexEnter a number : >-3-4iThe absolute value of your number is 5

Page 40: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Algorithm Example

Give an algorithm for deciding what to play. Rules are as follows:

• play golf if sky is clear and temp. < 35

• If it is raining but there is no lightining and temp. < 15 play soccer, otherwise play an indoor game.

• If it is cloudy, go running

• if sky is clear but temp > 35 stay at home

• Go indoor swimming if it is raining and temp > 15

• play ping pong when other indoor activities are not suitable

Page 41: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

One Solution ..

Page 42: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Pseudo-code version

If raining if temp > 15 go indoor swimming (raining & temp > 15)

else if no lightining play soccer (raining & temp < 15 & no lightining)

else play ping pong (raning & temp < 15 & lightining)

else if cloudy go running

else if temp < 35 play golf

else stay at home

Page 43: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

What Goes Inside the If() ?

• Any real value

• any non-zero value is interpreted as “true”

• zero value represents “false”

• can also use functions : “true” and “false”

if (123.45)

if (0)

if (true)

Page 44: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

What Produces a Logical Answer?

• Literals like 0, 5, true, false …

• Relational Operators like <, > ..

• Logic Operators like &&, ||

• Functions like isreal(), ischar() …

Page 45: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Relational Operators

Operator Description

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal to

== Equal to

~= Not equal to

Page 46: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Logical Operators

Inputs and or not xor

A B A &&B

A || B

~A xor(A,B)

0 0 0 0 1 0

0 1 0 1 1 1

1 0 0 1 0 1

1 1 1 1 0 0

Page 47: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Absolute Solution (tricky version)

absValue=0;

if (imag(value) == 0 || real(value) == 0)

absValue=imag(value) + real(value);

if (absValue < 0)

absValue = -absValue;

end

else

absValue = sqrt(real(value)^2 + imag(value)^2);

end

Page 48: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Absolute Solution (better)

absValue=0;if (imag(value) ~= 0 && real(value) ~= 0) absValue = sqrt(real(value)^2 + imag(value)^2);else if (imag(value) == 0) absValue = real(value); else % do not need to ask real(value) == 0 absValue = imag(value); end if (absValue < 0) absValue = -absValue; endend

Page 49: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age.

Doctor Algorithm

Questions chained :

if (temp < 36)

put on a sweater

elseif (temp > 39)

take an aspirin

else

go home

end

Individual Questions:

if (temp < 36)

put on a sweater

end

if (temp > 39)

take an aspirin

end

if (temp <= 39 && temp >= 36)

go home

end