Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

MATLAB Basics: Learning Goals, Environment, and Scripting, Slides of Computational Methods

An overview of matlab basics, including learning goals, the matlab environment, and scripting. It covers topics such as turning on matlab, creating basic cartesian plots, writing and saving scripts, executing conditional statements, and using loops. It also explains the difference between script files and function files, and how to enter commands and perform arithmetic operations in matlab.

Typology: Slides

2012/2013

Uploaded on 04/30/2013

aadita
aadita 🇮🇳

4.6

(29)

86 documents

1 / 51

Toggle sidebar

Related documents


Partial preview of the text

Download MATLAB Basics: Learning Goals, Environment, and Scripting and more Slides Computational Methods in PDF only on Docsity! Chp1 MATLAB OverView: Part-1 Docsity.com Learning Goals • Turn On MATLAB and use as a calculator • Create Basic Cartesian Plots • Write and Save simple “Script” Program- files • Execute Conditional Statements – IF, THEN, ELSE, >, <, >=, etc. • Execute Loop Statements – FOR & WHILE Docsity.com Example Cmd Window Session >> %Use MATLAB As Calculator >> 17*19 ans = 323 >> 77/19 -4.3 ans = -0.2474 >> 64^(1/3) + 32^0.2 ans = 6 >> (7+11)*2.5 ans = 45 >> L = 14.4 L = 14.4000 >> W = 13.3 W = 13.3000 >> Area = L*W Area = 191.5200 Docsity.com ipt/Function File Editor @ Editor - | :\Working_Files\Bruce_Files\Chabot_0307\Course_Planning_Fall03\ENGR25_C.. Lei cat 7) t Cell Tools Debug Desktop Window Help PT Gea ee 1 kB. Bayer O5Nevos 2 = Prob 6.12: Thermal Resistance 3 & 4 3 Rearrange the Continuity Eqns listed 5 & Eqns 162 => (Ri + R2)T1 - RITZ = R2Ti 6 4% Eqns 263 RIT - (R2 + R3)T2 + R2TS = 0 7 % Eqns 304 => -R4T2 + (R3 + R4)T3 = BRITO 8 9 4 Know Ti & Toy thus have 3-Fqns in 3-Unknowns 11 3The Knowns € Constants 12- R= (0.036, 4.01, 0.408, 0.038); * in Kelvins/Watt = *¢C/Watt i3- Ti = 20; To = -10; % in °C 14- Area = 10; 4 in sq-m 16 % the Cale § 17 - A= [RU1)+Rl2),-Rl1),O7R(3),—-(Rl2)4R(3)),R(2)70,—- Bl4), R(3) +R (4); 168 - b = (RZ) *TizO;R(3)*To]; 19 - display{'T1, T2, T3 in °e') 20- Ts A\b % in *c 21- display({' Heat Flux in W/sq—rm'} 22 - gq = (1/RC1)d*(Ti - Thi)) 9% in Wésqem 23 - display{'Heat Flow in ™'} 24- O = dreatg Docsity.com Script & Function Files (m-files) • SCRIPTS and FUNCTIONS in MATLAB are stored in text files that end with the extension “.m” – These files are called m-files • SCRIPTS (a.k.a. “programs”) – Scripts files are useful for automating tasks that may need to be repeated. – They have no input/output parameters – They can (but probably shouldn’t) share variables with the command workspace Docsity.com Entering Commands & Expressions • MATLAB retains your previous keystrokes. • Use the up-arrow (↑) key to scroll back through the commands. • Press the key (↑) once to see the previous entry, and so on. • Use the down-arrow (↓) key to scroll forward. • Edit a line using the left (←) & right (→) arrow keys the Backspace key, and the Delete key. • Press the Enter key to execute the command Docsity.com Arithmetic Scalar Operations Symbol Operation MATLAB ^ exponentiation: ab a^b * multiplication: ab a*b / right division: a/b a/b \ left division: b/a a\b + addition: a + b a + b - subtraction: a - b a - b  LEFT-Division A\b read from Right-to-Left as: “b divided by A”C Docsity.com Math Op Precedence (PEMDAS) Precedence Operation First Parentheses, evaluated starting with the innermost pair. Second Exponentiation, evaluated from left to right. Third Multiplication and Division with EQUAL precedence, evaluated from left to right. Fourth Addition and Subtraction with EQUAL precedence, evaluated from left to right. Docsity.com “=“ → Assignment Operator • Typing x = 3 ASSIGNS the value 3 to the variable x. • We can then type x = x + 2. This assigns the value 3 + 2 = 5 to x. But in algebra this implies that 0 = 2. • In algebra we can write x+2 = 20, but in MATLAB we cannot. • In MATLAB the LEFT side of the = operator MUST be a SINGLE variable. • The Right side must be a computable value Docsity.com Work Session Commands Command Description clc Clears the Command window clear Removes all variables from memory clear v1 v2 Removes the variables v1 and v2 from memory exist(‘var’) Determines if a file or variable exists having the name ‘var’ quit Stops MATLAB Docsity.com Work Session Commands cont.1 Command Description who Lists the variables currently in memory whos Lists the current variables and sizes, and indicates if they have imaginary parts. : (Colon) Generates an array having regularly spaced elements , (Comma) Separates elements of an array ; (Semicolon) Suppresses screen printing; also denotes a new row in an array … (Ellipsis) Continues a line Docsity.com The Complex Plane Im (i or j) Re BjABiA +=+= FF or Docsity.com Complex-Number Operations • The number c1 = 1 – 2i is entered as: c1 = 12i or c1 = 1-2j • An Asterisk is NOT needed between i or j and a NUMBER, although it is required with a VARIABLE, such as c2 = 5 i*c1. • Be careful. The expressions – y = 7/2*i and x = 7/2j • give two DIFFERENT results: – y = (7/2)i = 3.5i – and x = 7/(2j) = –3.5j Docsity.com Complex Arithmetic >> Im_Pwr = Z1^3.84 Im_Pwr = -1.6858e+004 -2.5886e+004i >> e_to_Z = exp(Z2) e_to_Z = 6.8518e+006 -2.3163e+007i >> Ln_Z = log(Z1) Ln_Z = 2.6922 + 1.0769i >> Log_Z = log10(Z2) Log_Z = 1.2485 + 0.1242i Docsity.com Discrete Math Examples factor777 = factor(777) factor777 = 3 7 37 GCF = gcd(1001, 1105) GCF = 13 F7 = factorial(7) F7 = 5040 P93 = primes(93) P93 = Columns 1 through 12 2 3 5 7 11 13 17 19 23 29 31 37 Columns 13 through 24 41 43 47 53 59 61 67 71 73 79 83 89 Docsity.com Arrays • An ARRAY is an ORDERED SET of Numbers of with n DIMENSIONS – A regular Number (a SCALAR) is an Array of Dimension ZERO • A VECTOR is a 1-Dim Array  A MATRIX is an ARRAY of Dim ≥ 2 with special properties Docsity.com Arrays in MATLAB • The numbers 0, 0.1, 0.2, …, 10 can be assigned to the array variable u by typing – u = [0:0.1:10] • To compute w = 5 sin u for u = 0, 0.1, 0.2, 0.3, 0.4,…, 10, the command session is; – >>u = [0:0.1:10]; – >>w = 5*sin(u); • The single line, w = 5*sin(u), computed the formula, w = 5 sin(u), 101 times. Docsity.com 5th Order Polynomial • Find the roots of the 5th Order function ( ) 0266465359 2345 =−+−+−= yyyyyyg >> r5 = [1,-9,35,-65,64,-26]; >> roots(r5) ans = 3.0000 + 2.0000i 3.0000 - 2.0000i 1.0000 + 1.0000i 1.0000 - 1.0000i 1.0000  The roots of g(y) • y1,2 = 3 ± 2j • y3,4 = 1 ± j • y5 = 1 Docsity.com Common Math Functions Fcn MATLAB Fcn MATLAB ex exp(x) sin x sin(x) √x sqrt(x) tan x tan(x) ln x log(x) cos-1 x acos(x) log10 x log10(x) sin-1 x asin(x) cos x cos(x) tan-1 x atan(x)  Note that MATLAB Trig functions Operate on RADIANS • Convert using Ratio: π-rads per 180° ( ) ( ) rads 274.1 180 73 e.g., ; 180 = ° ° ° °= radsradsrad ππθθ Docsity.com The “d” Trig Comands for Degrees >> T1 = sind(77) T1 = 0.9744 >> T2 = cosd(19) T2 = 0.9455 >> T3 = tand(53) T3 = 1.3270 >> T4 = asind(.497) T4 = 29.8017 >> T5 = acosd(0.629) T5 = 51.0236 >> T6 = atand(1.73) T6 = 59.9706 Docsity.com Printing From Command Window - 3 • Send to printer from Print Dialog Box  Caveat • In a COMMAND WINDOW session once you Hit Enter () you can NOT Go back to Edit the Text – Can Save your command sequence as an m-file SCRIPT Docsity.com Alternative Cmd Window Printing • Perform MATLAB Operation • Select Desired Text • COPY text to the Windows Paste Buffer • Open Text application – MSWord, WordPad, NotePad, etc. • PASTE the MATLAB Text Into the Text Processor • Print from the Text Processor as Usual Docsity.com DIARY Function to Record Cmnds • Keeping a Session Log → The diary Function – The diary function creates a copy of your session in MATLAB on a disk file, including keyboard input and system responses, but excluding graphics. You can view and edit the resulting text file using any text editor, such as the MATLAB Editor. To create a file on your disk called sept23.out that contains all the functions you enter, as well as output from MATLAB, enter • diary('sept23.out') – To stop recording the session, use • diary('off') – To view the file, run – edit('sept23.out') Docsity.com System, Directory, File Cmnds Command Description pwd Displays the current directory cd dirname Changes the current directory to dirname rmpath dirname Removes the directory dirname from the search path. what Lists the MATLAB-specific files found in the current working directory. Most data files and other non-MATLAB files are not listed. Use dir to get a list of all files what dirname Lists the MATLAB-specific files in directory dirname Docsity.com Plotting with MATLAB • Plot over 573° ( ) ( ) ( )1ln73.1 cos + += p ppq ( ) ( ) ( )1ln73.1cos ++= PPPQ Docsity.com MATLAB Plotting Commands Command Description plot(x,y) Generates a plot of the array y versus the array x on rectilinear axes title(’text’) Puts text in a title at the top of the plot xlabel(’text’) Adds a text label to the horizontal axis (the abscissa). ylabel(’text’) Adds a text label to the vertical axis (the ordinate). grid Puts grid lines on the plot gtext(’text’) Enables placement of text with the mouse Docsity.com Example Problem 1-21 • Plot This Function tetT 2.07ln6 −= • Where – T ≡ Temperature (°C) – t ≡ time (minutes) • For: 1 ≤ t ≤ 3 Docsity.com All Done for Today P1.30 Tutorial Next Meeting Docsity.com Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Appendix Docsity.com Lay | File Edit Yiew Insert Tools Desktop Window Help Dees FRAaMoae OB aoa Prablern 1-22 Temperture (°C) Docsity.com 1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3 -9 -8.5 -8 -7.5 -7 -6.5 -6 time (min) Te m pe ra tu re (° C ) Problem 1-22 Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved