Master MATLAB Fundamentals with Michael Tanguay

Slide Note
Embed
Share

Explore the significance of MATLAB, programming etiquette, and how to create complex programs independently. Gain insights into using MATLAB efficiently and making your tasks easier. Engage in hands-on activities to enhance your learning experience.


Uploaded on Sep 27, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. MATLAB Fundamentals Michael Tanguay

  2. Objectives LEARN THE IMPORTANCE OF MATLAB COMPREHEND PROGRAMMING ETIQUETTE GAIN THE BASICS TO BE INDEPENDENT AND CREATE MORE COMPLEX PROGRAMS UTILIZE AND INCORPORATE MATLAB TO MAKE YOUR LIFE EASIER

  3. Overview 1. The Importance of Programming a. Matrix operations a. Feasibility i. Dot operator b. Automation b. Indexing/Accessing/Slicing c. Marketability 7. Plotting 2. What is MATLAB? 8. User-Defined Functions 3. Creating a Program 9. Loops and Conditional Statements a. Overlay (CMD, Editor) a. For loops and if Statements b. Initializations and Declarations b. Nesting i. Variable and Filename Nomenclature 10. Symbolic Toolkit and Live script 4. Mathematical Operations 11. Examples 5. Built-in Functions 12. Next Steps 6. Vectors and Matrices

  4. Student Requirements We will be learning today. Please utilize your thinking caps and open MATLAB on your devices. Get the most out of your time. Please take notes. We will be doing some independent/collaborative programming. Participate!!!

  5. 1. The Importance of Programming Solve the following linear system of equations:

  6. 1. The Importance of Programming Now solve this linear system:

  7. 1. The Importance of Programming Programming Hand Calculations Use of loops or simply recalling a program Only make a program once. Plug and Chug Carries over a bunch of precision (Though a computer could only store so much, machine epsilon, truncation) Tell the program to plot it for you Repetition in Calculations Exhaustive Calculations Complex steps involve Time consuming Human error Hand plotting

  8. 1. The Importance of Programming Feasibility Want something done fast, right, and easy Automation Have the program do the work for you Marketability MATLAB is a tool and a skill

  9. 2. What is MATLAB? MATLAB is a programming platform designed specifically for engineers and scientists. Mathworks MATLAB is short for MATrix LABoratory Higher level language, object oriented, top-down and left to right. Ideal for its user-friendly interface for plotting and numerical computations Contains a plethora of toolboxes designed for specified fields such as optimization and partial differential equations

  10. 3. Creating a Program - Overlay MATLAB overlay command window, workspace, editor %----------------------------------------------------------------------------------% See ProgramExample1_Hello_World.m CONGRATULATIONS!!! YOU MADE YOUR FIRST PROGRAM!!! %----------------------------------------------------------------------------------% Syntax Learned: Comments ( % ) Allows user to make comments on code Functions Learned: disp() Displays text

  11. 3. Creating a Program Etiquette is vital to the overall understanding of the user and the programmer themselves. Utilizing comments, organizations, and nomenclature are the trifecta to a good and recallable program. Imagine you created a program 15 years ago that you worked so arduously to understand only to not understand how the program works later! Don t waste your own time. See ExampleProgram0_Etiquette.m

  12. 3. Creating a Program NOMENCLATURE - I Definitions: Variables => (x = 2), x is the variable Rules: Variables and program file names cannot have: A number for the first place. Ex) 10ten = 10 or 10program.m Special characters like !, $, #. Ex) hello! = 3 or program$.m Spaces. Ex.) number 1 = 1 or program 1.m Variables can have: An underscore for spaces instead. Ex.) number_1 = 1 or program_1.m Numbers anywhere besides the first place. Ex) h1 = 10 or p10rogram_10.m

  13. 3. Creating a Program NOMENCLATURE - II Rules: Variables and program names are case sensitive. Ex) Hello_1 is not the same as hello_1 Variables are always on the left side. Ex) x = 2 is good and 2 = x is bad!

  14. 4. Mathematical Operations +, -, *, /,^ Ex.) y = 2+2 => 4 , x = 10/2 => 5 , z = x + y => 9 PEMDAS!!! Definitions: = is called the assignment operator. It assigns everything on the right to the left side.

  15. NOTE In real life we convert words into numbers and statements that do something. The whole point of programming is being able to translate physical meaning into programming instructions. Gaining the ability to break down processes into simpler, bite-sized steps is the key to efficient programming.

  16. 5. Built-In functions help command Search documents Function lists Google!!! Ex.) sin(), sind(), sqrt(), round(), ceil(), plot(), xlabel() An excellent reference for all of MATLAB Functions: http://matlab.izmiran.ru/help/techdoc/ref/

  17. 6. Vectors and Matrices Definitions: Transpose ( ) interchanges rows for columns and columns for rows Row vector (1 x n) is 1-D information. Ex.) z = [ 1, 2 , 3, 4] or [1 2 3 4] Column vector (n x 1) is 1-D information. Ex.) z = [ 1, 2 , 3, 4] or [1;2;3;4] or [1 2 3 4]

  18. 6. Vectors and Matrices Dot operator (.) multiplies like elements together but keeps it in matrix form(Does not return a scaler. This is different from the dot product) Must be the same dimensions. Indexing Can access an element of a matrix to reassign (manipulate). Slicing - Accessing part of a matrix to alter (uses colons and numbers ex (1:5, 1:5)) end accesses the last component of a matrix : - means all columns or rows.

  19. 7. Plotting The reason why we need matrices. Particularly vectors. Plotting requires 2D data an x and y. Must be the same length! Uses the plot() function Endless customization Ex.) Plot x = [1:0.01:20] and y = sin(x)

  20. 8. User Defined Functions You can make your own functions that when given input will run. Can connect to your main program. Increases the overall efficiency of your program. Requires that the user defined function and the main program be on the same file path.

  21. 9. Loops and Conditional Statements Loops are commonly used to populate matrices or do repetitive tasks in an iterative manner. Some loops: for, while Nesting - You can have loops inside loops inside loops inside loops inside loops . Conditional Statements check if a given statement is true or not before proceeding. ==, >=,<=, ~= (This is different from the assignment operator!) Some conditional statements: if-else, switch (though not commonly used) The break command is often utilized to break out of loops if a conditional statement is satisfied.

  22. 10. Symbolic Toolkit Live Script Allows you to solve variables without assigning values to them. Really handy when it comes down to solving extremely difficult equations.

  23. 11. Examples The power of MATLAB!!! Fluxin1Dslab.m Sinewaveapproximation.m Nodes.m | Evaluationfunc.m

  24. 12. Next Steps Think of it like going to the gym Consistency = results Going to the gym = using MATLAB to do your calculations or for anything Challenge yourself = use more built-in functions more complex programs, etc Eat right = use proper programming etiquette (commenting, clear, clc, close all, organizations) LET'S GET THEM GAINS!!!! P.S The first thing you could do is review everything. I will be sending these slides and the MATLAB codes in a compressed file through email

  25. Questions?

  26. Because you attended this workshop, if you want your name entered in a drawing for a Book Scholarship at the end of the Fall semester, please complete our feedback: https://ess.unm.edu/events/event-feedback.html

  27. Practice Problems

  28. 4. Mathematical Operations Sample Problem #1 Assign 4 to x then set y equivalent to the product of x and 20. Display only y and z to the command window. Have the program also display This was fun! See ProgramExample2_Math.m

  29. 5. Built-In Functions Sample Problem #2 Set x to 45 and let y represent sin(x) and z represent sind(x). How come they have different values? See ProgramExample3_Built_Functions.m

  30. Create two Matrices A for the left matrix and B for the right (refer to the figure on the slide). Multiply Matrices A and B to attain result. Now transpose the result. Add that result to C = [1 1 2; 2, 3, 4] and assign to D. Now take the dot product C and D and assign to E. Have the answer display only the first 3 components. 6. Vectors and Matrices Sample Problem #3 See ExampleProgram4_Matrices.m

  31. 7. Plotting Sample Problem #4 Create a 1x20 row vector using the rand() function and assign to x. x = 1:20, y = rand(). Plot. ExampleProgram5_Plot.m

  32. Create a function that ties into a main program. Have the main program be titled as ExampleProgram6_User_Functions.m and the function it is calling Program6_Function.m. In the function set z to x +12 and require that it has an input of x. In the main program call the function and pass 6 into the function (this means x=6 to the function program) and set it equal to R. Now have the main program display R. To see the full benefit of user defined functions change x to 12 and run the program, then 24. 8. User-Defined Functions Sample Problem #5

  33. The ultimate sample problem!!! Create a function that ties into a main program. Have the main program be titled as ExampleProgram7_Loops.m and the function it is calling Program7_Function.m. Create a function that requires the input of the size of a square matrix, n. In the function have a loop fill a n by n vector with the first component as 1 and the second as 2 and so on. Prior to the end of the matrix, implement an if statement where if the matrix is on its last component set it equal to 12. In the main program pass a value for n into the function and set the function call equal to A. Now create a vector of ones the size of n and set the vector to b. Take the inverse of A and multiply with b. Do you have to transpose b? 9. Loops and Conditional Statements Sample Problem #6

  34. 10. Symbolic Toolkit Live Script Sample Problem #7 Declare a, b, c, and d as symbols and set y = ax^2 + bx + c. Use the solve function to find y = 0. Is the result familiar?

More Related Content