Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

Similar documents
Lecture 1: Hello, MATLAB!

Math 98 - Introduction to MATLAB Programming. Fall Lecture 2

Python Programming Exercises 1

An Introduction to MATLAB

Introduction to Programming for Biology Research

Matlab as a calculator

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

CS 221 Lecture. Tuesday, 13 September 2011

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

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

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

MATLAB Project: Getting Started with MATLAB

CS 112: Intro to Comp Prog

MATLAB Project: Getting Started with MATLAB

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

MATLAB TUTORIAL WORKSHEET

Odd-Numbered Answers to Exercise Set 1.1: Numbers

Full file at

Chapter 7: Programming in MATLAB

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

Polynomial and Rational Functions

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

Introduction to Java Applications

Math Interim Mini-Tests. 3rd Grade Mini-Tests

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

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

1 Introduction to Matlab

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

McTutorial: A MATLAB Tutorial

Files and File Management Scripts Logical Operations Conditional Statements

Introduction to MATLAB

APPM 2460 Matlab Basics

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:

Lab 1 Intro to MATLAB and FreeMat

Lecture 3 Tao Wang 1

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

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

MAT 275 Laboratory 1 Introduction to MATLAB

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Matlab Primer. Lecture 02a Optical Sciences 330 Physical Optics II William J. Dallas January 12, 2005

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

MATLAB - Lecture # 4

Dr Richard Greenaway

Chapter 3: Functions and Files

Relational and Logical Statements

Mathematical Experiments with Mathematica

17 USING THE EDITOR AND CREATING PROGRAMS AND FUNCTIONS

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

may be sent to:

MAT137 Calculus! Lecture 31

Getting To Know Matlab

Relational and Logical Operators

Python lab session 1

Expressions and Variables

Introduction to MATLAB

Today s topics. Announcements/Reminders: Characters and strings Review of topics for Test 1

Teaching Manual Math 2131

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

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

Lecture 3 Programming in MATLAB. Dr. Bedir Yousif

User Defined Functions

Math Homework 3

Fundamentals: Expressions and Assignment

MATLAB Lecture 1. Introduction to MATLAB

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Linear Algebra and MATLAB

Digital Image Processing

ADD Identifier-1 TO Identifier-2 Literal-1. ADD Identifier-1. GIVING Identifier-2 Literal-1

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

MATLAB. Devon Cormack and James Staley

MATLAB Introduction to MATLAB Programming

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

To start using Matlab, you only need be concerned with the command window for now.

MATLAB Introductory Course Computer Exercise Session

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

1 Overview of the standard Matlab syntax

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

Some elements for Matlab programming

Computer Project: Getting Started with MATLAB

LAB: INTRODUCTION TO FUNCTIONS IN C++

Finding, Starting and Using Matlab

Introduction to MATLAB

Matlab Programming Introduction 1 2

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

Copyright 2006 Melanie Butler Chapter 1: Review. Chapter 1: Review

The purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about

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

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Boolean Logic & Branching Lab Conditional Tests

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

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Chapter 1 Introduction to MATLAB

Introduction to Computer Programming for Non-Majors

AN INTRODUCTION TO MATLAB

HW 4 HINTS. 1. Chapter 6

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Introduction to the C++ Programming Language

Introduction to MATLAB

Transcription:

Syllabus Instructor: Chris Policastro Class Website: https://math.berkeley.edu/~cpoli/math98/fall2016.html See website for 1 Class Number 2 Oce hours 3 Textbooks 4 Lecture schedule slides programs

Syllabus Class Website: https://math.berkeley.edu/~cpoli/math98/fall2016.html See website for 1 Homework grading policy schedule assignments solutions Homework Submission: https://bcourses.berkeley.edu See bcourses site for 1 Suggestions and hints on the homework and project. 2 Homework must be submitted by 11:59pm

Syllabus Class Website: https://math.berkeley.edu/~cpoli/math98/fall2016.html See website for links to the documentation 1 Language fundamentals: http: //www.mathworks.com/help/matlab/language-fundamentals.html syntax operators data types array indexing and manipulation 2 Programming Scripts and Functions http://www.mathworks.com/help/matlab/ programming-and-data-types.html

Getting started with MATLAB MATLAB has ve features: 1 Command Window is a programmable calculator with broad capabilities. Here we can perform calculations, dene variables, debug and much more. 2 Editor allows us to write collections of commands and save them as M-les. 3 Current Directory shows the les that MATLAB is accessing. By default MATLAB cannot execute les contained in other folders. Use pwd to check current directory Use dir to check contents 4 Workspace is a MAT-le containing the variables you have dened in your session. 5 Command History Can be accessed using up arrow. It should be closed in the interface.

Lecture 1: Turning formulas into programs 1 Problems (problem_1_1) Surface area of a sphere (Chapter 1.1 van Loan) (problem_1_2) Minimum of quadratic polynomial on an interval (Chapter 1.2 van Loan) 2 Concepts variable, initialization, arithmetic, string conditional, logical expression, boolean expression

(problem_1_1) Surface area of a sphere The formula for the surface area of a sphere of radius r is A(r) = 4πr 2 For large values of r, the numbers r and r 2 dier by a large amount. The factor r 2 in the formula implies that a small change in r (call it δr) aects a large change in surface area (call it δa) We want to study δa as a function of δr to illustrate rounding errors through comparison of the (approximate) formulas 1 4π(r + δr) 2 4πr 2 2 4π(2r + δr) δr 3 8πr δr for δa.

