1 / 14

Introduction to MATLAB

Introduction to MATLAB. Programming with MATLAB 1. 1 Applied Numerical Methods with MATLAB for Engineers and Scientists , 2nd ed., Steven C. Chapra, McGraw Hill, 2008, Ch. 3. Outline. M-files Script files* Function files Interactive Input and output Enter information Display result

ryder
Download Presentation

Introduction to MATLAB

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. Introduction to MATLAB Programming with MATLAB1 1Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven C. Chapra, McGraw Hill, 2008, Ch. 3.

  2. Outline • M-files • Script files* • Function files • Interactive Input and output • Enter information • Display result • Structured programming • Decisions (or Selection) • Loops (or Repetition)

  3. M-files • M-files: An M-file is a text file stored with a .m extension. • To create a new M-file: Go to File, New, M-file. • Type in the commands line by line in the edit window. • Save the file as filename.m. • To execute an M-file: Type the filename without .m in the command window. • Two types of M-files: • Script files: A script file is a set of MATLAB commands that are saved on a file – we consider script files mainly in this course. • Function files: Read 3.1.2 and 3.5 of the hand-out.

  4. Example: bungee-jumper-1.m • Develop a script file to compute and plot the velocity v of the bungee jumper as a function of time t. Set t = 0 to 20 s in steps of 2. • Save it as bungee-jumper-1.m • Execute the file. • What are the values of v at t = 0, 10, and 20 s? t=[0:2:20]’; %Time(s) g=9.81; %Acceleration due to gravity(m/s^2) m=68.1; %Mass (kg) cd=0.25; %Drag coefficient (kg/m) v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t); figure; %To open a figure window plot(t, v) title(‘Plot of v versus t’) xlabel(‘Values of t’) ylabel(‘Values of v’)

  5. Interactive Input and output • The input command: n = input('promptstring’):Prompt the user to enter a value from the keyboard n = input('promptstring','s'): Prompt the user to enter a string from the keyboard • Examples: >> m = input(‘Mass (kg): ‘); >> myname = input(‘My name is: ‘,’s’);  • The display command: display(value): value = a numeric value, a variable, or a string enclosed in ‘ ‘. • Examples: >> display(m) >> display(myname)

  6. Example: bungee-jumper-2.m g=9.81; %Acceleration due to gravity(m/s^2) m=input(‘Mass (kg): ‘); cd=input(‘Drag coefficient (kg/m): ‘); t=input (‘Time (s): ‘); v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t); disp(‘ ‘) disp(‘Velocity (m/s):’) disp(v) • In the bungee-jumper example, create a script file that prompts the user to enter the values for the mass, m, in kg, the drag coefficient, cd, in kg/m, and the time, t, in s. Also, compute and display the value for the velocity, v, at the specified time instant. • Save the file as bungee-jumper-2.m. • Run the file.

  7. Input: grade = numerical value (0 -100) grade >= 60 F T Output: displayed message (‘passing grade’) Structured programming: Decisions (1) The if Structure: • Syntax: if condition statements end • Example: Write an M-file (Grade.m) to evaluate whether a grade is passing. See the flowchart.

  8. Input: grade = numerical value (0 -100) grade >= 60 T F Output: displayed message (‘Not passing grade’) Output: displayed message (‘passing grade’) Structured programming: Decisions (cont.) (2) The if…else Structure: • Syntax: if condition statements1 else statements2 end • Example: Modify Grade.m to evaluate whether a grade is passing or not passing. See the flowchart.

  9. Structured programming: Decisions (cont.) (3) The if…elseif Structure: • Syntax if condition1 statements1 elseif condition2 statements2 . . . else statementselse end • Example: Write an M-file (mysign.m) to do the following: Input x; If x > 0, display ‘sign = 1’; if x < 0, display ‘sign = -1’; otherwise, display ‘sign = 0’.

  10. Relational operators in MATLAB

  11. Logical operators in MATLAB Priority of the operators: Highest Lowest

  12. Structured programming: Loops (1) The for…end Structure: • Syntax: for index = start:step:finish statements end • Note: • If an increment of 1 is desired, the step can be dropped. • step can be any numeric value, integer or non-integer, positive or negative. • Example: Write your own M-file (mysum.m) to compute the following sum using a for loop: 1 + 2 + 3 +…100 Also, check the MATLAB built-in function sum.

  13. Structured programming: Loops (cont.) (2) The whilestructure: A while loop repeats as long as a logical condition is true. • Syntax: while condition statements end • Example: What will be displayed? x = 8 while x > 0 x = x – 3; disp(x) end

  14. Structured programming: Loops (cont.) (3) The while…break loop: It allows loop termination on a true condition anywhere in the loop. • Syntax: while (1) statements if condition, break, end statements end • Example: What will be the final x value? x = 8; while (1) x = x – 5; if x < 0, break, end end

More Related