0% found this document useful (0 votes)
49 views

Lecture Notes 1

This document provides an introduction to MATLAB. It discusses getting started with MATLAB including the command window, current folder, workspace, editor, and command history. It covers defining and using variables, basic arithmetic, strings, logical expressions, and if/else statements. It includes two exercises - writing a script to convert temperatures and compare two user-input numbers. The overall purpose is to familiarize users with MATLAB's basic functionality and programming concepts.

Uploaded by

boomerang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Lecture Notes 1

This document provides an introduction to MATLAB. It discusses getting started with MATLAB including the command window, current folder, workspace, editor, and command history. It covers defining and using variables, basic arithmetic, strings, logical expressions, and if/else statements. It includes two exercises - writing a script to convert temperatures and compare two user-input numbers. The overall purpose is to familiarize users with MATLAB's basic functionality and programming concepts.

Uploaded by

boomerang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Lecture 1: Hello, MATLAB!

Math 98, Fall 2018

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 1 / 22


Syllabus
Instructor: Eric Hallman
Class Website: https://math.berkeley.edu/~ehallman/98-fa18/
Login: !cmfmath98
Password: c@1analog (?)
See website for
1 Textbooks
2 Lecture schedule
3 Homework
4 Links to MATLAB documentation
1 Language fundamentals:
http://www.mathworks.com/help/matlab/
language-fundamentals.html
2 Programming Scripts and Functions
http://www.mathworks.com/help/matlab/
programming-and-data-types.html

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 2 / 22


Syllabus

Labor Day is a holiday, so when should we have Lecture 2?


1 Wednesday, 8/29
2 Friday, 8/31
3 Wednesday, 9/5
4 Monday, 10/8

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 3 / 22


Getting Started with MATLAB

Obligatory first program:

>> disp("Hello, world!")


Hello, world!

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 4 / 22


Getting started with MATLAB

Matlab has five features:


1 Current Folder shows the files that MATLAB is accessing. By
default 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 in
your 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, Fall 2018 Lecture 1: Hello, MATLAB! 5 / 22


Current Folder

pwd prints the current working directory


dir lists its contents
cd DIR-NAME can change the directory
I cd .. (double dots) moves up one level
I You can also do this by clicking the buttons over “Current Folder”.

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 6 / 22


Command Window

Among other things, it’s a giant calculator.



Operations: +, −, ∗, /,ˆ·, exp(·), ·, log(·)

2 − 3 ∗ (1 + 2)/2 = 2 − 3 1+2
2

log(4) = 1.3863, log2(4) = 2, log10(4) = .6021

pi= 3.1416, pi∗42 = 50.2655

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 7 / 22


Variables

The workspace shows variables that have been defined in the current
session. In particular, ans is by default the value of the last arithmetic
computation we made. We can check the value of a variable by entering
its name in the command window.
>> ans
ans =
50.2655

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 8 / 22


Variables
We can define our own variables, too! Variable names must start with a
letter and can contain letters, digits, and underscores. MATLAB is case
sensitive but all built-in MATLAB functions use lowercase letters, so if you
use at least one capital letter in your variable names you can be sure to
avoid 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, Fall 2018 Lecture 1: Hello, MATLAB! 9 / 22


Variables

Use a semicolon to suppress the output of a command. Using disp will


suppress 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 (...) and
SHIFT-ENTER to continue a line.
>> sqrt(5 + 7 + ...
13)
ans =
5

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 10 / 22


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, Fall 2018 Lecture 1: Hello, MATLAB! 11 / 22


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 of
instructions for the computer. The name of the file must have the format
filename.m. For MATLAB to execute the file, it must be saved in the
Current Directory.

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 12 / 22


input

A string is a sequence of characters enclosed by single or double


quotations, e.g. ’teststring123x’.
The command input will prompt the user to input a number or string. If
the input should be a string, then use

input(’instructions for input: ’, ’s’)

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 13 / 22


fprintf

The command fprintf is used to display formatted strings that


incorporate 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 for
floating 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, Fall 2018 Lecture 1: Hello, MATLAB! 14 / 22


Commenting with %

Except within a string, everything following % is a comment. Comments


do not get executed when the program runs, but can make the code easier
to read by providing information about its organization and usage.
Comments in the beginning lines of a program will be revealed when using
the command help:

>> help hello


It’s a program that says hello.

help (as well as doc) is also invaluable when learning how to use various
MATLAB functions.

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 15 / 22


Exercise 1

A temperature can be converted from Fahrenheit to Celsius using the


formula c = (5/9)(f − 32), where c is Celsius and f is Fahrenheit.
Write a script called conversion.m that prompts the user for a
temperature in Fahrenheit, converts it to Celsius, and prints it in a nice
format.

Math 98, Fall 2018 Lecture 1: Hello, MATLAB! 16 / 22


Problem

A quadratic polynomial has the form f (x) = x 2 + bx + c. We would like


to 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 R
3 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, Fall 2018 Lecture 1: Hello, MATLAB! 17 / 22


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, Fall 2018 Lecture 1: Hello, MATLAB! 18 / 22


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, Fall 2018 Lecture 1: Hello, MATLAB! 19 / 22


Boolean Expressions
A boolean expression is any expression involving relations or logical
statements:
((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, Fall 2018 Lecture 1: Hello, MATLAB! 20 / 22


if-else

This construct is used where the decision to execute one or another set of
computations 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, Fall 2018 Lecture 1: Hello, MATLAB! 21 / 22


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, Fall 2018 Lecture 1: Hello, MATLAB! 22 / 22

You might also like