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 Tutorial: Understanding Variables, Vectors, Matrices, and Plotting, Study notes of History

A tutorial on using MATLAB, focusing on defining variables, creating vectors and matrices, importing data, performing calculations, generating plots, and developing scripts. It covers topics such as managing variables, importing and exporting data, performing calculations, generating plots, and creating scripts. The tutorial also includes instructions on getting help and creating shortcuts.

Typology: Study notes

2010/2011

Uploaded on 08/28/2021

julius-iver
julius-iver 🇦🇺

1 document

1 / 16

Toggle sidebar

Related documents


Partial preview of the text

Download MATLAB Tutorial: Understanding Variables, Vectors, Matrices, and Plotting and more Study notes History in PDF only on Docsity! Lab Exercise 1: Using MATLAB for Control Systems (Part 1) Objectives: This lab provides an introduction to MATLAB in the first part. The lab also provides tutorial of polynomials, script writing and programming aspect of MATLAB from control systems view point. List of Equipment/Software Following equipment/software is required: * MATLAB Category Soft-Experiment Deliverables A complete lab report including the following: ¢ Summarized learning outcomes. ¢ MATLAB scripts and their results should be reported properly. Part I: Introduction to MATLAB Objective: The objective of this exercise will be to introduce you to the concept of mathematical programming using the software called MATLAB. We shall study how to define variables, matrices etc, see how we can plot results and write simple MATLAB codes. MATLAB TUTORIAL Page 1 Topics i = Introduction = MATLAB Environment = Getting Help = Variables = Vectors, Matrices, and Linear Algebra = Plotting Introduction ye What is MATLAB ? + MATLAB is @ computer program that combines computation and visualization power that makes it partcularly useful tool for engineers. + MATLAB is an executive program, and a script can be made with a list of MATLAB commands like other programming language. » MATLAB Stands for MATrix LABoratory. + The system was designed to make matrix computation particularly easy. > The MATLAB environment allows the user to: + manage variables + import and export data + perform calculations + generate plots + develop and manage files for use with MATLAB, MATLAB Environment ‘start MATLAB: START [] PROGRAMS [] MATLAB 6.5 [J MATLAB 65 (Or shortcut creation/activation on the desktop Page 5 fran ctors = = Arow vector n MATLAB can be created by an explct list, starting with a let bracket, fentering fe values separated by spaces (or commas) and dosing the vector with a fight bracket ‘A column vector can be created the same way, and the rows are separated by semicolons. Example 00.251 05°pi 0.75%pI pi) 00,7854 1.5708 2.3562 3.1416 x is a row vector. 0, 0.25%pk O.5"pl 075i pi) y is a column vector. ples" rs (con't...) Vector Addressing — A vector element is addressed in MATLAB with an integer index enclosed in parentheses. = Example: 2x3 1578 [] 3“ element of vector x + The colon notation may be used to address a block of elements. (tat = increment : end) ‘tat fs the stating inde, increment isthe amount to add to each suecessive index, and fend isthe ending index. A shoriened format (tart: end) may be used finerement is. + Example: >>x(29) 00785415705] 1 to 3"elements of vector x NOTE: MATLAB index starts at 1. Vectors (con't...) Some useful commands: counting By ane, ending at en increment ending et ar before ns ‘end, having number elements Page 8 atrices i 1 A Matix aray is two-dimensional, having both multiple rows and multiple ‘edumns, simfar tovecter arrays: * itbegins wit (, and end with} * spaces or commas are used to separate elements in a row + semicolon or enter is used w separate rows. +Bxample: Aisan mxn matrix, pe f=[123;456) " 122 a yy the main diagonal Matrices (con't Magic Function + Forexample you can generate a matrix by entering >> m=magic(4) It generates a matrix whose elements are such that the sum of all ‘elements in its rows, columns and diagonal elements are same = Sum Function + Youcan vetify the above magic square by entering >> sum(m) + Forrows take the transpose and then take the sum >> sum(m’) + Diag + Youcan get the diagonal elements of a matrix by entering >> dediag(m) >> sum(d) iets “tices (con't Matric Addrossing matroname(row, column) colon may be used in place of raw or column reference o select the entre ow or column + Example: recall: >> 1(2,3) f= 6 >> ha) h TT 1 wR oN wou Qe ean Page 9 atrices (con't ) Some useful commands: 2eros(n) retums a nxn matrix of zeros zeros(m.n) returns am xn matrix of zeros cones(n) retums a nxn matrix of ones ‘ones(m.n) returns am xn matrix of ones rand(n) retums a nxn matrix of random number rand(m.n) returns a m xn matrix of random number size (A) for am xn matrix A, returns the row vector [m,n] containing the number of ows and columns in matrix length(a) returns the larger of the number of rows oF columns in A. ) pen (ee (con't commands Transpose bee Teentity Matric ‘eye(n)(ratums an nxn dertty matric ‘eye(m,n) [returns an m x n matrix withones on the main diagonal and zeros elsewhere, Faaition ane ‘subtraction ‘Scalar Muliplcation [B= A where Ts scalar Matrix Multiplication | C= AB Matrix Ierse Bm inv(A), A must be 8 square matiixin ths Case. rank (A) [[retums the rank of the matrix A, Matrix Powers Bm AZ [[equares each element In the matte mA +A [lcomputes AYA, and A must be a square matrix Determinant ‘et (A), and A must be a square matrix Array Operations ‘Scalar-Array Mathematics For addition, subtraction, multiplication, and division of an array by a ‘scalar simply apply the operations to all elements of the array. = Example: >>f=[12;34] f= 12 peg=et Each element in the array fis o 1a multiplied by 2, then subtracted 57 by 1. Page 10 Waifs? toting (con't...) ‘cama toma anton) recent a ates sale hearse, ner le & ie ™ ss es yame ene X= Linspace (0, 30,260) % plotting the potymansat: fagure (2) subptee (2,2,2), plor C9) file (-Potynomial, Linear/Linear scate' ) ylabet Cy"), gi subplot (2,2,2), semitowe (x,y); Elete (Poiynenat, Log/tinesr” sate’) Ylabet Cy"), grid Eutptot (2,2, 3), sent togy (x,y) Eitte (-Potynamiat, Linear” tog scale’); xlabel(%"), ylabet Cy"), Eitpiot (22,4), togtog (x y) tite (-Potynamiat, Log/t scale’); xlabel(%"), ylabet Cy"), onus Plotting (con't...) ‘Adding new curves to the existing graph: ‘+ Use the hold command to add lines/points to an existing plot fold on retain existing axes, add new curves to curent axes ‘Ares are resesled when necessary. tld off rlease the current figure window for new plots = Grids and Labels cama asian oe Tog 9is gr sais [son STOR ESAT Additional commands for plotting Exercise 1: Use Matlab command to obtain the following a) Extract the fourth row of the matrix generated by magic(6) b) Show the results of ‘x’ multiply by ‘y’ and ‘y’ divides by ‘x’. Given x = [0:0.1:1.1] and y = [10:21] c) Generate random matrix ‘r’ of size 4 by 5 with number varying between -8 and 9 Exercise 2: Use MATLAB commands to get exactly as the figure shown below x=pi/2:pi/10:2*pi; y=sin(x); Z=cos(X); Bin Curve 1 + T + a5 Fed > he Foo + +4 o +, + O85 scene wPoeugeo cectetsbessrensdagetiiecsseal : 4 a “| i i i i Pi ee i 16 2 25 #3 36 4 46 6 66 6 65 Angle Cos Cune 1 r 05 a Ope 05 es wy \ i PO i i i i 16 2 265 #3 38 4 45 §& 55 6 BS Angle EXERCISE 1: a) Extract the fourth row of the matrix generated by magic(6) Screenshot: (@ Editor - E:\MATLAB\exercises\Exercise|_part_a.m @x { Exercise_partam %] + | 1- ele; w 2- @ = magic(6): 3- minagicé = a; 4- disp('The original magic(6)"); 5- disp(a); 6- magic4=minagicé(4,:); T- disp('extracted fourth row of magic (6): '")+ 8- disp (magic4); a Output: Command Window © The original magic(é) 35 1 6 26 1s 24 3 32 7 21 23 25 31 3 2 22 27 20 8 28 33 17 10 is 30 5 34 12 14 16 4 36 29 13 18 11 extracted fourth row of magic (€): 8 28 33 17 lo 15 fe >> Code: clc; a = magic(6); minagic6 = a; disp('The original magic(6)'); disp(a); magic4=minagic6(4,:); disp('extracted fourth row of magic (6): '); disp(magic4) ; PART II: Use MATLAB commands to get exactly as the figure shown below xX=pi/2:pi/10:2*pi; y=sin(x); Z=cos(X); Screenshot: 7 oe Exercise!_part_llm ca el 2- x=[(pi/2):(pi/10): (2*pi)]: — a= yrsin (x); 47 z=cos (x); |= figure('Name','Sine and Cos Curve','NumberTitle', 'off') 6- subplot (2,1,1) Tl plot (x,y, 'bi+', 'linewidth',2); Bl legend('Sine'); B\= title('Sin Curve') 10 - ylabel('Sin(x)") cn xlabel('Angle') 12 - grid; 13 - subplot (2,1,2) 14 - plot (x,z, 'r--+','linewidth',2); cl title('Cos Curve'); 16 - legend('Cosine"); ly- ylabel ("Cos (x) ") 18 — xlabel('Angle') 1g - grid; Output: {@ Sine and Cos Curve = a x File Edit View Insert Tools Desktop Window Help a oea@as|a\o Sin Curve Sin(x) Cosi(x) Code: clc; x=[(pi/2):(pi/10):(2*pi)]; in(x) ; Z=cos(X); figure('Name', 'Sine and Cos Curve','NumberTitle', 'off') subplot(2,1,1) plot(x,y, 'b:+','linewidth',2); legend('Sine'); title('Sin Curve') ylabel('Sin(x)') Xlabel('Angle') grid; subplot(2,1, 2) plot(x,z, 'r--+!','linewidth',2); title('Cos Curve'); legend('Cosine'); ylabel('Cos(x)') Xlabel('Angle') grid;
Docsity logo



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