Chia sẻ: BA AB | Ngày: | Loại File: PPT | Số trang:17

lượt xem
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine Email: © 2007 Daniel Valentine. All rights reserved.

Chủ đề:

Nội dung Text: L1_introduction

  1. Lecture 1: Introduction Lecture Lecture series based on the text: Essential MATLAB for Engineers and Scientists for By By Hahn & Valentine Hahn Email: © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
  2. MATLAB desktop MATLAB Command Window Command History Command Window Window Workspace Window Current Directory Current Window Window Start Button
  3. Command Window Command The Command Window on the right is the main The Command  panel where you interact with MATLAB. You key (or type) and commands after You  the prompt >>; MATLAB executes the commands >> MATLAB and displays results (if requested). and Some commonly used tools and commands:    (up arrow) returns last command input, can be returns repeated repeated – clc – clears the screen – whos – shows list of variables – clear – clears variables
  4. Evaluation of MATLAB Evaluation  HANDS­ON with MATLAB – Type >> 2+3 2+3 into the Command Window >> clc >> whos whos Throughout the lecture, yellow text indicates  what you should type into MATLAB.
  5. Command History Window Command The Command History Window logs all of the The Command  commands you enter in MATLAB. commands It should have logged 2+3. It 2+3  Use the Command History Window to reenter 2+3 Use 2+3 in the command window (use copy-and- paste or double click on 2+3). double  This is useful to retrieve past commands.  Use “Shift” key to select multiple lines. Use “Shift”
  6. Arithmetic with MATLAB Arithmetic with MATLAB  Let us explore by doing exercises: >> 3–2 >> 3–2 >> 3*2 >> 3*2 >> 3/2 >> 3/2 >> 3\2 >> 3\2 >> 3^2 >> 3^2 >> 2/0 >> 2/0 >> 0/2 >> 0/2 >> 3*Inf >> 3*Inf
  7. Algebraic­numeric computations Algebraic­numeric computations  Let us explore by doing exercises: >> a= 3 >> >> b= 2 >> >> a– b >> >> a/ b >> >> a^2 >> a^2 >> c= a*b >> >> d= c^(b+1) >> c^(b+1) >> who
  8. Hiding Output Hiding Output Let us explore by doing exercises:       >> clear; clc >> clear; >> whos >> whos >> a = 3; 3; >> b = 2; >> 2; >> c = a * b; >> b; >> d = c^(b+1); c^(b+1); >> who >> >> % a, b, c, d are in workspace a, >> a, b, c, d
  9. Plot y versus x Plot  Introduction to plotting & displaying data:  >> clear; clc >> clear; >> x = 0:0.1:1; >> 0:0.1:1; >> y = x.^2; >> x.^2; >> whos >> whos >> plot(x,y,x,y,’o’) >> plot(x,y,x,y,’o’) >> disp(' '),disp('...... x ........ y .....'),disp([x‘ y']) >> >> x >> >> y >> % x and y are 1-by-11 arrays of numbers! >> and
  10. Write a Simple Program  Write Consider computing the volume of a cone:  Volume = (pi.*r.^2.*h)./3 radius = 6 inches height = 12 inches In the command window key in:  >> clear; clc >> clear; >> r = 6 >> h = 12 >> v = (pi.*r.^2.*h)./3 >> whos
  11. Editor & M-Files Editor  An M-file in MATLAB is analogous to a txt- An M-file file in Microsoft Notepad. file  An M-file is created in MATLAB text editor. An text  M-files: – You can save your programs (i.e., list of You executable commands) as M-files. executable – You can reopen and modify your program. – They are useful for “debugging” (correcting They errors) as you develop your programs (your technical computing tools). technical
  12. Comments in programs Comments Every time you write a program to be saved, it is Every  helpful for you to comment (i.e., describe) it well. comment (i.e., it To insert a comment on a line in the editor or in To  the Command Window, use the comment operator %, then type your comment. MATLAB:  – will not run lines that begin with the comment operator will (in the editor comments appear in green). Comments  – Comments allow you (and others) to more easily Comments understand your program. understand – When your lines of code are easy to understand, your When code will be easier to use later. code
  13. Art of well-written code Art  A well-written program is like literature; it well-written contains comments explaining: contains – what your program requires as input. – what the variables in the program represent. – what your program computes and displays.  It is useful for you to add a set of header It comments that include the name of the program, your name (as the programmer), and the date the program was created or modified. modified.
  14. Saving code in an M-File Saving Open the editor by:  – Entering the command edit in the command window. Entering edit – Or click the white-sheet-of-paper icon in the upper left Or hand corner directly below file. hand Now enter the lines of code to find the volume of a cone:  rr = 4 h = 12 v = (pi.*r.^2.*h)./3 REMARK: If you save it, add header comments and comments REMARK: explaining what the program does. explaining After you have typed in the code, save it as cone.m. 
  15. This is cone.m in the editor This % % Tool to compute the volume of a cone. % A simple sample for a first lecture. % B.H.& Daniel........... January 2007 % rr = 4; % radius of the cone h = 12; % height of the cone v = (pi.*r.^2.*h)./3 % Volume of the cone
  16. Execute an M-file as a Command Execute Now execute (or run) the program by pushing F5, Now  or by typing on the command line or >> cone cone – or by clicking the run button. (Note that the run button looks run like a page with a down arrow to its left. It can be found below help on the toolbar of the edit window.) the If you entered the code as written on the previous If  slide you will get an error! What went wrong?  Repair your program (Change rr = 4 to r = 4.), save it, and Repair save  (Change run it again. run Now change the height to 24, save and run your Now  program again.
  17. Summary Summary MATLAB can be used like a hand calculator to do MATLAB  arithmetic. You can define (or assign) variables with You  numbers and expressions to do calculations as illustrated by the volume-of-cone example. illustrated The advantage of saving programs as M-files is The M-files  that you open it, make changes and/or execute it again without having to type it all over again. again This concludes our overview of MATLAB and a This  taste of things to come! taste



Đồng bộ tài khoản