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

Arrays-and-Script matlab notes, Slides of Matlab skills

Arrays-and-Script notes engineering

Typology: Slides

2020/2021

Uploaded on 08/12/2021

mohammed-shehada
mohammed-shehada 🇵🇸

2 documents

1 / 38

Toggle sidebar

Related documents


Partial preview of the text

Download Arrays-and-Script matlab notes and more Slides Matlab skills in PDF only on Docsity! Lecture 3: Array Applications, Cells, Structures & Script Files Dr. Mohammed Hawa Electrical Engineering Department University of Jordan EE201: Computer Applications. See Textbook Chapter 2 and Chapter 3. Euclidean Vectors * An Euclidean vector (or geometric vector, or simply a vector) is a geometric entity that has both magnitude and direction. * In physics, vectors are used to represent physical quantities that have both magnitude and direction, such as force, acceleration, electric field, etc. * Vector algebra: adding and subtracting vectors, multiplying vectors, scaling vectors, etc. Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan Vector Scaling * For vector: a= da,i+ a,j+a,k * Scaling this vector by a factor of 2 gives: °* v=2a = 2a,i+ 2ayj + 2a,k ¢ This is just like MATLAB scalar multiplication of a vector: ONT 2*[x, Vr Z]; Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 5 Adding and Subtracting Vectors Vector addition by geometry: The Vv vV+w parallelogram law. Or, mathematically: WwW a=a,i+a,j+a,k b = b,i+ byj + b,k a+b =(a,+b,)i c + (ay + by)j b + (a, + b,)k Same as vector addition a and subtraction in MATLAB. c-~a-b Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 6 Exercise >> a= [2 -4 6] 6 -1] -1 5 7 12 Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 7 Complex Numbers Copyright © Dr. Mohammed Hawa >> a=7 + 45 act 7.0000 + 4.0000i >> [theta, rho] = cart2pol(real(a), imag(a) ) theta = 0.5191 rho = 8.0623 2 >> rho = abs(a) % magnitude of complex number rho = 8.0623 >> theta = atan2(imag(a), real(a)) theta = 0.5191 % atan2 is four quadrant inverse tangent >> b=3 + 43 b= 3.0000 + 4.00001 >> atb ans = 10.0000 + 8.0000i >> axb ans = 5.0000 + 40.0000i Electrical Engineering Department, University of Jordan 10 Polynomials ¢ A polynomial can be written in the form: Anx” + Any xr +e + agx? +a,x + a * Or more concisely: n ». aAjix i=0 * We can use MATLAB to find all the roots of the polynomial, i.e., the values of x that makes the polynomial equation equal 0. Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 11 Exercise Polynomial Roots: x3 — 7x? + 40x - 34 =0 Roots arex=1,x =3 451. We can also build polynomial coefficients from its roots. We can also multiply (convolution) and divide (deconvolution) two polynomials. Copyright © Dr. Mohammed Hawa >> a = [1 -7 40 -34]; >> roots (a) ans = 3.0000 + 5.0000i 3.0000 - 5.0000i 1.0000 >> poly([1 3+5i 3-5i]) ans = 1 -7 40 -34 Electrical Engineering Department, University of Jordan 12 Useful functions C = cell(n) Creates n X n cell array C of empty mattices. C = cell(n,m) Creates n X m cell array C of empty matrices. celldisp(C) Displays the contents of cell array C. cellplot (Cc) Displays a graphical representation of the cell array C. C = num2cell (A) Converts a numeric array A into a cell array C. iscell(C) Returns a 1 if C is a cell array; otherwise, returns a 0. Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 15 Exercise >> C = cell (3) c= [] [] [] [] [] [] [] [] [] >> D = cell(l1, 3) D= [] [] [] >> A(1,1) = {'Walden Pond'}; >> A(1,2) = {[1+21 5+9i]}; >> A(2,1) = {[60,72,65]}; >> A(2,2) = {[55,57,56;54,56,55;52,55,53]}; >> A A = "Walden Pond' [1x2 double] [1x3 double] [3x3 double] Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 16 Exercise (Continue) >> celldisp (A) A{i,1} = Walden Pond A{2,1} = 60 72 65 A{1,2} = 1.0000 + 2.0000i 5.0000 + 9.0000i A{2,2} = 55 57 56 54 56 55 52 55 53 >> B= {[2,4], [6,-973,5]; [7;2], 10} [1x2 double] [2x2 double] [2x1 double] [ 10] >> B{1,2} ans = 6 -9 3 5 Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 17 Investigate Structure >> student (2) ans = name: 'Mary Jones! SSN: '431-56-9832' email: 'jonesm@myschool.edu' exam_scores: [84 78 93] >> fieldnames (student) ans = "name! "SSN! "email' "exam_scores' >> max (student (2) .exam_scores) ans = 93 >> isstruct (student) ans = 1 Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 20 Script files * You can save a particular sequence of MATLAB commands for reuse later in a script file (.m file) * Each line is the same as typing a command in the command window. * From the main menu, select File | New | Script, then save the file as mycylinder.m || File Edit Text Go Cell Tools Debug Desktop Window DSGE|4eOre|SB-|Mevele "BCS! -i0 | +) =l1a | x | 086 | O@ a res 7) h = 13 a= Vepi* r*2*h 4- Az=2* pi*xr* (r + h) Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 21 Remember Example? * Develop MATLAB code to find Cylinder volume and surface area. radius height ¢ Assume radius of 5m and height of 13 m. V=ar-h A = 2nr* + 2nrh = 2nr(r +h) Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 22 Be ware... Script File names MUST begin with a letter, and may include digits and the underscore character. Script File names should NOT: — include spaces — start with a number — use the same name as a variable or an existing command If you do any of the above you will get unusual errors when you try to run your script. You can check to see if a command, function or file name already exists by using the exist command. Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 25 Running .m files ¢ Run sequence of commands by typing =| 2”_™c¥t ner 5 mycylinder h = 13 in the command V = . 1.0210e+003 window A= ¢ Make sure the current 565.4867 folder is set properly Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 26 When you type mycylinder When multiple commands have the same name in the current scope (scope includes current file, optional private subfolder, current folder, and the MATLAB path), MATLAB uses this precedence order: 1. Variables in current workspace: Hence, if you create a variable with the same name as a function, MATLAB cannot run that function until you clear the variable from memory. N Nested functions within current function » Local functions within current file Functions in current folder a . Functions elsewhere on the path, in order of appearance Precedence of functions within the same folder depends on file type: 1. MATLAB built-in functions have precedence 2. Then Simulink models 3. Then program files with .m extension Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 27 Exercise # Editor - D:\EE 201 Computer Applications\Book Chapters\Lecture3 Arrays and Script Files\ter File Edit Text Go Cell Tools Debug Desktop Window Help NSE SRO SD-| Meh |e - ARBAal BOB | - [10 [+ | = la |x | | @ 1 $ temperature.m Convert the boiling point for 2 $ water from degrees Celsius (C) to Farenheit (F) 3 % Author: Dr. Mohammed Hawa 4 5 $ Convert freezing point of water 6- Cc #100 7- FSC * 9/5 + 32 Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 30 Header comments >> help temperature temperature.m Convert the boiling point for water from degrees Celsius (C) to Farenheit (F) Author: Dr. Mohammed Hawa >> temperature Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 31 Simple User Interaction: I/O ¢ Use input command to get input from the user and store it in a variable: h = input('Enter the height:') * MATLAB will display the message enclosed in quotes, wait for input and then store the entered value in the variable Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 32 oummary disp (A) Displays the contents, but not the name, of the array A. disp (‘text’) Displays the text string enclosed within quotes. x = input(’text’) Displays the text in quotes, waits for user input from the keyboard, and stores the value in x. x = input (’text’,’s’) Displays the text in quotes, waits for user input from the keyboard, and stores the input as a string in x. Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 35 Homework * The speed v of a falling object dropped with zero initial velocity is given as a function of time t by v = gt, where g is the gravitational acceleration. ¢ Plot v asa function of tfor0 «Kt « t,, where t,is the final time entered by the user. * Use a script file with proper comments. Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 36 Solution Plot speed of a falling object Author: Dr. Mohammed Hawa ale ol? g = 9.81; % Acceleration in SI units tf = input('Enter final time in seconds:'); t = [0:tf£/500:tf]; % array of 501 time instants 2 v = g*t; % speed plot(t,v); xlabel('t (sseconds)'); ylabel('v m/s)'); Copyright © Dr. Mohammed Hawa Electrical Engineering Department, University of Jordan 37
Docsity logo



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