September 13, 2005 Lecture 4 - By P. Lin 1 CPET 190 Lecture 4 Problem Solving with MATLAB

Slides:



Advertisements
Similar presentations
Multidimensional Array
Advertisements

A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Introduction to MATLAB The language of Technical Computing.
Introduction to arrays
MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Flow Charts, Loop Structures
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
1 Chapter 1 Matter, Measurements, & Calculations 1.6 cont’ Temperature Conversions Copyright © 2005 by Pearson Education, Inc. Publishing as Benjamin Cummings.
Pam Perlich Urban Planning 5/6020
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.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Temperature Temperature is a qualitative measurement- movement of molecules How hot or cold something is. Fahrenheit Celsius o F = 9/5C + 32 o C = 5/9.
Fahrenheit and Celsius Conversion Formulas Fahrenheit to Celsius: Celsius to Fahrenheit:
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
Temperature. Temperature Scales Temperature can be measured with a variety of scales: – Celsius scale – Fahrenheit scale – Kelvin scale.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
1 Chapter 3 Matter and Energy 3.3Temperature Copyright © 2008 by Pearson Education, Inc. Publishing as Benjamin Cummings.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Input, Output, and Processing
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.
MATLAB FUNDAMENTALS: MATRIX/ARRAY FUNCTIONS THE COLON MATRIX/ARRAY MANIPULATION INPUT/OUTPUT HP 100 – MATLAB Wednesday, 9/3/2014
MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB
ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Working with Arrays in MATLAB
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
General, Organic, and Biological ChemistryCopyright © 2010 Pearson Education, Inc.1 Energy and Matter Temperature.
FINDING THE EQUATION OF A LINE 1.KNOWING A POINT AND THE SLOPE 2.KNOWING TWO POINTS.
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.
General, Organic, and Biological Chemistry Fourth Edition Karen Timberlake 2.2 Temperature Chapter 2 Energy and Matter © 2013 Pearson Education, Inc. Lectures.
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
Matlab Basic. MATLAB Product Family 2 3 Entering & Quitting MATLAB To enter MATLAB double click on the MATLAB icon. To Leave MATLAB Simply type quit.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Temperature.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
3.3 Temperature A digital ear thermometer is used to measure body temperature. Learning Goal Given a temperature, calculate a corresponding temperature.
MATLAB – More Script Files
EGR 115 Introduction to Computing for Engineers
Control Structure Senior Lecturer
Temperature, Thermal Energy and Heat Temperature
Lecture 2 Introduction to MATLAB
Presentation transcript:

September 13, 2005 Lecture 4 - By P. Lin 1 CPET 190 Lecture 4 Problem Solving with MATLAB

September 13, 2005 Lecture 4 - By P. Lin 2 Lecture 4: MATLAB Array Basics 4.1 MATLAB Variables and Arrays Variables Variables Variable Initialization Variable Initialization AssignmentAssignment input()input() Variable Value Display Variable Value Display disp() functiondisp() function fprintf() functionfprintf() function Example 4.1.a Row Vector Example 4.1.a Row Vector Example 4.1.b Column Vector Example 4.1.b Column Vector Example 4.1.c Square Matrix or Array Example 4.1.c Square Matrix or Array Example 4.1.d A 2-by-3 Array Example Example 4.1.d A 2-by-3 Array Example

September 13, 2005 Lecture 4 - By P. Lin 3 Lecture 4: MATLAB Array Basics (continue) 4.2 Problem Solving Examples Example 4-2: Calculating Series Resistance Example 4-3: Temperature Conversion

September 13, 2005 Lecture 4 - By P. Lin MATLAB Variables and Arrays Variables Variables Variable names are case sensitive and weakly typedVariable names are case sensitive and weakly typed Numerical variable type – “double” type in the range of to with 15 to 16 significant digitsNumerical variable type – “double” type in the range of to with 15 to 16 significant digits String variables – 16-bit value characters, enclosed by a pair of single quotesString variables – 16-bit value characters, enclosed by a pair of single quotes >> n = 1e-308 n = e e-308 >> n = 1e-400 n = 0 >> name = ‘MATLAB’ name = MATLAB

September 13, 2005 Lecture 4 - By P. Lin MATLAB Variables and Arrays Variable Initialization Variable Initialization Using assignment statementUsing assignment statement Using keyboard input function – input( )Using keyboard input function – input( ) Variable Value Display Variable Value Display disp( )disp( ) fprintf( )fprintf( ) >> x = 10; >> y = x y = >> x1 = input('Please enter a number: ') Please enter a number: 100 x1 = 100 >> disp(x1) >> fprintf('%f',x1) >> fprintf('%e',x1) e+002

September 13, 2005 Lecture 4 - By P. Lin MATLAB Variables and Arrays (continue) Arrays Arrays Definition – an array is a collection data values organized into rows and columns, and accessed by its name and subscriptsDefinition – an array is a collection data values organized into rows and columns, and accessed by its name and subscripts Fundamental unit of data in MATLAB programsFundamental unit of data in MATLAB programs Scalars are treated as arrays with one column and one rowScalars are treated as arrays with one column and one row A MATALB Example: scalar variable n A MATALB Example: scalar variable n >> n = 100;

