Introduction to Programming for Mechanical Engineers (ME 319)

Slides:



Advertisements
Similar presentations
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Advertisements

Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
CHAPTER 5 INPUT/OUT FORMAT. Introduction READ (*,*) x WRITE (*,*) x This is free format: the first * is for I/O device number (* = input is keyboard *
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
General Computer Science for Engineers CISC 106 Lecture 07 Dr. John Cavazos Computer and Information Sciences 2/25/2009.
Lecture 5 Input and Output inputfprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
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.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
MATLAB Technical Computing Environment. MATLAB Matlab is an interactive computing environment that enables numerical computation and data visualization.
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Introduction to Engineering MATLAB – 7 Script Files - 2 Agenda Script files continued.
GENERATION OF RANDOM NUMBERS
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Early File I/O To help you get started with your final project 1. Definition of “high level” 2. Is using a High Level function appropriate? 3. xlsread()
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Use SPSS for solving the problems Lecture#21. Opening SPSS The default window will have the data editor There are two sheets in the window: 1. Data view2.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Overview Input Input statements Menu statements Output Display statements fprintf statements Message boxes.
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.
Physics 114: Lecture 1 Overview of Class Intro to MATLAB
28 Formatted Output.
Reading and Writing Data Files
MATLAB – More Script Files
Release Numbers MATLAB is updated regularly
Lecture 25.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Chapter 4 MATLAB Programming
Using Script Files and Managing Data
Introduction to Programming for Mechanical Engineers (ME 319)
Intro to PHP & Variables
ORACLE SQL Developer & SQLPLUS Statements
Jeff Henrikson Lecture 11 Data Import & Export Jeff Henrikson 1.
Variables and Arithmetic Operations
Advanced Data Import & Export Jeff Henrikson
Lecture 13 Input/Output Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 3 The DATA DIVISION.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Lecture 2 Introduction to MATLAB
String Manipulation Chapter 7 Attaway MATLAB 4E.
Introduction to MATLAB Programming
Islamic University of Gaza
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Introduction to Java Applications
Introduction to MATLAB Programming
Using Script Files and Managing Data
Matlab Basics.
Introduction to MATLAB Programming
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

Introduction to Programming for Mechanical Engineers (ME 319) Lecture 4, Module 1

Quote of the day “Be the change you wish to see in the world” - M.K.Gandhi

Things you should have known so far! Create vectors and plot them together. Plot functions over an interval using the fplot command. Use plotting options such as specifiers. Assign different line and marker specifiers to different plots. Add text, legend, x-label, y-label, and title to a figure. Use other types of 2-D plots such as the Pie and the Polar coordinates.

Input to a script file >> Ch4_preparation_1 result = -4 -14 What if we need to change the elements in A or B? Is it convenient to open the file each time and change the values manually?

Input to a script file: input >> Ch4_preparation_1 Enter the value of element A(1,1) 2 Enter the value of element A(1,2) -4 Enter the value of element A(2,1) -3 Enter the value of element A(2,2) 1 Enter the value of element B(1) 6 Enter the value of element B(2) 4 result = -14 So, it is more systematic way and more practical Use the syntax provided x = input(‘string’) Hit the enter key to assign the elements values to the variables. Use \n at the end of the string to take the cursor to the next line: x = input(‘string\n’)

Input to a script file: input % in a script file A = [input(‘Element (1,1)’) input(‘Element (1,2)’) input(‘Element (2,1)’) input(‘Element (2,2)’)] Input data: In a script file. In the workspace. In a command through the workspace (using input).

Output commands: disp The disp command is used to display the elements of a variable without displaying the name of the variable. It displays the numerical value in the variable or a string of text. disp(name of variable) or disp(‘text as string’) Examples: >> a = 6; >> disp(a) 6 >> disp('ME319') ME319 After executing the script file, the following will be displayed in the workspace: The result of AxB is 17 39

Output commands: fprintf Unlike disp, the output can be formatted using fprintf. Numerical values and text can be intermixed and displayed in the same line. Also, numbers can be formatted. >> fprintf ('UNLV, Mechanical Engineering, ME319') UNLV, Mechanical Engineering, ME319>> >> fprintf ('UNLV, Mechanical Engineering, \n ME319\n') UNLV, Mechanical Engineering, ME319 >> \n New line Continues the following string in a new line \t Horizontal tab Adds a horizontal tab \b Backspace Deletes the character before

Output commands: fprintf To display a mix of text and numerical values, use the following syntax: fprintf (‘text as string %-5.2f additional text‘,variable_name) The initial text The % sign marks the spot where the variable value is inserted Name of variable to be displayed Formatting elements flag - (minus) Left justifies the number within text + (plus) Adds a plus sign in front of number 0 (zero) Adds zeros in the unused field spaces

Output commands: fprintf To display a mix of text and numerical values, use the following syntax: fprintf (‘text as string %-8.3f additional text‘,variable_name) Name of variable to be displayed The initial text The % sign marks the spot where the number is inserted Formatting elements >> A = 2200/7; >> fprintf('the value is $\t %-8.3f \tdollrs\n',A) the value is $ 314.286 dollrs >> fprintf('the value is $\t %08.2f \tdollrs\n',a) the value is $ 00314.29 dollrs >> Field and precision Field is the integer value (8) The number of used digits in the display Precision is the decimal value (3) (optional) The number of used digits to represent the decimal e Exponential notation lower case 3.14e+002 E Exponential notation upper case 3.14E+002 f Fixed point notation 314.2857 g The shorter of e or f notations G The shorter of E or f notations i Integer

Output commands: fprintf fprintf (‘…text….%f….%f…%g… additional text‘,variable1, variable2, variable3,...) % Example (MATLAB script file): x = input('Enter the balance in dollars\n'); y = input('Enter the money deposited in dollars\n'); z = y - x; fprintf('If your balance is $ %5.2f \n and the money you deposited is $ %5.2f \n then the money you get back from cashier is $ %+5.2f\n',x,y,z) % WORKSPACE Enter the balance in dollars 2.41 Enter the money deposited in dollars 5 If your balance is $ 2.41 and the money you deposited is $ 5.00 then the money you get back from cashier is $ +2.59 >>

Output commands: fprintf fprintf is vectorized, meaning that if the variable is a matrix or a vector, the command repeats itself till all the elements are displayed. >> people = 2:7; >> cost = linspace(10,6,6); >> cinema = [people;cost]; >> fprintf('If the number of people is\t %5.0f\t then the cost per ticket would be \t $%5.2f\t\n',cinema) If the number of people is 2 then the cost per ticket would be $10.00 If the number of people is 3 then the cost per ticket would be $ 9.20 If the number of people is 4 then the cost per ticket would be $ 8.40 If the number of people is 5 then the cost per ticket would be $ 7.60 If the number of people is 6 then the cost per ticket would be $ 6.80 If the number of people is 7 then the cost per ticket would be $ 6.00

Output commands: fprintf Using fprintf to save output to a file Using fprintf to save output to a file: 1st step: open the file using the following syntax: fid = fopen('file_name_including_extension','permission'); 2nd step: use fprintf to write to the file using the following syntax: fprintf(fid, 'text %5.2f additional text',variable_name); 3rd step: close the file using the following syntax: fclose(fid); permission 'r' Opens a file for reading (default) 'w' Opens a file for writing. If the file exists, its content is deleted, if it doesn’t exist, it is created 'a' Same as 'w', except that if the file exists, the data is written at the end of the file 13

Output commands: fprintf Using fprintf to save output to a file minimum_income = 20000:10000:70000; maximum_income = 29999:10000:79999; tax = linspace(5000,22000,6); Income = [minimum_income;maximum_income;tax]; fid1 = fopen('Income_to_tax.doc','w'); fprintf(fid1 ,'Income to tax table\n'); fprintf(fid1,'Minimum income per year Maximum income per year Tax\n\n'); fprintf(fid1,' $%8.2f \t $%8.2f $%8.2f\n',Income); fclose(fid1); Commonly used Extensions .doc Word document .txt Notepad document .xls Excel sheet .ppt Power point document 14

Output commands: sprintf sprintf - Write formatted data to an output string Syntax: sprintf(‘......................’,’format_string’, value1,value2, ..., valueN) sprintf('The price of %s on %d/%d/%d was $%.2f.', ... 'bread', 7, 1, 2006, 2.49) ans = The price of bread on 7/1/2006 was $2.49.

Thank you for the attention Any Questions and Suggestions please…