1 / 8

ENGR 1320 Final Review - Programming

ENGR 1320 Final Review - Programming. Major Topics: Functions and Scripts Vector and Matrix Operations in Matlab Dot product Cross product Plotting in Matlab Decision Making Loops For While Input methods Menu input Output methods Disp Fprintf. Functions and Scripts.

varden
Télécharger la présentation

ENGR 1320 Final Review - Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ENGR 1320 Final Review - Programming • Major Topics: • Functions and Scripts • Vector and Matrix Operations in Matlab • Dot product • Cross product • Plotting in Matlab • Decision Making • Loops • For • While • Input methods • Menu • input • Output methods • Disp • Fprintf

  2. Functions and Scripts • What is the difference between a function and a script file in MATLAB? • A script is simply a file that contains a series of commands • A function has inputs and outputs. • To call the function from the command window, you must supply the input values, and the function will return the output values. • For example the ‘sqrt()’ function will take the square root of the input, and return the value as the output. • Example problem: Write a function in MATLAB that returns the roots of a quadratic equation. • Inputs: a,b,c of the quadratic formula • Outputs: roots • How would you make this a script? • See Function Example and Functions in MATLAB for proper coding of functions

  3. Vector operations in MATLAB • Vectors and Matrices are handled in the same way in MATLAB. • To create a vector or matrix, use square brackets • A = [1 2 3; 4 5 6; 7 8 9] • Spaces or commas separate columns • Semicolons separate rows • You must enter data row-by-row • To retrieve elements of a matrix, you can specify the value you want with row-column notation • A(3,2) • A(:,1) • To take dot or cross products in MATLAB, use the cross() and dot() functions. (Type help cross or help dot for correct usage) • To multiply matrices, use the * operator. • Matrices must be correctly sized, or the operation will return an error • To find an inverse of a matrix, use the inv() function. • Example problem: write a function to find the angle between 2 vectors • Hint: the norm() function finds the magnitude of a vector • Example 2: solve the matrix equation:

  4. Plotting in MATLAB • Use the plot() command to plot data in an x vector against data in a y vector • Additional plot commands: • title • xlabel • ylabel • legend • Example problem: plot the two vectors • x = [0:1:10] • y = x.^2 • What does the period before the ^ operator do? • See Matlab Basics

  5. User input and output • MATLAB has 2 main ways of prompting a user for information • input() • menu() • To output data, you will either plot results or display information to the command window • fprintf() • disp() • use fprintf() to display formatted results and numerical values • See input examples and output examples

  6. Decision Making • MATLAB has several logical operators • == • ~= • && • || • > • < • <= • >= • These operators compare two things and return either a ‘1’ if the statement is true or a ‘0’ if the statement is false • Examples: • a+b == b+a returns a ‘1’ • 3+3 == 4+3 returns a ‘0’ • a+b == b+a && 3+3 == 4+3 returns ? • MATLAB can make decisions based on the truth of an expression • If/then statement • Code will execute if the logical expression is true, or be skipped if the expression is false • Example problem: Write a code that has the user input a number between 1 and 10 and returns a statement to the screen that tells the user whether the number greater or less than 5. • See Decision Making Screencast

  7. Loops • We studied two methods for making code execute multiple times: for loop and while loop • the for structure will loop a set number of times for i=1:n code to execute n times end • the while structure will loop until a condition is met while (logical expression) code to execute until the logical expression is false end • See loops for the proper syntax • Example problem: Write a function that has an input of a single number n and returns a vector [1 2 3 4 … n] using a ‘for’ loop. • Example problem 2: What is the value of ‘factorial’ at the end of the execution of this code: counter = 5; factorial= 1; while(counter > 0) factorial= factorial * counter; %Multiply counter = counter - 1; %Decrement end factorial

  8. Study Strategy • Exam problems will be similar to homeworks • Several problems have been revisited in this class: • Electric circuits, truss equations, etc… • Look over the first 2 exams for representative problems (particularly the 2st for matlab-related problems) • Refer to the MATLAB book for help

More Related