September 13, 2005 Lecture 4 - By P. Lin MATLAB Variables and Arrays (continue) Arrays Arrays An array can be single dimension or multidimensionalAn array can be single dimension or multidimensional The dimension of the MATLAB array can be defined implicitly by assignment statement or by size( ) functionThe dimension of the MATLAB array can be defined implicitly by assignment statement or by size( ) function Array Initialization ExamplesArray Initialization Examples 1-D row vector 1-D row vector 1-D column vector 1-D column vector 2-D array or matrix 2-D array or matrix

September 13, 2005 Lecture 4 - By P. Lin MATLAB Variables and Arrays (continue) Example 4.1.a - A 1- by-4 Array, or Row Vector Example Example 4.1.a - A 1- by-4 Array, or Row Vector Example >> product = [ ]; >> product(1,1) ans = 10 >> m = product(1,2) m = 20 >> product(1,3) = m product = >> r = 1; c = 4; >> k = product(r,c) K = 40 Syntax for accessing individual element: product(r, c) Subscripts: r - row, c - column

September 13, 2005 Lecture 4 - By P. Lin MATLAB Variables and Arrays (continue) Example 4.1.b - A 3-by-1 Array, or Column Vector Example Example 4.1.b - A 3-by-1 Array, or Column Vector Example >> column_vector = [10; 20; 30] column_vector =

September 13, 2005 Lecture 4 - By P. Lin MATLAB Variables and Arrays (continue) Example 4.1.c - A 4 by 4 Array Example Example 4.1.c - A 4 by 4 Array Example >>items = [ ; ; ; ]; >> r = 3; c = 3; >> items(r, c) ans = 110 >> size(items) ans = 4 4

September 13, 2005 Lecture 4 - By P. Lin MATLAB Variables and Arrays (continue) Example 4.1.d - A 2 by 3 Array Example Example 4.1.d - A 2 by 3 Array Example >> num = [10 20; 30 40; 50 60] num =

September 13, 2005 Lecture 4 - By P. Lin Problem Solving Examples Example 4-2: Calculating Series Resistance. Example 4-2: Calculating Series Resistance. Problem Statement: Problem Statement: You are asked by your supervisor to design a MATLAB program for your colleague. This program should allow the user to calculate the total series resistance of a circuit.You are asked by your supervisor to design a MATLAB program for your colleague. This program should allow the user to calculate the total series resistance of a circuit.

September 13, 2005 Lecture 4 - By P. Lin Problem Solving Examples (continue) Example 4-2: Calculating Series Resistance. Example 4-2: Calculating Series Resistance. Problem Solving Process and Steps: Problem Solving Process and Steps: Define the problemDefine the problem Formulate a mathematical modelFormulate a mathematical model Develop an algorithmDevelop an algorithm Write the code for the problemWrite the code for the problem Test program and verify the solutionTest program and verify the solution Document the program and solutionDocument the program and solution

September 13, 2005 Lecture 4 - By P. Lin Problem Solving Examples (continue) Example 4-2: Calculating Series Resistance. Example 4-2: Calculating Series Resistance. Problem Solving Process and Steps: Problem Solving Process and Steps: 1.Define the problem 2.Formulate a mathematical model - Total R = R1 + R2 + R3 + … + Rn 3.Develop an algorithm Step 3.1: User input R1, R2, R3, … Rn, one at a time Step 3.1: User input R1, R2, R3, … Rn, one at a time Step 3.2: Compute subtotal resistance Step 3.2: Compute subtotal resistance Step 3.3: Repeat until the end of resistance entering Step 3.3: Repeat until the end of resistance entering Step 3.4: Print total resistance Step 3.4: Print total resistance 4. Write the code for the problem

September 13, 2005 Lecture 4 - By P. Lin 15 Example 4-2: Calculating Series Resistance – Solution 1 MATLAB Solution 1 MATLAB Solution 1 Understand who users areUnderstand who users are How to input resistance values?How to input resistance values? The mathematical model or equationThe mathematical model or equation Total R = R1 + R2 + R3 + … + Rn How to display or print total resistance value?How to display or print total resistance value? >> r1 = 10; r2 = 20; >> r3 = 40; >> r_total = r1 + r2 + r3 r_total = 70 70

September 13, 2005 Lecture 4 - By P. Lin 16 Example 4-2: Calculating Series Resistance – Solution 2