Arithmetic Operations: +,,, /,ˆ. (2 3 (5 4)) 4/5 = 4 (2 3(5 4)) 5 1/2/3/4 = ((1/2)/3)/4. Order of operations goes parentheses, division, multiplication, addition/subtraction 2 + 3, 2 3, 2/3, 2 3,2 3,2 1/3 log 2 3, π 3

Command line Use a semicolon to suppress the output of command. Multiple commands can be placed on a line separated by commas or semicolons. sqrt(5); abs( 3), sign( 10); Use SHIFT-ENTER to start a new line. Use ellipses (... ) and SHIFT-ENTER to continue a line. Ellipses cannot be inserted anywhere e.g. sqrt(5... );

Variables The value of an arithmetic expression is saved as ans. ans is a variable whose value is stored in the Workspace. The Workspace is an example of a MAT-le, which is a special le that contains MATLAB data. Variables can be dened by Variable_Name = Value for example r=6367 This is called initializing a variable. MATLAB is case sensitive. However all built-in MATLAB functions use lower case letters. Therefore try to use at least one capital letter to avoid conict (e.g. Abs not abs)

Example 1 %%%S u r f a c e A r e a.m%%% 2 % 3 % Computes s u r f a c e a r e a o f s p h e r e o f r a d i u s r 4 % User i s prompted to i n p u t r 5 6 r=i n p u t ( ' E n t e r r a d i u s r : ' ) ; 7 A=4 p i r ^2; 8 f p r i n t f ( ' r = %10.3 f A = %10.3 e \n ', r, A) ;

Scripts and m-les A script is a short computer program, that is, a procedure implemented as a sequence of instructions for the computer. The order of the instructions is important. An m-le is a le containing a script for MATLAB. The name of the le must have the format filename.m. For MATLAB to execute the le, it must be saved in the Current Directory.

input A string is a sequence of characters enclosed by single quotation e.g. `stringexample&)' The command input will prompt the user to input a number or string. If the input should be a string, then use input('exampleinstructions ','s')

fprintf The command fprintf is used to display formatted strings that incorporate specied values fprintf('formattedstring',listofvariables) The formats are %d for integer, %e for scientic notation, and %f for decimal format. fprintf('temperature = %4d Pressure = %5.9f \n',t,p) \n is to start a new line.

Commenting with % Except within a string, everything following % is a comment. Comments facilitate the reading of code by providing information about the organization. Comments should always appear at the beginning of a document. Providing comments is called documenting. Using explanatory le names and variable names helps with documenting. temp for a temporary variable not auehh125

Review of problem_1_1 There are two sources of error in the calculation from problem_1_1. 1 π is an irrational number meaning its decimal expansion continues without repeating. A computer can only process irrational numbers through approximation by rational numbers. 2 Even rational numbers are rounded in MATLAB. Think of a calculator rendering 1 divided by 3 as.3333. This error is carried through a calculation. The sources of error account for 1 The dierence between method 1 and method 2 2 The nearness of method 1 and method 3 Note the large change in surface area from small change in radius. Compare to the surprising fact that increasing circumference by 2πδ means increasing the radius by δ.

exercise_1_1 A temperature can be converted from the Fahrenheit scale into the Celsius scale using the formula c = (5/9)(f 32) where c is Celsius and f is Farenheit Write a script that prompts the user for a temperature in Farhenheit, converts it to Celsius and prints it.

(problem_1_2) Minimum of quadratic polynomial on an interval A quadratic polynomial has form f(x) = x 2 + bx + c. It attains minimum over R at x = b/2. On an interval [L, R] the minimum may be attained at one of the end points. We want to write a program that will 1 prompt the user for a quadratic polynomial x 2 + bx + c 2 prompt the user for an interval [L, R] 3 compare the values of b/2, L and R 4 print the location of the minimum on the interval For this we need to know about conditional statements.

Relations The following statements take value 0 or 1 a<b a less than b a>b a less than or equal to b a<=b a greater than or equal to b a>=b a greater than or equal to b a==b a equal to b a = b a not equal to b

Logical statements 1 and(a,b) equivalently a & b 2 or(a,b) equivalently a b 3 not(a) 4 xor(a,b) What do the commands && and do?

boolean expression A boolean expression is any expression involving relations or logical statements ((4 <= 100) ( 2 >= 7))& (0.5 == 1e16) A boolean expression takes the values 1 (for true) and 0 (for false). Note that 0 and 1 are just number; there does not exist a separate class for them. The order of operations for evaluations of boolean expressions is 1 negation 2 relations (<, =,...) 3 and 4 or

if-else The construct is used in situations where the decision to choose between a pair of alternative computations is based upon a boolean expression 1 i f b o o l e a n e x p r e s s i o n 2 3 command to be e x e c u t e d when e x p r e s s i o n i s t r u e 4 5 e l s e 6 7 command to be e x e c u t e d when e x p r e s s i o n i s not t r u e 8 9 end See van Loan and Fan for a variant on the construction using if-elseif-else

Review of problem_1_2 Just as arithmetic comes with rules such as 1 order or operations 2 association 3 distribution... manipulations with boolean expressions have rules such as 1 order or operations 2 association 3 distribution... By taking to 0 to be false and 1 to be true, we can represent boolean expressions on a computer. These expressions are the ingredients of conditional statements, which execute commands based on the validity of boolean expressions.

exercise_1_1 A string can be inserted into another string using fprintf. The place holder for a string is %s. Take Str='MATH98'. fprintf('%s is a class',str) would print MATH98 is a class to the command window. Write a script that prompts the user for two numbers. Call them x and y. Use an if-else statement to determine whether x equals y. Output either The numbers are equal or The numbers are not equal to the command window.