You are on page 1of 16

3/4/2022

MATLAB BASICS

Dr. Emile M. Al-Mayah


Biochemical Engineering Department
Al-Khwarizmi College of Engineering
University of Baghdad

OBJECTIVES :
At the end of the lesson, students should be able to:

 Understand the following topics:


• Basic Ideas:
– MATLAB Desktop manipulation
– Basic Commands
– Matrix Operations
– Format
– Using Help
– Save
• Using MATLAB Functions:
– Quick function guide
– Trigonometric functions
– Logical Operations
• Plotting with MATLAB:
– Optimization Problem
– Help Plot
– Importing

1
3/4/2022

BASIC IDEAS

MATLAB R2010b Menu Bar


• Contains the commands you can use to perform certain tasks

2
3/4/2022

Current Directory
• Shows the active directory.

Command Window
• This is where the variables, and MATLAB commands are entered

3
3/4/2022

Workspace
• Creates, imports, displays the name, value, min, and max size of the variables.

Command History Window


• Shows the time/date at which commands were issued.

4
3/4/2022

Window Manipulation 1/8


• Can customize the desktop layout on the local machine. Here is the default layout:

Window Manipulation 2/8


• First on the menu bar: Help>Product help. A window should shift from the left to
the right

10

5
3/4/2022

Window Manipulation 4/8


• Press with the left mouse button and hold on the workspace blue bar
• Drag the workspace window from right to left until the outline matches the picture
below
• Release the mouse button

11

Window Manipulation 5/8


• The workspace window is now “tabbed” with the working directory window on
the left side of the desktop.

12

6
3/4/2022

Window Manipulation 6/8


• Drag the Command history window on the right side of the screen to the left
bottom side until it matches the outline in the picture below.

13

Window Manipulation 7/8


• The desktop layout should now be something like this:

14

7
3/4/2022

Window Manipulation 8/8


• Now save the layout of desktop:
– In the menu bar: Desktop>Save Layout...>Type: “practicum default”>press OK

15

BASIC COMMANDS

16

8
3/4/2022

Basic Commands 1/6


• x=5
– Press the enter button to enter a variable, or command
• y=2;
• x+y
• 2*x+3*x/y
• z=3*y^2

17

Basic Commands 2/6


• To review the value of the variables recently entered look at the “Workspace
window:
– Shows the name of the variable
– Value of the variable
– Min and max values

18

9
3/4/2022

Basic Commands 3/6


• Use up arrow commands to re-enter a previous command, in reverse sequence
– Press the up arrow key once, look at the command window, and press enter

– Press the up arrow key 5 times, then press the down arrow key twice, then
press enter:
• After the 5 times using the arrow key, it shows the x variable
• Then the down arrow key sequence moves down to show the x+y variable

19

Basic Commands 4/6


• Instead of using the arrow keys, use the Command History window:
– Inside the Command History window, single “left click”: “2*x+3*x/y”

– “Right click” the highlighted command and select “Copy”:

– Click anywhere inside the “Command Window” and right click next to “>>”
– Press “Paste button”, the command is now entered into the command window,
now press enter button on the keyboard.

20

10
3/4/2022

Basic Commands 5/6


• Enter in the “clc” command:
– Clears the contents of the command window, but does not erase variables that
are not in the current memory
– Commands in MATLAB is case sensitive
• Enter “clear x”:
– Clears the x-variable, and only that variable in the workspace
• Enter “clear all”:
– Clears all of the variables in the workspace

21

Basic Commands 6/6


• Generate a range of values:
– Type “x=0:0.25:100”
• This generates an array from 0 to 100, in increments of 0.25
• array= Start value: Increment: End value
– Type “x=0:100”
• This generates an array from 0 to 100, in increments of 1
• array=Start value: End value
– Default increment is 1 if it is not specified
– Type “countdown=5:-1:0”
• This generates an array with decreasing value, dependent on the increment.
• Array=Maximum value (Start value): decrease increment: minimum value

22

11
3/4/2022

SOLVING PROBLEM IN MATLAB

23

Problem Solving in Engineering

1. State the problem


2. Describe the input and output
3. Develop an algorithm
4. Solve the problem
5. Test the solution

24

12
3/4/2022

Example 1.1

Albert Einstein

E=mc2

The sun is fueled by the conversion of matter


to energy

25

State the Problem

 Find the amount of matter necessary to


produce the amount of energy radiated
by the sun everyday

26

13
3/4/2022

Describe the Input and Output


 Input
 E=mc2

 Rate of energy radiation

 -E= 385*1024 Joules/second

 Speed of light

 -c=3.0*108 meters/second

 Output
 Mass (m) in kilograms

27

Developing an Algorithm-Hand
Example

28

14
3/4/2022

Developing an Algorithm-Hand
Example

29

Solve the Problem


• Here is below
MATLAB solution

30

15
3/4/2022

Test your Solution

31

Test your Solution

32

16

You might also like