Computer Programming in MATLAB

Similar documents
MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Introduction to Engineering gii

Dr Richard Greenaway

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

SPEECH RECOGNITION COMMON COMMANDS

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

A General Introduction to Matlab

LAB 1 General MATLAB Information 1

PART 1 PROGRAMMING WITH MATHLAB

ELEMENTARY MATLAB PROGRAMMING

General MATLAB Information 1

Introduction to Programming with RAPTOR

Laboratory 1 Octave Tutorial

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Lecture 1: What is MATLAB?

Introduction to Matlab

Lecture 2 FORTRAN Basics. Lubna Ahmed

2.0 MATLAB Fundamentals

Lab 1 Intro to MATLAB and FreeMat

MATLAB QUICK START TUTORIAL

Introduction to MATLAB

Lecturer: Keyvan Dehmamy

The Graphing Calculator

Matlab Programming Introduction 1 2

McTutorial: A MATLAB Tutorial

Math 2250 MATLAB TUTORIAL Fall 2005

MATLAB The first steps. Edited by Péter Vass

YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

Julia Calculator ( Introduction)

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

1 Introduction to Matlab

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

Basic stuff -- assignments, arithmetic and functions

EGR 111 Introduction to MATLAB

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

INTRODUCTION TO MATLAB PROGRAMMING Lec 1.1: MATLAB Basics

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

The Very Basics of the R Interpreter

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Getting to Know Maple

Introduction to MATLAB Programming

VARIABLES Storing numbers:

Finding, Starting and Using Matlab

Introduction to MATLAB Practical 1

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

Introduction to MATLAB

MATLAB. Miran H. S. Mohammed. Lecture 1

Using Game Maker 8: GML Scripting

Introduction to Matlab

Password Management Guidelines for Cisco UCS Passwords

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

Chapter 3. built in functions help feature elementary math functions data analysis functions random number functions computational limits

CITS2401 Computer Analysis & Visualisation

A Brief Introduction to MATLAB

University of Alberta

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Graphing Calculator Scientific Calculator Version 2.0

Chapter 1 MATLAB Preliminaries

Introduction to MATLAB

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

Introduction to MATLAB

Computational Modelling 102 (Scientific Programming) Tutorials

Introduction to MATLAB

Matlab Lecture 1 - Introduction to MATLAB. Five Parts of Matlab. Entering Matrices (2) - Method 1:Direct entry. Entering Matrices (1) - Magic Square

Introduction to MATLAB

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Appendix C. Numeric and Character Entity Reference

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

PIV Programming. Today s Contents: 1. Matlab Programming 2. An example of PIV in Matlab code 3. EDPIV 4. PIV plugin for ImageJ 5.

Introduction to MATLAB

Section 1: Numerical Calculations

Introduction to MATLAB

Dr Richard Greenaway

Excel Tool: Calculations with Data Sets

1 Introduction to MATLAB

WHAT IS MATLAB?... 1 STARTING MATLAB & USING THE COMMAND LINE... 1 BASIC ARITHMETIC OPERATIONS... 5 ORDER OF OPERATIONS... 7

Programming in Mathematics. Mili I. Shah

Ebooks Chemical Engineering

ECE Lesson Plan - Class 1 Fall, 2001

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

How to Design Programs Languages

! The MATLAB language

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

EP375 Computational Physics

FORTRAN Basis. PROGRAM LAYOUT PROGRAM program name IMPLICIT NONE [declaration statements] [executable statements] END PROGRAM [program name]

Transcription:

Computer Programming in MATLAB Prof. Dr. İrfan KAYMAZ Atatürk University Engineering Faculty Department of Mechanical Engineering

