Lecture 1: Hello, MATLAB!

Similar documents
Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

An Introduction to MATLAB

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Why use MATLAB? Mathematcal computations. Used a lot for problem solving. Statistical Analysis (e.g., mean, min) Visualisation (1D-3D)

Introduction to Programming for Biology Research

MATLAB - Lecture # 4

Files and File Management Scripts Logical Operations Conditional Statements

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

Python Programming Exercises 1

Python lab session 1

MATLAB Introduction to MATLAB Programming

Introduction to MATLAB

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Step by step set of instructions to accomplish a task or solve a problem

Introduction to Engineering gii

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

Computer Programming in MATLAB

Numerical Analysis First Term Dr. Selcuk CANKURT

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Dr Richard Greenaway

CME 192: Introduction to Matlab

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Introduction to MATLAB Programming

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

CS 221 Lecture. Tuesday, 13 September 2011

7 Control Structures, Logical Statements

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

Lecture 2 Introduction to MATLAB. Dr.Tony Cahill

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Programming in Mathematics. Mili I. Shah

MATLAB Introduction To Engineering for ECE Topics Covered: 1. Creating Script Files (.m files) 2. Using the Real Time Debugger

MATLAB TUTORIAL WORKSHEET

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Top bar items. Current folder should be shown. Command Window This has the the prompt >>

Introduction to MATLAB

Finding, Starting and Using Matlab

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

MATLAB Project: Getting Started with MATLAB

Matlab Examples. (v.01, Fall 2011, prepared by HP Huang)

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Full file at

McTutorial: A MATLAB Tutorial

MATH 2650/ Intro to Scientific Computation - Fall Lab 1: Starting with MATLAB. Script Files

PART 1 PROGRAMMING WITH MATHLAB

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

MATLAB Project: Getting Started with MATLAB

Teaching Manual Math 2131

Getting To Know Matlab

MATLAB. Devon Cormack and James Staley

MATLAB BASICS. M Files. Objectives

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

CS 112: Intro to Comp Prog

Fundamentals of Programming CS-110. Lecture 2

Algorithms and Programming I. Lecture#12 Spring 2015

3+2 3*2 3/2 3^2 3**2 In matlab, use ^ or ** for exponentiation. In fortran, use only ** not ^ VARIABLES LECTURE 1: ARITHMETIC AND FUNCTIONS

Matlab- Command Window Operations, Scalars and Arrays

Linear Algebra and MATLAB

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Hello, World and Variables

LECTURE 1. What Is Matlab? Matlab Windows. Help

Numerical Methods for Civil Engineers

An Introduction to MATLAB. Lab tutor : Dennis Yang LIU Lab 1: Sept. 11, 2014

Lab 1 Intro to MATLAB and FreeMat

Programming in MATLAB

Introduction to MATLAB

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Built-in Types of Data

HW DUE Floating point

Matlab Programming MET 164 1/24

MBI REU Matlab Tutorial

CHAPTER 3 BASIC INSTRUCTION OF C++

MATLAB Introductory Course Computer Exercise Session

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Computational Modelling 102 (Scientific Programming) Tutorials

Section 1: Numerical Calculations

Lecture 1 arithmetic and functions

WHAT IS MATLAB?... 1 STARTING MATLAB & USING THE COMMAND LINE... 1 BASIC ARITHMETIC OPERATIONS... 5 ORDER OF OPERATIONS... 7

Digital Image Processing

WINTER. Web Development. Template. PHP Variables and Constants. Lecture

17 USING THE EDITOR AND CREATING PROGRAMS AND FUNCTIONS

MATLAB The first steps. Edited by Péter Vass

2.0 MATLAB Fundamentals

Matlab Programming Introduction 1 2

Programming 1. Script files. help cd Example:

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

Worksheet 1. Hello Matlab

INTRODUCTION TO NUMERICAL ANALYSIS

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Software and Programming 1

Programming for Experimental Research. Flow Control

Chapter 3: Programming with MATLAB

Course Layout. Go to follow instr. Accessible within campus (only for the first download)

Transcription:

Lecture 1: Hello, MATLAB! Math 98, Spring 2018 Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 1 / 21

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, Spring 2018 Lecture 1: Hello, MATLAB! 2 / 21

Getting Started with MATLAB Obligatory first program: >> disp("hello, world!") Hello, world! Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 3 / 21

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, Spring 2018 Lecture 1: Hello, MATLAB! 4 / 21

Current Folder pwd prints the current working directory dir lists its contents cd DIR-NAME can change the directory cd.. (double dots) moves up one level You can also do this by clicking the buttons over Current Folder. Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 5 / 21

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 4 2 = 50.2655 Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 6 / 21

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

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, Spring 2018 Lecture 1: Hello, MATLAB! 8 / 21

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, Spring 2018 Lecture 1: Hello, MATLAB! 9 / 21

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

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

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, Spring 2018 Lecture 1: Hello, MATLAB! 12 / 21

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, Spring 2018 Lecture 1: Hello, MATLAB! 13 / 21

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, Spring 2018 Lecture 1: Hello, MATLAB! 14 / 21

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 that prompts the user for a temperature in Fahrenheit, converts it to Celsius, and prints it in a nice format. Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 15 / 21

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 if it does, determine whether it has any roots in [L, R] if not, say so In order to write this program, we will need a few more tools. Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 16 / 21

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

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

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. Note that 0 and 1 are just numbers and are not in a separate class for logicals. >> 5 + true ans = 6 The order of operations is as follows: 1 negation 2 relations 3 and 4 or Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 19 / 21

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, Spring 2018 Lecture 1: Hello, MATLAB! 20 / 21

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