September 13, 2005 Lecture 4 - By P. Lin 17 Example 4-2: Calculating Series Resistance – Solution 2 % Program: n_resistor.m % Author: M. Lin % Date: August 20, 2004 % Description: % This program does the follwoings: % 1. Prompts the user to enter the % number of resistors in series % 2. Prompts the user to enter individual % resistor value, and compute the % subtotal % value. % 3. Updates the loop counter until all % resistance are entered % 4. Compute and print the total resistance % Variables used: % r_total -- Total Resistance % r -- Input Resistance % n -- Number of resistors r_total = 0; n = input('Number of Resistors: ‘ ) while n > 0 r = input('Enter a resistance: ‘ ) r = input('Enter a resistance: ‘ ) r_total = r_total + r; r_total = r_total + r; n = n-1; n = n-1;end fprintf('Total Resistance = %f', r_total)

September 13, 2005 Lecture 4 - By P. Lin 18 Example 4-2: Calculating Series Resistance – Solution 2 (continue)

September 13, 2005 Lecture 4 - By P. Lin 19 Example 4-2: Run n_resistor.m at the Command Window Current Directory n_resistor.m 3 hit Enter key 10 hit Enter key 30 hit Enter key 20 hit Enter key Total Resistance

September 13, 2005 Lecture 4 - By P. Lin Problem Solving Examples Example 4-3: Problem Statement: Design a MATLAB program that reads an input temperature in degree Fahrenheit, converts it to an temperature in degree Kelvin and degree Celsius. Example 4-3: Problem Statement: Design a MATLAB program that reads an input temperature in degree Fahrenheit, converts it to an temperature in degree Kelvin and degree Celsius. Analysis of the Problem Analysis of the Problem ReferencesReferences National Institute of Standards, Institute of Standards, Conversion of Temperatures, berlin.de/chemistry/general/units_en.html#tempConversion of Temperatures, berlin.de/chemistry/general/units_en.html#temphttp:// berlin.de/chemistry/general/units_en.html#temphttp:// berlin.de/chemistry/general/units_en.html#temp

September 13, 2005 Lecture 4 - By P. Lin 21 Example 4.3 Temperature Conversion - Analysis of the Problem (cont.) The degree Fahrenheit (°F) - non-metric temperature scale The degree Fahrenheit (°F) - non-metric temperature scale Freezing temperature of water - 32°F Freezing temperature of water - 32°F Boiling temperature of water - 212°F Boiling temperature of water - 212°F Formula for C to F ConversionFormula for C to F Conversion Degree F = C (9/5) + 32 Degree F = C (9/5) + 32

September 13, 2005 Lecture 4 - By P. Lin 22 Example 4-3 Temperature Conversion – Analysis of the Problem (cont.) The degree Celsius (°C) scale - dividing the range of temperature between the freezing and boiling temperatures of pure water at standard atmospheric conditions (sea level pressure) into 100 equal parts. The degree Celsius (°C) scale - dividing the range of temperature between the freezing and boiling temperatures of pure water at standard atmospheric conditions (sea level pressure) into 100 equal parts. Formulas Formulas Degree C = kelvin Degree C = (F – 32)/1.8

September 13, 2005 Lecture 4 - By P. Lin 23 Example 4-3 Temperature Conversion – Analysis of the Problem (cont.) The kelvin (K) temperature scale is an extension of the degree Celsius scale down to absolute zero, a hypothetical temperature characterized by a complete absence of heat energy. The kelvin (K) temperature scale is an extension of the degree Celsius scale down to absolute zero, a hypothetical temperature characterized by a complete absence of heat energy. Temperatures on this scale are called kelvin, NOT degrees kelvin, kelvin is not capitalized, and the symbol (capital K) stands alone with no degree symbol. Temperatures on this scale are called kelvin, NOT degrees kelvin, kelvin is not capitalized, and the symbol (capital K) stands alone with no degree symbol. Formulas Formulas kelvin = C degree F = C (9/5) + 32

September 13, 2005 Lecture 4 - By P. Lin 24 Example 4-3 Temperature Conversion – Program Design We use the following values for validating the computes answers: We use the following values for validating the computes answers: The boiling point temperature of water 212°F 100°C K212°F 100°C K The sublimation point of dry ice -110°F-77.89°C K-110°F-77.89°C K Formulas for computation Formulas for computation T C in degree C = (T F – 32.0)* (9/5) kelvin = T C User input will be from keyboard using input( ) function User input will be from keyboard using input( ) function Display output using fprintf() function Display output using fprintf() function

September 13, 2005 Lecture 4 - By P. Lin 25 Example 4-3 Temperature Conversion – Coding MATLAB Program

September 13, 2005 Lecture 4 - By P. Lin 26 Example 4-3 Temperature Conversion – Coding MATLAB Program

September 13, 2005 Lecture 4 - By P. Lin 27 Example 4-3 Temperature Conversion – Testing the Program >>temp_convert Please enter the temperature in F: 212 Temperature in F Temperature in C Temperature in kelvin >> temp_convert Please enter the temperature in F: -110 Temperature in F Temperature in C Temperature in kelvin

September 13, 2005 Lecture 4 - By P. Lin 28 Summary MATLAB Variables and Arrays MATLAB Variables and Arrays VariableVariable Variable InitializationVariable Initialization Arrays and examplesArrays and examples Problem Solving Examples Problem Solving Examples Calculating series resistanceCalculating series resistance Temperature conversionTemperature conversion

September 13, 2005 Lecture 4 - By P. Lin 29 Question? Answers