Introduction to matlab
This presentation is the property of its rightful owner.
Sponsored Links
1 / 49

Introduction to Matlab PowerPoint PPT Presentation


  • 28 Views
  • Uploaded on
  • Presentation posted in: General

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 Use disp to display the year. Type in Your Program. Give Your Script a Name. Run Your Program. Program in Action. Search Path.

Download Presentation

Introduction to Matlab

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


Introduction to matlab

Introduction to Matlab


Creating matlab scripts

Creating Matlab Scripts


Birth year program

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


Type in your program

Type in Your Program


Give your script a name

Give Your Script a Name


Run your program

Run Your Program


Program in action

Program in Action


Search path

Search Path


Improve the birth year program

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

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 program1

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

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 digits1

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

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

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

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 program1

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

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

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

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

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

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

Expand 1 & 3

1.Enter kitchen

1.1walk to the kitchen door

1.2go through door

1.3walk into kitchen

2.Get Glass

3.Get milk from fridge

3.1open fridge

3.2get milk

3.3close fridge

4.Fill glass with milk

5.Drink it!

<---door may be closed !


Introduction to matlab

What if door is already open !

1.Enter kitchen

1.1walk to the kitchen door

1.2open door and go through it

1.3walk into kitchen

2.Get Glass

3.Get milk from fridge

3.1open fridge

3.2get milk

3.3close fridge

4.Fill glass with milk

5.Drink it!


Final algorithm

Final Algorithm

1.Enter kitchen

1.1walk to the kitchen door

1.2if door is closed then

1.2.Topen it

1.3walk into kitchen

2.Get Glass

3.Get milk from fridge

3.1open fridge

3.2get milk

3.3close fridge

4.Fill glass with milk

4.1while glass not full

4.1.1pour some milk into it

5.Drink it!


If statement single option

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

If statement: two options

condition

evaluated

statementT

statementF

Matlab:

if (condition)

statementT1

statementT2

else

statementF1

statementF2

end

true

false


Doctor s algorithm

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

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

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

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

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

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

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


Some remarks

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

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

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


Matlab program1

Matlab Program …


Testing

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

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


One solution

One Solution ..


Pseudo code version

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

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

What Produces a Logical Answer?

  • Literals like 0, 5, true, false …

  • Relational Operators like <, > ..

  • Logic Operators like &&, ||

  • Functions like isreal(), ischar() …


Relational operators

Relational Operators


Logical operators

Logical Operators


Absolute solution tricky version

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

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

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


  • Login