- 46 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Introduction to Matlab' - august

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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

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;

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

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) ]);

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

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)]);

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

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)]);

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

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) ]);

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 …

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.

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.

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.

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!

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 !

What if door is already open !

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!

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!

If Statement: Single Option

condition

evaluated

true

false

statements

Pseudo-code :

if (condition)

statement1 statement2

…

Matlab:

if (condition)

statement1

statement2

…

end

If statement: two options

condition

evaluated

statementT

statementF

Matlab:

if (condition)

statementT1

statementT2

…

else

statementF1

statementF2

…

end

true

false

Doctor’s Algorithm

check patient’s temperature

Is temp. < 36?

Yes

No

Is temp. > 39?

Yes

No

Put on a sweater

Take an aspirin

Go home!

Doctor with Nested If-else

Matlab Way:

if (temp < 36)

put on a sweater

else

if (temp > 39)

take an aspirin

else

go home

end

end

Pseudo-code:

if (temp < 36)

put on a sweater

else

if (temp > 39)

take an aspirin

else

go home

Doctor with If-else & else if

Matlab Way:

if (temp < 36)

put on a sweater

elseif (temp > 39)

take an aspirin

else

go home

end

Pseudo-code:

if (temp < 36)

put on a sweater

else if (temp > 39)

take an aspirin

else

go home

Absolute Value Example

if value negative

true

false

display value

negate value

get value from user

- Get value from user
- If the value is negative
- Negate the value

- Display the value

Absolute Value Program

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

if (value < 0)

value = -value;

end

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

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

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

- if value is negative
- 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

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

Without Indentation

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 = 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

Matlab Program

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

absValue=0;

if (isreal(value)) % or if imag(value) == 0

if (value < 0)

absValue = -value;

else

absValue = value;

end

elseif (real(value) == 0)

absValue=imag(value);

if (absValue < 0)

absValue = -absValue;

end

else

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

end

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

Testing…

Enter a number : >3

The absolute value of your number is 3

>> absolute_complex

Enter a number : >-3

The absolute value of your number is 3

>> absolute_complex

Enter a number : >3i

The absolute value of your number is 3

>> absolute_complex

Enter a number : >-3i

The absolute value of your number is 3

>> absolute_complex

Enter a number : >3+4i

The absolute value of your number is 5

>> absolute_complex

Enter a number : >3-4i

The absolute value of your number is 5

>> absolute_complex

Enter a number : >-3-4i

The absolute value of your number is 5

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

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

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)

What Produces a Logical Answer?

- Literals like 0, 5, true, false …
- Relational Operators like <, > ..
- Logic Operators like &&, ||
- Functions like isreal(), ischar() …

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

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;

end

end

Doctor Algorithm

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

Questions chained :

if (temp < 36)

put on a sweater

elseif (temp > 39)

take an aspirin

else

go home

end

Download Presentation

Connecting to Server..