Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Programming with MATLAB
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
MATLAB Basics CS 111 Introduction to Computing in Engineering and Science.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Review Selim Aksoy Bilkent University Department of Computer Engineering
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
Al-Amer An Introduction to MATLAB Dr. Samir Al-Amer Term 062.
MATLAB Programming fprintf Cell arrays Structures Flow of control Vectorization Functions 1.
Introduction to MATLAB
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions.
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Interduction to MATLAB Manal Alotaibi Mathematics department College of science King saud university.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
Introduction to Matlab By: Jafar Muhammadi April 2005.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Structured Programming EEN170 Programming in MATLAB.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
EEE 161 Applied Electromagnetics
- Standard C Statements
Expressions and Control Flow in JavaScript
MATLAB DENC 2533 ECADD LAB 9.
Conditional Statements
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
CS100J 26 April. Matlab Use help button!!! Variables, values, types
Use of Mathematics using Technology (Maltlab)
MATLAB Basics.
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
CS 111 Introduction to Computing in Engineering and Science
Matlab Basics.
Dale Roberts, Lecturer IUPUI
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university

Content  Some basic commands.  M-files.  The Colon Operator.  Relational operation.  Logical operation.  Conditional statements.  Loops(for,while) MATLAB

Outline  Some basic commands.  M-files.  The Colon Operator.  Relational operation.  Logical operation.  Conditional statements.  Loops(for,while) MATLAB

who lists all of the variables in your matlab workspace Clc Clears the command window whos list the variables and describes their matrix size clear erases variables and functions from memory clear x erases the matrix 'x' from your workspace ; (semicolon) prevent commands from outputting results % (percent sign) comments line … A line can be terminated with three periods (...), which causes the next line to be a continuation line Some Basic Commands

Input function The input function displays a prompt string in the Command Window and then waits for the user to respond. my_val = input( ‘Enter an input value: ’ ); in1 = input( ‘Enter data: ’ ); in2 = input( ‘Enter data: ’,`s`);

disp The disp( array ) function – >> disp( 'Hello' ) – Hello – >> disp(5) – 5 – >> disp( [ 'Bilkent ' 'University' ] ) – Bilkent University – >> name = 'Alper'; – >> disp( [ 'Hello ' name ] ) – Hello Alper

fprintf The fprintf( format, data ) function –%dinteger –%ffloating point format –%eexponential format –%geither floating point or exponential format, whichever is shorter –\nnew line character –\ttab character

– >> fprintf( 'Result is %d', 3 ) – Result is 3 – >> fprintf( 'Area of a circle with radius %d is %f', 3, pi*3^2 ) – Area of a circle with radius 3 is – >> x = 5; – >> fprintf( 'x = %3d', x ) – x = 5 – >> x = pi; – >> fprintf( 'x = %0.2f', x ) – x = 3.14 – >> fprintf( 'x = %6.2f', x ) – x = 3.14 – >> fprintf( 'x = %d\ny = %d\n', 3, 13 ) – x = 3 – y = 13

Functions and feval

Outline  Some basic commands.  M-files.  The Colon Operator.  Relational operation.  Logical operation.  Conditional statements.  Loops(for,while)

M-files There are two kinds of M-files: - Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace. Any variables that they create remain in the workspace, to be used in subsequent computations - Functions, which can accept input arguments and return output arguments. Internal variables are local to the function.

M-file script Example: write a program to solve the equation.

M-file function Example: create an m-file fuction to evaluate the roots of equation

Outline  Some basic commands.  M-files.  The Colon Operator.  Relational operation.  Logical operation.  Conditional statements.  Loops(for,while)

The Colon Operator For example: 1:10 is a row vector containing the integers from 1 to 10: To obtain non-unit spacing, specify an increment. For example: 100:-7:50 will give you

Outline  Some basic commands.  M-files.  The Colon Operator.  Relational operation.  Logical operation.  Conditional statements.  Loops(for,while)

MATLAB has 6 relational operators to make comparisons between variables. < less than > greater than <= less than or equal to >= greater than or equal to == equal to (You will get this one wrong in the exam!) ~= not equal to

Results of comparison using relational operators: – ZERO, if comparison is false. False = 0 – ONE, if comparison if true. True = 1 – If comparing numbers, any non-zero is considered “true”

>> x=2; >> y=3; >> z=x<y; % same as z = (x<y) >> u=x==y; % same as u = (x==y) >> z,u z = 1 u = 0

The difference between ‘=‘ and’==‘

Outline  Some basic commands.  M-files.  The Colon Operator.  Relational operation.  Logical operation.  Conditional statements.  Loops(for,while)

MATLAB has five logical operators ( also called Boolean operators) : ~ (NOT): z = ~x. & (AND): used to link logical expressions: z =(x b). | (OR): used to link logical expressions: q =(x b). && (Short-Circuit AND): used for operations on 2 scalar logical expressions. || (Short-Circuit OR): used for operations on 2 scalar logical expressions. – (Note | is the shift-\ key, above Enter). xor (exclusive OR) is used to link logical expressions: w = xor(A, B).

Outline  Some basic commands.  M-files.  The Colon Operator.  Relational operation.  Logical operation.  Conditional statements.  Loops(for,while)

If statements If logical expression statements end

Simple example If a <= 30 total = 2*a end

If-else statements If logical expression statement group 1 else statement group 2 end

Simple example If a <= 30 total = 2*a else total = a + 10 end

If-elseif-else If logical expression 1 statement group 1 elseif logical expression 2 statement group 2 else statement group 3 end

Example If a <= 30 c = a * 2 elseif c >= 20 d = c else d = 0 end

Outline  Some basic commands.  M-files.  The Colon Operator.  Relational operation.  Logical operation.  Conditional statements.  Loops(for,while)

For loops for loop variable = m:s:n statements end Example:

Assighnment Q1) Creat m-file function to find factorial of x (use f statement if x<0)