What is a computer??? Computer is a device that computes, especially a programmable electronic machine that performs high-speed mathematical or logical operations or that assembles, stores, correlates, or otherwise processes information. Computer Program is a set of ordered instructions that enable a computer to carry out a specific task. Writing a computer program 1- Algorithm: step-by-step procedure for calculations 2- Choosing language: right language for program 3- Encoding Languages: Scientific and engineering: MATLAB, Pascal, C, C++, Java Database: DBASE,SQL,FOXPRO,PARADOX Operating system: C,C++, Java General purpose: C,C++,Java,VB and Pascal Atatürk University

What is algorithm? An algorithm is an effective method expressed as a finite list of welldefined instructions for calculating a function. A set of instructions or procedures for solving a problem.

Example 1 Let s write an algorithm to calculate the sum of two numbers Step 1 : Start Step 2 :enter X value Step 3 :enter Y value Step 4 :Z = X+Y Step 5 :print Z value Step 6 :End

Example 2 Write an algorithm to calculate the mean of two numbers given. Step 1: start Step 2 :enter X value Step 3 :enter Y value Step 4 :Z = X+Y Step 5 :mean= Z/2 Step 6 :print mean Step 7 :end

Example 3 An algorithm to calculate the area of a rectangle with given length. If the length of edge is defined as negative, it will be redefined. Let s define Variables: First edge length of rectangle: a, Second edge length of rectangle : b, Area of rectangle: area Algorithm: S1 : start S2 : enter a value S3 : if a<0 repeat the second step S4 : enter b value S5 : if b<0, repeat the fourth step S6 : area = a*b S7 : print area value S8 : end

Flowcharts Flow chart gives the logical flow of the solution in a diagramatic form.

Flowcharts

Example 4 Draw a flowchart to find the sum of numbers from 1 to 100 Algorithm: S1 : Start S2 : enter sum as zero S3 : enter counter as zero S4 : add counter to sum S5 : add 1 to counter S6 : if counter <100 then go to S4 S7 : print sum S8 : End

Example 5 Draw a flowchart to find the root of the following first order equation, Ax+B=0 Start A=0 End

Example 6 Draw a flowchart to find the mean and sum of squares, square roots, cubes, squares of cubes of the numbers from 1 to 100

Example 7 Values of F(x) function depending on the x are as follows. X changes from 0 to 10 with the steps of 0.5. Draw the flowchart to calculate F(x) for each x values 0< X < 2 f(x)= X 2< X < 3 f(x)=x -X 3-22 3< X <4 f(x)=x 2-2X+13 4< X f(x)=x 4-3X 2-43

Example 8 Draw a flowchat to find the largest of three numbers of A, B, and C.

Example 9 Draw a flowchat for computing factorial N (N!)

What is MATLAB MATLAB is the acronym for MATrix LABoratory. MATLAB, the language of technical computing, is a programming environment for algorithm development, data analysis, visualization, and numeric computation. MATLAB; has a user-friendly interface, has many of scientific and mathematical function, provides a wide variety of techniques to display data graphically.

What is MATLAB? TOOLBOXES MATLAB has a rich family of application-specific solution packages called toolboxes, which are comprehensive collections of MATLAB functions related to particular problems. Optimization Toolbox Control System Toolbox Neural Network Toolbox Fuzzy Logic Toolbox

References Some useful Links www.mathworks.com http://www.mathtools.net/ http://www.math.utah.edu/lab/ms/matlab/matlab.html http://www.owlnet.rice.edu/~ceng303/manuals/matlab/index.html http://web.cecs.pdx.edu/~gerry/matlab/masteroutline.html#intro http://www.phon.ucl.ac.uk/courses/spsci/matlab/

Start MATLAB Starting Matlab: double click the Matlab icon Command window and Matlab prompt >> (prompt is a symbol on the command window indicating that the matlab is waiting for an input. ) The MATLAB Help Window gives you access to a great deal of useful information about the MATLAB language and MATLAB computing environment. It also has a number of example programs and tutorials. Quiting Matlab Type quit at the prompt, or Select Exit Matlab from the File menu

