Introduction To Matlab Class 2

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Lec 6 Logical Operators String comparison. Review – if statement syntax OR.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Operations on Strings. 8/8/2005 Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved L: String Manipulation.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Introduction To Matlab Class 5 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
28 Formatted Output.
Introduction To Matlab Class 3
ECE Application Programming
Chapter 4: Making Decisions.
Matlab Class 2 Xiaotao Su, Ph. D
Basic operators - strings
Chapter 4: Making Decisions.
Java Programming: Guided Learning with Early Objects
Introduction To Matlab Class 1
Selection CIS 40 – Introduction to Programming in Python
Conditions and Ifs BIS1523 – Lecture 8.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Applying Exponent Rules: Scientific Notation
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Introduction to Java, and DrJava part 1
T. Jumana Abu Shmais – AOU - Riyadh
Differences between Java and C
We are starting JavaScript. Here are a set of examples
Computer Science Core Concepts
Introduction to Java, and DrJava
SELECTIONS STATEMENTS
Unit 4 Scientific Notation
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Matlab Basics.
Introduction to Java, and DrJava part 1
Chap 7. Advanced Control Statements in Java
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
REPETITION Why Repetition?
COMPUTING.
Presentation transcript:

Introduction To Matlab Class 2 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD Double click the matlab icon When prompted click “Skip”

Character Vectors And Numeric Vectors 'hello world' is referred to as a character string or a character vector [35, 26.3, 11, 4] is a numeric vector These are the two main data types in matlab h e l o w r d (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) 35 26.3 11 4 (1) (2) (3) (4)

Character Vectors (aka Strings) And Numeric Vectors How to convert between these two data types? What if I have a string, '39.5' and I want to add to it? num2str & str2num str2num('39.5') + 5 ['hello w', 'orld'] a = [3, 9]; b = [12.6, 8]; [a, b] c = ['1', num2str(39.5)] str2num(c) + 200

Formatting strings with sprintf result = sprintf(format_string, input1, input2 …) Format string: [flags][width][.precision][length] specifier Common Specifiers: %c character %s string %d integer %e or %E scientific notation %f floating point m = 44.5; k = ‘hello world’ res = sprintf(‘%s, it is %d degrees outside’, k, m); res will be “hello world, it is 44.5 degrees outside”

Formatting strings with sprintf Flags: - left justify; default is right 0 left pads the number with zeroes if padding specified Width: just a number Precision: just a number Examples sprintf('%01.2f', 12.4); %outputs 12.40 sprintf('%010.2f', 12.4); %outputs 0000012.40 sprintf('%.3e', 362525200); %outputs 3.625e+8 For further reference go to: http://php.net/manual/en/function.sprintf.php http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

Taking inputs Create a new program called math.m % math.m % % Carries out a series of calculations on two % numbers % written by Your Name July 2010

clear all; %take inputs num1=input('what is the first number?'); num2=input('what is the second number?'); %show calculations disp([num2str(num1), '+', num2str(num2), '=', num2str(num1+num2)]); disp([num2str(num1), '-', num2str(num2), '=', num2str(num1-num2)]);

boolean expressions == > >= < <= & | equals greater than Boolean expressions either return 1 for true or 0 for false Possible operators: == > >= < <= & | equals greater than greater than or equal to less than and or

Note: Put operations inside parenthesis so they are preformed first. 1==1 (1 + 1)==2 (5 + 5) < 10 (5 + 5) <=10 (1 == 1) & (2==3) (1 == 1) | (2==3) 5 + (5 < 10) Note: Put operations inside parenthesis so they are preformed first.

Conditionals (if statements) if(Boolean Expression) do something end you can also do this: if(boolean expression) else do something else or this: if(boolean expression) do something elseif(boolean expr2) do something else end

Back to math.m if ( (round(num1)==num1) & (round(num2)==num2) ) disp('num1 and num2 are integers') end

if ( (round(num1)==num1) & (round(num2)==num2) ) disp('num1 and num2 are integers') elseif(round(num1)==num1) disp('only num1 is an integer') elseif(round(num2)==num2) disp('only num2 is an integer') else disp('neither number is an integer') end

Task 1 == > >= < <= & | equals greater than Have math.m tell me which number is larger then the other “the biggest number is …” == > >= < <= & | equals greater than greater than or equal to less than and or

Tic and toc tic starts a timer toc tells you the number of seconds since when you ran toc

While loop while is a type of loop that runs while a condition is true while(boolean statement) %do something many times end %you would stop repeating the loop when the %boolean statement is false OR if you use %break

While program tic loops = 0; while(toc<5) loops = loops+1; disp(['number of loops = ', num2str(loops)]); disp(['number of seconds = ', num2str(toc)]); end

While program stopping after 10 loops tic loops = 0; while(toc<5) loops = loops+1; disp(['number of loops = ', num2str(loops)]); disp(['number of seconds = ', num2str(toc)]); if(loops>=10) break; end