Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
Lecture 1: Hello, MATLAB!
Math 98, Spring 2019
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 1 / 23
Syllabus
Instructor: Eric HallmanClass Website: https://math.berkeley.edu/~ehallman/98-fa18/
Login: !cmfmath98 (?)Password: c@1analog (?)See website for
1 Textbooks
2 Lecture schedule
3 Homework4 Links to MATLAB documentation
1 Language fundamentals:http://www.mathworks.com/help/matlab/
language-fundamentals.html2 Programming Scripts and Functions
http://www.mathworks.com/help/matlab/
programming-and-data-types.html
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 2 / 23
Syllabus
1 6 homeworks, due each Thursday 11:59pm, 2pts each
2 One final project, due 3/22, 6 pts
3 Upload all assignments to bCourses
4 12/18 points to pass
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 3 / 23
Getting Started with MATLAB
Obligatory first program:
>> disp("Hello, world!")
Hello, world!
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 4 / 23
Getting started with MATLAB
Matlab has five features:
1 Current Folder shows the files that MATLAB is accessing. Bydefault MATLAB cannot execute files contained in other folders.
2 Command Window is what we just used to say “Hello, world!”.Here we can define variables, perform calculations, and much more.
3 Workspace is a MAT-file containing the variables you have defined inyour session.
4 Editor allows us to save collections of commands as M-files.
5 Command History can be accessed using the up arrow.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 5 / 23
Current Folder
pwd prints the current working directory
dir lists its contents
cd DIR-NAME can change the directoryI cd .. (double dots) moves up one levelI You can also do this by clicking the buttons over “Current Folder”.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 6 / 23
Command Window
Among other things, it’s a giant calculator.Operations: +,−, ∗, /, ·̂, exp(·),
√·, log(·)
2− 3 ∗ (1 + 2)/2 = 2− 31+22
log(4) = 1.3863, log2(4) = 2, log10(4) = .6021
pi= 3.1416, pi∗42 = 50.2655
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 7 / 23
Variables
The workspace shows variables that have been defined in the currentsession. In particular, ans is by default the value of the last arithmeticcomputation we made. We can check the value of a variable by enteringits name in the command window.
>> ans
ans =
50.2655
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 8 / 23
Variables
We can define our own variables, too! Variable names must start with aletter and can contain letters, digits, and underscores. MATLAB is casesensitive but all built-in MATLAB functions use lowercase letters, so if youuse at least one capital letter in your variable names you can be sure toavoid any name conflicts.
>> x1 = 5.337
>> my variable = "howdy"
>> frodoBaggins33 = sqrt(2)*pi
>> radius = 4
>> pi*radius^2
ans =
50.2655
Do your best to choose informative names for your variables.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 9 / 23
Variables
Use a semicolon to suppress the output of a command. Using disp willsuppress the ans = text, but will also not save the output to ans.Multiple commands can be placed on a line separated by semicolons.
>> x = 5; y = 6; disp(x+y)
11
Use SHIFT-ENTER to start a new line. Use ellipses (...) andSHIFT-ENTER to continue a line.
>> sqrt(5 + 7 + ...
13)
ans =
5
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 10 / 23
Variables
Use clear to clear all variables from the workspace. Use clear VAR1
VAR2 to clear specific variables VAR1 and VAR2.
>> x = 5; clear x;
>> x
Undefined function or variable ’x’.
Use clc to clear the command line.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 11 / 23
Editor
Okay, it’s finally time to make a proper script! In the command line, enter
>> edit Hello.m
An m-file is a file containing a script for MATLAB—a sequence ofinstructions for the computer. The name of the file must have the formatfilename.m. For MATLAB to execute the file, it must be saved in theCurrent Directory.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 12 / 23
input
A string is a sequence of characters enclosed by single or doublequotations, e.g. ’teststring123x’.The command input will prompt the user to input a number or string. Ifthe input should be a string, then use
input(’instructions for input: ’, ’s’)
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 13 / 23
fprintf
The command fprintf is used to display formatted strings thatincorporate specified values.
>> T = 123.4511; P = 121.471;
>> fprintf(’Temperature = %6.2f, Pressure = %5.3e\n’, T, P)
Temperature = 123.45, Pressure = 1.215e+02
>>
The formats are %d for integer, %e for scientific notation, and %f forfloating point format. %s is used to include strings (rather than numbers),and the character \n adds a new line to the end of the output.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 14 / 23
Commenting with %
Except within a string, everything following % is a comment. Commentsdo not get executed when the program runs, but can make the code easierto read by providing information about its organization and usage.Comments in the beginning lines of a program will be revealed when usingthe command help:
>> help hello
It’s a program that says hello.
help (as well as doc) is also invaluable when learning how to use variousMATLAB functions.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 15 / 23
Exercise 1
A temperature can be converted from Fahrenheit to Celsius using theformula c = (5/9)(f − 32), where c is Celsius and f is Fahrenheit.Write a script called conversion.m that prompts the user for atemperature in Fahrenheit, converts it to Celsius, and prints it in a niceformat.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 16 / 23
Problem
A quadratic polynomial has the form f (x) = x2 + bx + c . We would liketo know whether it has any roots in the interval [L,R].We want to write a program that does the following:
1 prompts the user for real numbers b and c
2 prompts the user for real numbers L and R3 determine whether the polynomial f (x) has any real roots at all
I if it does, determine whether it has any roots in [L,R]I if not, say so
In order to write this program, we will need a few more tools.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 17 / 23
Relations
The following statements will take value 0 (if false) or 1 (if true)
a < b: a less than b
a > b: a greater than b
a <= b: a less than or equal to b
a >= b: a greater than or equal to b
a == b: a equal to b (note the doubled equals sign!)
a ∼= b: a not equal to b
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 18 / 23
Logical Statements
and(a,b) or equivalently a & b
or(a,b) or equivalently a | b
not(a)
xor(a,b)
What do the commands && and || do?
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 19 / 23
Boolean Expressions
A boolean expression is any expression involving relations or logicalstatements:
((4 <= 100)|(−2 > 5))&(true| ∼ false)
Boolean expressions evaluate to 1 for true and 0 for false.
>> 5 + true
ans =
6
The order of operations is as follows:
1 negation
2 relations
3 and
4 or
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 20 / 23
if-else
This construct is used where the decision to execute one or another set ofcomputations depends on the value of a boolean expression.if this boolean expression is true
execute these commands
elseif this second expression is true instead
then exectue these other commands
else
do this if those earlier conditions are false
end
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 21 / 23
Exercise 2
Write a script that prompts the user for two numbers (call them x and y).Write a script that outputs The numbers are equal if x = y and The
numbers are not equal otherwise.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 22 / 23
for
Quick intro to for loops:
for i = 1:15
j = i + 5;
fprintf("%2d + 5 = %2d\n", i, j);
end
Very handy for executing similar pieces of code many times over.
Math 98, Spring 2019 Lecture 1: Hello, MATLAB! 23 / 23