Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB – More Script Files

Similar presentations


Presentation on theme: "MATLAB – More Script Files"— Presentation transcript:

1 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

2 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

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

4 Calculating your GPA : contd.
Step 3: Find the credit hours for each of the courses. For example: ENG Credit Hours Grade MATH Credit Hours Grade ENG Credit Hours 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) = GPA =

5 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 Credit Hours Grade MATH Credit Hours Grade ENG Credit Hours – 3.0 Grade MATLAB - 3

6 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

7 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

8 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=[ ]; % Assign the credit hours for the 3 subjects in a vector % Complete the program!!! credit=[ ]; % 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

9 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.

10 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

11 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

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

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

14 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)

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

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


Download ppt "MATLAB – More Script Files"

Similar presentations


Ads by Google