computer programming eciv 2303 fall semester...

28
Computer Programming ECIV 2303— Fall Semester 2019 Islamic University of Gaza Faculty of Engineering Civil Engineering Department Computer Programming Instructor: Dr. Jawdat Abu Taha, Ph.D. Electronics Engineering Room: B328 Telephone: 2853 E-mail: jtaha @iugaza.edu.ps Web-page: http:// site.iugaza.edu.ps/jtha Lecture 1

Upload: others

Post on 23-Mar-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Computer Programming

ECIV 2303— Fall Semester 2019

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming

Instructor: Dr. Jawdat Abu Taha, Ph.D. Electronics EngineeringRoom: B328Telephone: 2853E-mail: [email protected]

Web-page: http://site.iugaza.edu.ps/jtha

Lecture 1

Page 2: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Introduction MATLAB is a powerful language for technical computing.

The name MATLAB stands for MATrix LABoratory, because its basic

data element is a matrix (array).

MATLAB can be used for math computations, modeling and simulations,

data analysis and processing, visualization and graphics, and algorithm

development.

MATLAB is widely used in universities and colleges in introductory and

advanced courses in mathematics, science, and especially engineering.

In industry the software is used in research, development, and design.

The standard MATLAB program has tools (functions) that can be used

to solve common problems.

In addition, MATLAB has optional toolboxes that are collections of

specialized programs designed to solve specific types of problems.

Page 3: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

MATLAB: An Introduction with Applications is intended for students who are using MATLAB for the first time and

have little or no experience in computer programming.

History of MATLAB:

Moler, the chairman of the computer science department at the University of New Mexico, started developing MATLAB

in the late 1970s.

In the 1980’s, MATLAB was rewritten in C with more functionality (such as plotting routines)

The Mathworks, Inc. was created in 1984.

The Mathworks is now responsible for development, sale, and support for MATLAB.

R2017a

Page 4: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Strengths of MATLAB

MATLAB is relatively easy to learn.

MATLAB code is optimized to be relatively quick when performing matrix operations.

MATLAB may behave like a calculator or as a programming language.

MATLAB is interpreted, errors are easier to fix.

Weaknesses of MATLAB

MATLAB is NOT a general purpose programming language

MATLAB is an interpreted language (making it for the most part slower than a compiled language

such as C++)

MATLAB is designed for scientific computation and is not suitable for some things (such as parsing

text- to analyze (a string of characters) in order to associate groups of characters with the syntactic units

of the underlying grammar)

Page 5: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Starting with MATLABChapter l

Icons Window Action icon – the icon showing a down arrow with a

circle around it, i.e., It is in the upper, right corner of most MATLAB windows

Help icon – the question-mark icon ( ) in the Resources Section of the desktop toolbar

Layout icon – ( ) in the Environment Section of the desktop toolbar

Page 6: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

This chapter describes: MATLAB windows Command Window in detail How to do basic arithmetic Create basic variables Introductory script files

Page 7: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

• 1.1 Starting MATLAB, MATLAB Windows

Cu

rren

t Fo

lde

r W

ind

ow

Command Window

Workspace Window

Store all Variables

Command History Window

Page 8: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

• 1.1 Starting MATLAB, MATLAB Windows

Cu

rren

t Fo

lde

r W

ind

ow

Command Window

Workspace Window

Store all Variables

Command History Window

Page 9: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

MATLAB windowsWindow Purpose

Command Window Main window, enters variables, runs programs.

Figure Window Contains output from graphic commands.

Editor Window Creates and debugs script and function files.

Help Window Provides help information.

Command History Window Logs commands entered in the Command Window.

Workspace Window Provides information about the variables that are stored.

Current Folder Window Shows the files in the current folder.

Page 10: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Command Window: The Command Window is MATLAB 's main window and opens when

MATLAB is started.

Figure Window opens automatically after any command that draws a graph.

Page 11: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Editor Window: Use Editor Window to write and debug MATLAB scripts. Open with edit command.

Page 12: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Help Window: Get Help Window by clicking on Help icon (question mark) in tool strip

Page 13: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

More Fun with Windows:

To reopen a window, click the Layout icon and then click on the window name

To get the default window layout (shown before) click the Layout icon, then click Default.

