Starting With Matlab - Washington State University



4704588695616Learning MATLABRabayet SadnanWashington State Univ.OutlineStarting with MatlabArrays and MatricesPlotsProgramming with MatlabUser Defined FunctionsPolynomials and Curve FittingSymbolic MathBook Ref: Matlab, An Introduction With Applications Book by Amos GilatStarting With MatlabFolderScript/ EditorWorkspaceCommand WindowStarting With Matlab- WindowsWindows – Figure Window1744979229938Windows – Editor/Script Window13121631713764134611-764778Windows – Command WindowNotes for working in the Command Window:To type a command, the cursor must be placed next to the command prompt ( >> ).Once a command is typed and the Enter key is pressed, the command is executed.However, only the last command is executed. Everything executed previously (that might be still displayed) is unchanged.Several commands can be typed in the same line. This is done by typing a comma between the commands.When the Enter key is pressed, the commands are executed in order from left to right.It is not possible to go back to a previous line that is displayed in the Command Window, make a correction, and then re-execute the command.If a command is too long to fit in one line, it can be continued to the next line bytyping three periods ... (called an ellipsis) and pressing the Enter key. The continuation of the command is then typed in the new lineSemicolon (;) - If a semicolon ( ; ) is typed at the end of a command, the output of the command is not displayedTyping (%) - Comment‘clc’ – Clear Command Window2365269275451Arithmetic Operations -Some Notes –Variable start with letterCase sensitiveNo spaces are allowedNo built-in function’s name as variablePredefined Variables – ‘ans’, ‘pi’, ‘inf’, ‘eps’ (2^-52), ‘i’, ‘j’, ‘NaN’Useful Commands –ClearClear x y zClfClose4455422448404268723795323Creating Vector/Array with constant spacing:928106140461966206218247What happens for m:n ?2575941173439390601167790116291551860446Creating Vector/Array with Specific Number:Multidimensional Array/Matrices:1298447141794Special Commands:136702719754539044882169896077711188033Array OperationTranspose –Array addressing –Using colon –Adding elements in Matrices –3665220373740Deleting elements –Array Operations–Mul/Div :Element wise – A .* B or A ./ BMatrix operation - A * B or A / B & A \ BInv: B = inv (A) Determinant: B = det (A)Solve Linear eqn –Array Operations – Built-in CommandsB = mean (A) B = max (A)[pos B] = max (A)B = min (A)[pos B] = min (A)B = sum (A) B = sort (A)dot (A, C) cross (A, C)2240302-159205Plot Command179680413623817967861988661507265396240018348951534967421880-502607690104-289247307580502951Plot a function – 3.5x2 + 5x , from x = -12 to x =126774180214249Multiple plots in same graph – plot (x,y, a,b, c,d)6893052-2272807153656-1571767472171-289764plot (x,y) hold on; plot (a,b) hold on; plot (c,d)812291562409625448266405Multiple plots in multiple subplots –Subplot command:subplot( row , column, pos)subplot( 3, 2 , 2) plot (x,y)Formatting Plot using commands –17236442076385661668457574Special Plots –bar , pi, stairs, stem etc.6884503-13858bar(x,y) , barh (x,y), stairs (x,y), stem (x,y), pie (x)1159763219552393649195146368022621390232If-else , loops, logical operator7004279935968If-else , loop, logical operator2996183-2113For loop2996183-74847While loopProblem ----A user-defined function is a MATLAB program that is created by the user, saved as a function file, and then used like a built-in function.The function can be a simple single mathematical expressionOr a complicated and involved series of calculations.In many cases it is a subprogram within a computer program.Hmax( v2 )0=2gHow to create a user-defined function?Open a new scriptFollow the basic structure2075688131290Basic Structure:23197272205171437132823677Saving the file: SAME AS FUNCTIONS NAME Problem:1.OC to F, andIn cases when the value of a relatively simple mathematical expression has to be calculated many times within a program, MATLAB provides the option of using anonymous functions.An anonymous function is a user-defined function that is defined and written within the computer code (not in a separate function file) and is then used in the code.An anonymous function is a simple (one-line) user-defmed function that is defined without creating a separate function file (m-file).13619751975956550152224361 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download