MATLAB Screen Current Directory - View folders and m-files Current Directory - View folders and m-files Workspace View program variables - Double View click program on a variable variables to see it in the Array Editor - Double click on a variab to see it in the Array Ed Command Window Type commands - Command Type commands window is - Command where user interacts window is with MATLAB where user interacts with MATLAB Command History View past commands - View past commands Atatürk University

Matlab as a calculator - Type expressions at the >>, and press return - Result is computed, and displayed as ans - Use numbers, +, -, *, /, sin, cos, exp, abs >> 2+5 >> 3^2 >> sin(pi/4) >> 2*(2+3)

Matlab as a calculator Use ( ) symbols as parentheses, {} and [] mean something different. Example

Matlab as a calculator Previously entered commands can be recalled by dragging them from the command history to the command window or by pressing the up-arrow key, and then it can be edited. If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt. If you omit the semicolon at the end of a line, that causes the assigned value to be printed on the screen. To clear command window, type clc and press enter.

Variables in MATLAB Variables are named locations in memory where numbers, strings and other elements of data may be stored while the program is working. Variable names are used to assign the result of an expression to a variable. Variables do not need to be declared before assignment. A single equal sign (=) is the assignment operator, LHS (left hand side)= RHS (right hand side) Write the expression on the RHS, and assign the result to the variable named on the LHS (The left-hand-side needs to be a single variable name). Atatürk University

Rules for variable names Variable names are combinations of letters and numbers, but must start with a latter. All of the characters in variable names can not be the numbers. Variable names do not contain punctuation except underscore. Space between any characters in variable names can not be used. You can use underscore _ if you want a space. Matlab functions must not be used as variable names. Be sure to write the exact name of variable when you recall it. Because matlab is case sensitive for variable names. It distinguishes between uppercase and lowercase letters. So A and a are not the same variable. You should not use a MATLAB Function names (sin, cos, length, sqrt etc.). cos=5 changes cos function to a variable name.

Variable names Legal >> X = 5; >> A = sqrt(13); >> B = exp(2); >> A = 2*B >> A = A + 1 >> C = tan(pi/4) Illegal (all for different reasons) >> D = sqrt(e) + 1; >> 3 = E >> 3*A = 14 >> F = 2 3

Aritmetic operators MATLAB utilizes the following arithmetic operators: + addition - subtraction * multiplication / division ^ power operator ' transpose We will use dot. in front of these operators to make them element by element operators

Matlab functions Commonly used Matlab functions Function Symbol Example Sine, sin( ) sin sin(pi) Cosine, cos( ) cos cos(pi) Tangent, tan( ) tan tan(pi) Arcsine, arcsin( ) Arccosine, arccos( ) arctangent, arctan( ) asin acos atan asin(0) acos(0) atan(1) Function Symbol Example Eksponential, e x exp exp(2) Natural logarithm, ln(x) Common (base 10) logarithm log log10 log(10) log10(10) Square root, x sqrt sqrt(25) Absolute value, x abs abs(3)

Expressions suitable for Matlab

Mathematical Constants eps i Inf intmax intmin j NaN pi realmax realmin Floating-point relative accuracy Imaginary unit Infinity Largest value of specified integer type Smallest value of specified integer type Imaginary unit Not-a-Number Ratio of circle's circumference to its diameter Largest positive floating-point number Smallest positive normalized floating-point number

Keyboard symbols ~ tilde! exclamation mark % per cent ^ wedge & ampersand or and * asterisk ( left parenthesis ) right parenthesis _ underscore - hyphen, minus or dash = equals + plus { left brace (curly parenthesis) } right brace (curly parenthesis) [ left bracket ] right bracket vertical bar \ backslash / slash? question mark : colon ; semi-colon " quote, quotation mark ' apostrophe, single quote < less > greater, comma. dot * star + plus

Homework Please write the following nice mathematical expression suitable for computer programming x 3 1 c a b a c 2 b a cd 5 3 b 4 ( ac) 3 a a 2 4 a

Next lecture M-Files and Control statements