1 / 17

Script File Input – Output : Chapter 4

MATLAB – More Script Files. PLEASE HAVE STUDENTS START MATLAB NOW. Script File Input – Output : Chapter 4. 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.

slone
Download Presentation

Script File Input – Output : Chapter 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB – More Script Files PLEASE HAVE STUDENTS START MATLAB NOW Script File Input – Output : Chapter 4

  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.

  3. Calculating your GPA • Step 1: • Identify the grades for the courses. For example, • ENG 183 - Grade A • MATH 251 - Grade A- • ENG 191 – Grade 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 and so on… • Hence: • ENG 183 - 4.0 • MATH 251 - 3.7 • ENG 191 – 3.0

  4. 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 =

  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 • ENG 183 - 3 Credit Hours - 4.0 Grade • MATH 251 - 5 Credit Hours - 3.7 Grade • ENG 191 - 4 Credit Hours – 3.0 Grade

  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)

  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') disp(' ') % 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) Note that we use the vector arithmetic and the sum function to find GPA

  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. 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. This is done by using the input() command: x = input('text') Note the use of single quotes

  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

  12. Interactive Example using vectors Below is shown the program we just discussed treating the grades and credit hours as vectors. clear clc disp('Your name') disp('Seat number') % Ask the user to enter the 3 grades as a vector grade=input('Enter the Grades as a vector :'); % Ask the user to enter the 3 credit hours as a vector credit=input('Enter the corresponding Credit Hours as a vector :'); % Calculate the GPA by doing element by element arithmetic GPA = sum(grade.*credit) ./ sum(credit) Output on next slide

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

  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. Additional Example Script File clear %Initialization of variables clc disp('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)

  17. Script Example (Contd.) Command Window Output: Your Name Seat Number v = 50.6175 >>

More Related