Undocking a window means removing it from the main MATLAB window and then being able to move it independently. To undock a window:

Drag the window’s title bar until the cursor is outside the MATLAB window, then release the cursor or “Click on the Window Action icon, then click on Undock”

To dock a window: Click on the Window Action icon, then click on Dock.

Page 14: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Command Window is MATLAB’s main window.

Use it to:Execute commands

Open other windows

Run programs that you’ve written

Manage the MATLAB software

Page 15: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Basic procedure:1. At prompt (>>), type in MATLAB

command

2. Press ENTER key

3. MATLAB displays result in Command Window, followed by a prompt

4. Repeat from step 1

Page 16: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Notes on Command Window

To start a command, make sure cursor is next to prompt

MATLAB won’t respond until you press ENTER• It then executes only last command

• Commands before last one may still be visible, but MATLAB doesn’t execute them

Can type several commands in same line by putting a comma between commands• Hard to read, so don’t do this often

If command too long to fit on line, can continue to next line by typing ellipsis (3 periods, i.e., … ) and then pressing ENTER

Page 17: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

When cursor is in bottom command line:

• key moves cursor one character to left

• key moves cursor one character to right

• key recalls preceding command

• key recalls command that follows one being displayed.

Page 18: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

PAGE-UP key moves up to previous commands in a window-size at a time.

PAGE-DOWN key moves down to previous commands in a window-size at a time.

BACKSPACE key deletes character to left of cursor.

DELETE key deletes character to right of cursor.

Page 19: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

To quickly execute a previous command but with minor changes.

1. Recall command with up- and down-arrow keys.

2. Use left- and right-arrow keys to move to characters to be altered.

3. Use BACKSPACE or DELETE to remove old character, then type new character.

4. Press ENTER to execute modified command.

Page 20: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Semicolon (;)

When typed at end of command, suppresses output. (Only prompt displayed at next line)

• Useful for preventing display of large outputs

Percent sign(%)

When typed at beginning of line, MATLAB treats line as a comment and doesn’t execute line.

clc command

• Clears Command Window display

Up and down arrows still bring back previous commands

Page 21: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Command History Window

• Shows previous commands, including ones from previous MATLAB sessions

• Double-clicking on command puts it in Command Window and executes it

• Can drag command to Command Window, make changes in command, then execute it

• To clear one or more commands, select the lines to delete, right click, choose Delete Selection

• To clear entire history, right click, select Clear Command History

Page 22: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

In this chapter will only discuss arithmetic with scalars (single numbers)

• Can do arithmetic directly on numbers (like a calculator)

• Can store numbers in variables

Page 23: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Symbols for arithmetic are:

Left division rarely used with scalars

Operation Symbol Example

Addition + 5 + 3

Subtraction – 5 – 3

Multiplication * 5 * 3

Right division / 5 / 3

Left division \ 5 \ 3 = 3 / 5

Exponentiation ^ 5 ^ 3 (means 53 = 125)

Page 24: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Order in which MATLAB does arithmetic

Precedence Mathematical Operation

First Parentheses. For nested parentheses, the innermost are executed first.

Second Exponentiation.

Third Multiplication, division (equal precedence).

Fourth Addition and subtraction.

Page 25: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Precedence order• Same as most calculators

• Same as doing arithmetic by hand

• For multiple operations of same precedence, MATLAB goes left to right

• Can change order by using parentheses

Page 26: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Can use MATLAB as a (very expensive!) calculator

1. Type in mathematical expression

2. Press Enter key

3. MATLAB displays answer in Command Window as ans = followed by the result

Your display may appear on more than one line and have blank lines between text

Page 27: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Can control display of numbers with format command

• Once enter command, format stays the same until another format command

• Default format is fixed point with four digits to right of decimal point

• fixed-point means decimal point always between one’s-digit and one-tenth’s digit

• Format only affects display of numbers. MATLAB always computes and saves numbers in full precision

Page 28: Computer Programming ECIV 2303 Fall Semester 2019site.iugaza.edu.ps/jtaha/files/2019/09/Presentation1-Starting-with-MATLAB.pdf · Computer Programming Lecture 1 MATLAB: An Introduction

Islamic University of GazaFaculty of EngineeringCivil Engineering Department

Computer Programming Lecture 1

Some types of formatting