matlab workshop 1/10/07 lesson 1: matlab as a graphing calculator

20
Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Upload: hortense-owens

Post on 01-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Matlab Workshop 1/10/07 Lesson 1:

Matlab as a graphing calculator

Page 2: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Lesson 1 focuses on basic use of Matlab as a graphing calculator. In this lesson you will:

•Use Matlab as a simple calculator•Understand the main views in the Matlab application: (Command Window, Workspace, Command History, Array Editor, Editor)•Write simple scripts using the editor•Use Cell Mode to run and debug scripts•Publish and document the results

Page 3: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Overview

Page 4: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Example 1.1: Defining a variable in Matlab.

Page 5: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Things to explain:

•Where is the command window?•How do you interpret what is in the workspace?•What happens if you leave out the semicolon?•What do variables represent?•Do spaces matter?•Is Matlab case sensitive (i.e., is x different from X)?•Do line breaks matter?•How do I continue a command on the next line?

Type the following in the command window:

x = 3;

Page 6: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Example 1.2: Calculating a simple formula.

Page 7: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Find the value of the following polynomial at x = 3:

Things to explain:

•Why do I need to put * between 5.2 and x?•Why is y underlined in red?

Type the following in the command window:

y = 4.1*x*x*x - 5.2*x - 2;

Page 8: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Example 1.3: Using functions and predefined constants in formulas.

Page 9: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Find the volume (V) of a cylinder with a radius (r) of 2.5 and a height (h) of 7.3. The formula for the volume of a cylinder is:

Things to explain:

•Where did pi come from?•Why not volume = pi*r*r*h?•What is power?•How do I find out what other built-in functions Matlab has?

Type the following in the command window:

r = 2.5; h = 7.3; V = pi*power(r,2)*h;

Page 10: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Example 1.3: Plotting the graph of a function

Page 11: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Graph the following function on the interval 0 to 5.

Things to explain:

•How do I see what kind of values x and y hold?•Is 1x100 different from 100x1?•What are the actual values that x and y hold?•How do I specify the spacing between the values in x?•What happens if I leave the x out of the plot command?•How can I make x into a vector when it used to just be an integer (Example 1.1).

Type the following in the command window:

Page 12: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

x = linspace(0, 5); y = 4.1*power(x, 3) - 5.2*x -2; plot(x, y);

Page 13: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Example 1.4: Writing a script and using cell mode.

Page 14: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Redo Examples 1.2 and 1.3 as a script file and debug in Cell Mode.

Page 15: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Exercises

Page 16: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Now try these exercises. Create a new script called Lesson1Exercises.m When you and your partner have completed the exercises, place your card in front of your computer. If you have questions raise your hand.

1. Graph the following on the interval [0, 5]:

Page 17: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Summary of syntax

Page 18: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

•Lines with comments start with %•Line breaks matter. Use three dots (...) to continue on the next line•Matlab is case sensitive•Variables can be reassigned to any type•A variable designated as a x b double has a rows and b columns

Page 19: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Summary of strategies

Page 20: Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator

Here are some summary strategies for working with Matlab:

•Develop Matlab code in small steps•Use Cell Mode to debug each step•Try simple numbers on each thing first•Watch the types of the variables in the workspace as you work•Don't move code to its own function until you've thoroughly debugged it in Cell Mode•Document each small piece as you go•Use the Matlab publish feature to document you're work.•Keep the documentation updated or you will have no idea what you have after a few months