MATLAB – More Script Files

Slides:



Advertisements
Similar presentations
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Advertisements

Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
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.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
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.
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Documentation and Comments. What’s a comment? A comment is a simple form of documentation. Documentation is text that you the programmer write to explain.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
September 13, 2005 Lecture 4 - By P. Lin 1 CPET 190 Lecture 4 Problem Solving with MATLAB
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
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.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
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.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Development Environment
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
EEE 161 Applied Electromagnetics
Loops BIS1523 – Lecture 10.
Variables, Expressions, and IO
Introduction to Scripting
Basic operations in Matlab
Topics Introduction to Repetition Structures
Intro to PHP & Variables
MATLAB: Structures and File I/O
CS190/295 Programming in Python for Life Sciences: Lecture 1
Lecture 1: Introduction
More Selections BIS1523 – Lecture 9.
One-Dimensional Array Introduction Lesson xx
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
MATH 493 Introduction to MATLAB
Functions In Matlab.
Introduction to TouchDevelop
Introduction to Programming
Python 19 Mr. Husch.
Islamic University of Gaza
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
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.
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
Using Script Files and Managing Data
Input and Output Python3 Beginner #3.
Python 19 Mr. Husch.
Introduction to Programming
Week 7: Computer Tools for Problem Solving and Critical Thinking
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

MATLAB – More Script Files PLEASE HAVE STUDENTS START MATLAB NOW Instructor Notes: The aim of this lecture is to give the students thorough exercise in writing programs using different ways. Please work with students on all the examples. Give them time in class so that they can write the programs themselves. Script File Input – Output : Chapter 4 MATLAB - 3

Introduction In this lecture we will learn various ways of writing and executing programs in MATLAB. Techniques will be shown for getting data into and out of programs. We will be using the calculation of a GPA (Grade Point Average) as an example throughout this lecture. Notice how appropriate comments in the code help understand what is being done. You will be asked to add such comments to every script file from now on. Instructor Notes: There is one common example which is carried forward through the whole lecture – GPA calculation. This is something every student is familiar with. The different ways of doing this would teach them the various aspects of MATLAB programming. MATLAB - 3

Calculating your GPA Step 1: Identify the grades for the courses. For example, ENG 183 A MATH 251 A- ENG 191 B Step 2: Convert the letter grades into point grades based on OSU system: Grade A : 4.0 Pts Grade A- : 3.7 Pts Grade B+ : 3.3 Pts Grade B : 3.0 Pts ENG 183 4.0 MATH 251 3.7 ENG 191 3.0

Calculating your GPA : contd. Step 3: Find the credit hours for each of the courses. For example: ENG 183 - 3 Credit Hours - 4.0 Grade MATH 251 - 5 Credit Hours - 3.7 Grade ENG 191 - 4 Credit Hours - 3.0 Grade Step 4: Calculate your GPA based on the following formula (Grade in ENG183)*(Credit Hours for ENG183) + (Grade in MATH 251)*(Credit Hours for MATH 251) + (Grade in ENG 191)*(Credit Hours for ENG 191) Credit Hours for ENG183 + Credit Hours for MATH251 + Credit Hours for ENG191 = ( (4*3) + (3.7*5) + (3*4) ) / (3+5+4) = 3.541 GPA =

Writing a MATLAB Program for GPA Calculation Write a MATLAB program for the GPA calculation for the example shown in the previous slide. Use variables so the program can be used for any set of grades/credits. Steps: Create variables for the grades as grade1, grade2 etc. Create variables for the credit hours as credit1, credit2 etc. Write the formula for the GPA calculation using the above variables Values are: Grade1=4 , Grade2=3.7, Grade3=3 Credit1=3, Credit2=5, Credit3=4 Instructor Notes: There is program shown on the next slide. Discuss with students about the various steps needed to solve this problem. Then show them the program till the first step in the next slide. Ask them to complete the rest of it. ENG 183 - 3 Credit Hours - 4.0 Grade MATH 251 - 5 Credit Hours - 3.7 Grade ENG 191 - 4 Credit Hours – 3.0 Grade MATLAB - 3

