5. Intro to Matlab

# 5. Intro to Matlab - Introduction to Matlab Numbers...

This preview shows pages 1–9. Sign up to view the full content.

©2009 by L. Lagerstrom Introduction to Matlab Numbers Operators The Command Window Chaining operations Syntax errors Precedence of operations Variables and assignment statements Mathematical functions Punctuation: semi-colons, commas, and ellipses

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
©2009 by L. Lagerstrom Numbers in Matlab Numbers in Matlab may be entered in either fixed point or floating point format. Fixed point examples: 2.639, -827, 0.000023. Floating point examples: 4.39e5, 4.39E5, 245.8e-2. Note that "e" or "E" here stands for power-of-10 exponent and does NOT refer to the exponential function e x . In other words, 4.39e5 in Matlab means (4.39)(10 5 ) in regular math notation. Remember also that we call the 4.39 part the "mantissa." (We'll learn how to use the exponential function a little later.)
©2009 by L. Lagerstrom Math Operators Matlab has the usual math operators: • Addition: 5 + 8 • Subtraction: 9 - 4 • Multiplication: 5*7 (i.e., an asterisk) • Division: 56/8 (i.e., a slash) • Exponentiation: 7^2 (i.e., a caret symbol; note that 7^2 is 7 squared) In addition to the regular / operator for division, there is also a \ (backslash) operator, called the "left division" operator. (And the regular / operator is the "right division" operator.) When we write 8\56 it means "8 divided into 56." That is, 8\56 is the same as 56/8. (The reason for the left division operator will become clear when we cover solving systems of linear equations.)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
©2009 by L. Lagerstrom The Matlab Command Window When you start Matlab, the first window you will usually see is the "Command Window" with a ">>" prompt. You may enter math expressions at the prompt and then press the Enter key to have Matlab execute them. The results will be displayed as shown below. (The "ans" stands for "answer.") Examples: >>5 + 8 ans = 13 >>3*7 ans = 21
©2009 by L. Lagerstrom Interpreting Our Examples It's very important to understand what's going on in an example like that below, so that you don't get confused with our later examples. The ">>5 + 8" line means that we have typed "5 + 8" at the >> prompt in the Matlab Command window. (Sometimes we just call it the "command prompt".) We then press the Enter key to enter that expression, causing Matlab to execute it. After it executes it, Matlab displays the result (the "ans = 13" part). >>5 + 8 ans = 13

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
©2009 by L. Lagerstrom A Note re. Decimal Places Later we will cover in detail how Matlab displays numbers regarding number of decimal places, etc. For now, just understand that if the result is an exact integer, then it is displayed as such with no decimal part. If it's not an exact integer, then the decimal part is displayed up to four decimal places, rounded appropriately. (This is known as "format short.") >>5 + 8 >>9/18 ans = ans = 13 0.5 >>5/8 >>2/3 ans = ans = 0.625 0.6667
©2009 by L. Lagerstrom Chaining Operations Math operations may be chained together. Examples: >>5 + 8 - 3 ans = 10 >>3 * 7 - 10 + 4 ans = 15 Note that Matlab ignores white space in expressions. That is, writing 5+8 is the same as 5 + 8.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
©2009 by L. Lagerstrom Syntax Errors If we write an expression incorrectly (e.g., an error in the syntax), Matlab will try to point out the location of the error and display an error message.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern