eng 1181 college of engineering engineering education innovation center 1 script file input –...
TRANSCRIPT
• ENG 1181
College of EngineeringEngineering Education Innovation Center
1
Script File Input – Output : Chapter 4
PLEASE HAVE STUDENTS START MATLAB NOW
MATLAB – More Script Files
• ENG 1181
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.
Introduction
• ENG 1181
Calculating your GPAStep 1:Identify the grades for the courses. For example,1.ENG 183 - Grade A2.MATH 251 - Grade A-3.ENG 191 – Grade B
Step 2: Convert the letter grades into point grades based on OSU system:
Grade A : 4.0 PtsGrade A- : 3.7 PtsGrade B+ : 3.3 PtsGrade B : 3.0 Pts and so on…
Hence: 1.ENG 183 - 4.02. MATH 251 - 3.73. ENG 191 – 3.0
• ENG 1181
Calculating your GPA : contd.Step 3: Find the credit hours for each of the courses. For example:
1. ENG 183 - 3 Credit Hours - 4.0 Grade2. MATH 251 - 5 Credit Hours - 3.7 Grade3. 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 =
• ENG 1181
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:1.Create variables for the grades as grade1, grade2 etc.2.Create variables for the credit hours as credit1, credit2 etc.3.Write the formula for the GPA calculation using the above variables
Values are:Grade1=4 , Grade2=3.7, Grade3=3Credit1=3, Credit2=5, Credit3=4
1. ENG 183 - 3 Credit Hours - 4.0 Grade2. MATH 251 - 5 Credit Hours - 3.7 Grade3. ENG 191 - 4 Credit Hours – 3.0 Grade
• ENG 1181
Writing MATLAB Program for GPA Calculationclear % Initialization of variablesclcdisp(‘Your Name')disp('Seat Number') % Assign the grades for 3 subjectsgrade1=4;grade2=3.7;grade3=3.0; % Assign the credit hours for the 3 subjectscredit1=3; % Now complete the program!!credit2=5; credit3=4; % Calculate the GPA from the equationGPA = ((grade1*credit1)+(grade2*credit2)+ (grade3*credit3)) / (credit1+credit2+credit3)
• ENG 1181
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:1.Create a vector called grade to put the values of grade2.Create a vector called credit to put the values of credit hours3.Write the formula for the GPA calculation with vectors using element by element arithmetic
• ENG 1181
Writing MATLAB Program for GPA Calculation : Using Vectors
clear %Initialization of variablesclcdisp(Your Name')disp(' ')disp('Seat Number')disp(' ') % Assign the grades for 3 subjects in a vectorgrade=[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 arithmeticGPA = sum(grade.*credit) ./ sum(credit)
Note that we use the vector arithmetic and the sum function to find GPA
• ENG 1181
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.
• ENG 1181
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.
x = input('text')
Note the use of single quotes
This is done by using the input() command:
• ENG 1181
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
• ENG 1181
Interactive Example using vectors
clearclcdisp('Your name')disp('Seat number') % Ask the user to enter the 3 grades as a vectorgrade=input('Enter the Grades as a vector :');
% Ask the user to enter the 3 credit hours as a vectorcredit=input('Enter the corresponding Credit Hours as a vector :'); % Calculate the GPA by doing element by element arithmeticGPA = sum(grade.*credit) ./ sum(credit)
Below is shown the program we just discussed treating the
grades and credit hours as vectors.
Output on next slide
• ENG 1181
Interactive Example using vectors : Output
The values in red are enteredfollowing the promptYour 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
• ENG 1181
Improving the output: (see Example.m)
% Put grades and credit hours together as a tableTBL = [credit;grade]; % print headers (can be disp or fprintf)fprintf('\n Grade Summary\n\n')disp(' Credit')disp(' Hours Grade') %body of tablefprintf(' %1i %3.1f\n',TBL) %final resultsfprintf('\n The overall GPA is: %0.2f\n',GPA)
• ENG 1181
Produces
Grade Summary
Credit Hours Grade 3 4.0 5 3.7 4 3.0
The overall GPA is: 3.54
• ENG 1181
Additional Example
Script Fileclear %Initialization of variablesclcdisp('Your Name')disp(' ')disp('Seat Number')disp(' ')%script file computes the free-fall velocity of a bungee jumper%t=time (s)%m=mass(kg)of the jumper %cd = second-order drag coefficient(kg/m)%v=downward velocity (m/s)g=9.81; m=68.1; t=12; cd=0.25; v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t)
• ENG 1181
Script Example (Contd.)
Command Window Output:
Your Name
Seat Number
v =
50.6175
>>