Writing MATLAB Program for GPA Calculation clear % Initialization of variables clc disp(‘Your Name') disp('Seat Number') % Assign the grades for 3 subjects grade1=4; grade2=3.7; grade3=3.0; % Assign the credit hours for the 3 subjects credit1=3; % Now complete the program!! credit2=5; credit3=4; % Calculate the GPA from the equation GPA = ((grade1*credit1)+(grade2*credit2)+ (grade3*credit3)) / (credit1+credit2+credit3) Instructor Notes: Stop when you see the red comment. Ask the students to write the next few lines themselves. MATLAB - 3

Writing MATLAB Program for GPA Calculation : Using Vectors Now write a MATLAB program for the GPA calculation using vectors for the grades and credit hours. Steps: Create a vector called grade to put the values of grade Create a vector called credit to put the values of credit hours Write the formula for the GPA calculation with vectors using element by element arithmetic

Writing MATLAB Program for GPA Calculation : Using Vectors clear %Initialization of variables clc disp(Your Name') disp(' ') disp('Seat Number') % Assign the grades for 3 subjects in a vector grade=[4 3.7 3]; % Assign the credit hours for the 3 subjects in a vector % Complete the program!!! credit=[3 5 4]; % Calculate the GPA by doing element by element arithmetic GPA = sum(grade.*credit) ./ sum(credit) Instructor Notes: Stop when you see the red comment. Ask the students to write the next few lines themselves. Note that we use the vector arithmetic and the sum function to find GPA MATLAB - 3

Data Inputs to Programs Both of the previous programs defined and assigned values to variables for grade and credit hours inside the program. If we wish to run these programs for a different set of grades/credits, we have to change the program. Once a program is written and verified, it is best not to change it. We need a better way to change input data.

Note the use of single quotes INPUT TO A SCRIPT FILE The previous method requires the user to know the names of variables when assigning the values. Another method is to prompt the user to enter the values. This would make your program more robust and also user-friendly. Instructor notes: In this option, the variable has been defined in the script file, but not set to a value. Rather, the input command is used to request input from the user that will then be assigned to the variable. The format of the variable from the command window must be entered so that it is of the appropriate type. For example, if x is a scalar, then the number given in the command window should be a scalar. If the script file expects x to be a vector, or a matrix, then the values entered from the command window must be entered in correct vector or matrix format. The input () command takes one or two arguments. The first argument is a prompting string enclosed in single quotes. By default, input () expects values from the user that are numbers. If you want to use the input () command to receive string input from the user, then input () will require two arguments. The second argument must be ‘s’, where the “s” enclosed in single quotes tells input () to expect a string. This is done by using the input() command: x = input('text') Note the use of single quotes MATLAB - 3

Interactive Example For example, create a script file with: x = input('Please enter a value for x: ') Execute the script  The command window asks you: Please enter a value of x: x = 5 5 User Input MATLAB - 3

Interactive Example using vectors : Output The values in red are entered following the prompt Your name Seat number Enter the Grades as a vector : [4 3.7 3] Enter the corresponding Credit Hours as a vector : [3 5 4] GPA = 3.5417

fprintf ( )command fprintf(where? how? what?) Can add text and value in one line Know Matlab syntax (example \n, %e,f,i, pages 104-105 Ex: fprintf('\n The overall GPA is: %0.2f\n',GPA)

Improving the output: (see Example.m) % Put grades and credit hours together as a table TBL = [credit;grade]; % print headers (can be disp or fprintf) fprintf('\n Grade Summary\n\n') disp(' Credit') disp(' Hours Grade') %body of table fprintf(' %1i %3.1f\n',TBL) %final results fprintf('\n The overall GPA is: %0.2f\n',GPA)

Produces Grade Summary Credit Hours Grade 3 4.0 5 3.7 4 3.0 3 4.0 5 3.7 4 3.0 The overall GPA is: 3.54

Ctrl C in Command window To break from Matlab’s prompting for input when running a script file, Ctrl C…