Download presentation

Presentation is loading. Please wait.

Published byJane Spencer Modified over 4 years ago

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 functionsFunction ExampleFunctions in MATLAB

3
Vector operations in MATLAB

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 BasicsMatlab 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 examplesinput examplesoutput 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 ScreencastDecision 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 syntaxloops 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 2 st for matlab- related problems) Refer to the MATLAB book for help

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google