Physics 326G Winter Class 6

Size: px
Start display at page:

Download "Physics 326G Winter Class 6"

Transcription

1 Physics 36G Winter 008 Class 6 Today we will learn about functions, and also about some basic programming that allows you to control the execution of commands in the programs you write. You have already used several of Matlab s built-in functions, so you know roughly how they work. Every function is really a program written in an M-file. A function takes a number of arguments, which are input values that you give it, does something useful, then, usually, gives an output or return value. For example, when you do a = sin(x); x is the argument of the function SIN. The function calculates the sine of x and returns the result of that calculation, which here we have assigned to the variable a. Matlab functions can have many arguments and many return values. One big difference between functions and script files is that the variables used in a function are not accessible outside of that function. The exceptions to this are the variables you pass to the function as arguments, and the variables which the function gives back to you as output. (There is another exception, which we won t worry about here you can explicitly define variables to be global. This is useful sometimes but can also be dangerous.) Let s write a simple function. Open a new M-file in the Editor. The first line of a function must identify it as a function (rather than a script): function pressure = ideal_gas(density,temperature) Our function s name is ideal_gas. It takes two input arguments: variables called density and temperature. It has one output value, namely pressure. Note that in general any of these arguments could be arrays or strings; they don t have to be single numbers. The rest of the function is the series of Matlab commands we want to execute. The only thing we have to remember is to assign a value to the output variable, pressure. It is useful to make the first lines of a function after the definition line a series of comments that tells what the function does. Then you can use HELP with your own functions! Here is the complete function: function pressure = ideal_gas(moles,temperature) % This function takes the number of moles in a % litre of gas and % the temperature (in Kelvin) as inputs % and calculates the pressure using the ideal gas law. % It has been written to work with arrays, but moles and temperature % have to have the same length % check to see that the input arrays are the same length

2 if (length(moles) ~=length(temperature)), disp('error: input arrays must be the same length!') pressure = 0; return R = 8.057e-; % gas constant, liter atm/mol K % the ideal gas law is PV = nrt % where n is the number of moles of gas and R is the gas constant. % n/v is the number of moles per liter % so P = R*moles*temperature % note the dot-times in the line below, since we are working with arrays pressure = R*moles.*temperature; There are a few things to note here. The return statement s execution of the function. You don t need one at the of the function, since if Matlab gets to the of the function file it will return (stop) automatically anyhow. Early in the program we have an if statement. This allows you to do different things, deping on the value of a particular variable. In this case, the program checks the length of the input arrays. If they are not the same, it prints an error message and stops. If they are the same, it continues with the calculation. IF is an example of a control statement that allows you to control the execution of the program. More on this very soon. Type in this program and save it in a file ideal_gas.m. (You should always save functions in a file with a name that is the same as the function name; this avoids much confusion.) Try it out. Check that the HELP command works with your program. One thing you will need to do often is compare two quantities. Matlab has several relational operators that let you do this. They include > greater than < less than <= less than or equal to >= greater than or equal to = = equal to ~= not equal to The first four are obvious. Note that one equals sign alone is used to assign a value to a variable. To compare two quantities, you use two equals signs. Remember this, because it is very easy to make very confusing mistakes. The tilde ~ means not, so ~= is not equal to. The result of a relational operation is either 1 (meaning true) or 0 (meaning false). These operators work element-by-element on arrays of equal size. Type a =3;

3 b=4; then a<b which should give 1 (the relationship is true) a>b which should give 0 (the relationship is false). Now try it with arrays: c = [ ]; d = [ ]; Try all of the relational operators on these two arrays, and make sure you understand what happens. Matlab has a number of logical operators: & and or ~ not a&b (a AND b) returns a 1 (true) if both a and b are true, that is, if a and b are non-zero. a b (a OR b) returns a 1 (true) if either a or b or both are true, that is, if at least one of a and b is non-zero. ~a returns 1 if a is 0 (false), and returns 0 if a is non-zero (true). Logical operators also work on arrays. All of these operations can be used in conjunction with an IF statement to control program flow. IF statements allow Matlab to make decisions about what to deping on the result of a conditional test. For example if a>b, disp( a is greater than b ) The comma at the of the IF statement is optional; sometimes it helps to make the statement more readable. Every IF statement has to have an END statement associated with it. If the condition being tested is true, the statements after the IF up to the END are executed. If not, they are skipped. You can also use the ELSEIF statement to choose from among several possible outcomes: if a>b, disp( a is greater than b )

4 elseif a<b disp( a is less than b ) else disp( a is equal to b ) Write a script that takes two numbers from the keyboard, and then prints out which of the two is the biggest. Often you will want your program to execute a series of commands a given number of times. For example you might want to calculate something for a series of different values of a variable. You can do this with a FOR loop. Here is a silly example: for ii=1:10, fprintf( hello, this is step number ii=%d \n,ii) The statements between the FOR and the END are executed once for each element of the array specified in the FOR statement. The first time, the counter ii is set equal to the first element of the array, the second time, ii is set equal to the second element of the array, and so on. You can use any one-dimensional array; it does not have to be a sequence. For example, for ii=rand(1,10), fprintf( hello, is this step number ii=%f?\n,ii) It is worth pointing out that large loops in Matlab are usually very slow. Often you can eliminate FOR loops by taking advantage of Matlab s ability to do arithmetic with arrays all at one time, and you should do this whenever possible. For example if a aand b are arrays, it is better to write c = a+b; than for ii=1:length(a), c(ii) = a(ii)+b(ii); It takes some practice to use array arithmetic effectively but it is well worth it. In addition to gaining a few milliseconds because your program will run faster, your code will be shorter and easier to read, and you will make fewer mistakes. The Taylor series for sin(x) issin x = k k ( 1) x k = 0 ( k + ) + 1 1!. Write a function that takes as arguments the angle x in degrees and kmax, the number of terms to include in the sum. Your function should then convert x in to radians, then uses a FOR loop to calculate the sum up to k=kmax, and output

5 the result to the command window. Use a FOR loop to calculate the sum. The FACTORIAL function will be helpful. Matlab also has WHILE loops. These are useful when you know you want to iterate a loop, but you don t know at the outset how many iterations of the loop you will need. For example, you could modify the Taylor series program you wrote above so that it would keep going until the terms being added to the sum were smaller than some value epsilon. You could do something like: k = 0 total = 0; epsilon = 1e-5; term = 1 % start with a big number while term>epsilon, term =. total = total+term; k = k+1; In this example, the calculation will stop when the kth term is less than Instead of incrementing k automatically in the FOR loop, here you have to increment k in a separate line inside the WHILE loop because you don t know how many terms you will need. Each WHILE has to have an END associated with it, just as for FOR loops. The commands BREAK and CONTINUE are useful in loops. A BREAK command inside a loop will stop execution of that loop, the program will jump to the END statement of that loop and carry on from there. A CONTINUE statement will stop execution of the iteration of the loop that you are executing right now, and go on to the beginning of the next pass through the loop. Some practice problems: 1. Write a function called quadroots that finds the real roots of a quadratic equation ax + bx + c = 0. The coefficients a, b, and c should be arguments of the function. Arrange it so that if b 4ac> 0 then the program will print out a message saying that there are two real roots, then give the roots. If b 4ac= 0 it should print out a message saying that there is one real root, then give the root. Finally, if it is less than zero it should say that there are no real roots. Use your function to find the real roots of the following equations: x + 8x 3= 0 15x + 10x+ 5 = 0 18x + 1x + = 0. Write a program using a loop that calculates the sum of the first m terms of the series

6 m ( ) n= 0 n 1 1, then calculates the difference between the result and the exact result of π/4. n Modify the program you wrote in () so that it will keep increasing n until the difference between the sum and the exact result is less than 10-4.

Programming for Experimental Research. Flow Control

Programming for Experimental Research. Flow Control Programming for Experimental Research Flow Control FLOW CONTROL In a simple program, the commands are executed one after the other in the order they are typed. Many situations require more sophisticated

More information

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control COGS 119/219 MATLAB for Experimental Research Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control .m files We can write the MATLAB commands that we type at the command window

More information

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Using Monte Carlo to Estimate π using Buffon s Needle Problem An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Here s the problem (in a simplified form). Suppose

More information

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB MATLAB sessions: Laboratory MAT 75 Laboratory Matrix Computations and Programming in MATLAB In this laboratory session we will learn how to. Create and manipulate matrices and vectors.. Write simple programs

More information

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

Top bar items. Current folder should be shown. Command Window This has the the prompt >> MA1710: Week 2 for, if, else, break Getting started in session 2 With the Windows 7 operating system that we have in our Labs start Matlab as you did last week by clicking the mouse pointer on Start (bottom

More information

function [s p] = sumprod (f, g)

function [s p] = sumprod (f, g) Outline of the Lecture Introduction to M-function programming Matlab Programming Example Relational operators Logical Operators Matlab Flow control structures Introduction to M-function programming M-files:

More information

Chapter 7: Programming in MATLAB

Chapter 7: Programming in MATLAB The Islamic University of Gaza Faculty of Engineering Civil Engineering Department Computer Programming (ECIV 2302) Chapter 7: Programming in MATLAB 1 7.1 Relational and Logical Operators == Equal to ~=

More information

Physics 326 Matlab Primer. A Matlab Primer. See the file basics.m, which contains much of the following.

Physics 326 Matlab Primer. A Matlab Primer. See the file basics.m, which contains much of the following. A Matlab Primer Here is how the Matlab workspace looks on my laptop, which is running Windows Vista. Note the presence of the Command Window in the center of the display. You ll want to create a folder

More information

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

CSE/NEUBEH 528 Homework 0: Introduction to Matlab CSE/NEUBEH 528 Homework 0: Introduction to Matlab (Practice only: Do not turn in) Okay, let s begin! Open Matlab by double-clicking the Matlab icon (on MS Windows systems) or typing matlab at the prompt

More information

CS227-Scientific Computing. Lecture 3-MATLAB Programming

CS227-Scientific Computing. Lecture 3-MATLAB Programming CS227-Scientific Computing Lecture 3-MATLAB Programming Contents of this lecture Relational operators The MATLAB while Function M-files vs script M-files The MATLAB for Logical Operators The MATLAB if

More information

ENGR 1181 MATLAB 09: For Loops 2

ENGR 1181 MATLAB 09: For Loops 2 ENGR 1181 MATLAB 09: For Loops Learning Objectives 1. Use more complex ways of setting the loop index. Construct nested loops in the following situations: a. For use with two dimensional arrays b. For

More information

MATLAB TUTORIAL WORKSHEET

MATLAB TUTORIAL WORKSHEET MATLAB TUTORIAL WORKSHEET What is MATLAB? Software package used for computation High-level programming language with easy to use interactive environment Access MATLAB at Tufts here: https://it.tufts.edu/sw-matlabstudent

More information

Lecture Transcript While and Do While Statements in C++

Lecture Transcript While and Do While Statements in C++ Lecture Transcript While and Do While Statements in C++ Hello and welcome back. In this lecture we are going to look at the while and do...while iteration statements in C++. Here is a quick recap of some

More information

UNIT 8: SOLVING AND GRAPHING QUADRATICS. 8-1 Factoring to Solve Quadratic Equations. Solve each equation:

UNIT 8: SOLVING AND GRAPHING QUADRATICS. 8-1 Factoring to Solve Quadratic Equations. Solve each equation: UNIT 8: SOLVING AND GRAPHING QUADRATICS 8-1 Factoring to Solve Quadratic Equations Zero Product Property For all numbers a & b Solve each equation: If: ab 0, 1. (x + 3)(x 5) = 0 Then one of these is true:

More information

APPM 2460: Week Three For, While and If s

APPM 2460: Week Three For, While and If s APPM 2460: Week Three For, While and If s 1 Introduction Today we will learn a little more about programming. This time we will learn how to use for loops, while loops and if statements. 2 The For Loop

More information

7 Control Structures, Logical Statements

7 Control Structures, Logical Statements 7 Control Structures, Logical Statements 7.1 Logical Statements 1. Logical (true or false) statements comparing scalars or matrices can be evaluated in MATLAB. Two matrices of the same size may be compared,

More information

CS 221 Lecture. Tuesday, 11 October 2011

CS 221 Lecture. Tuesday, 11 October 2011 CS 221 Lecture Tuesday, 11 October 2011 "Computers in the future may weigh no more than 1.5 tons." - Popular Mechanics, forecasting the relentless march of science, 1949. Today s Topics 1. Announcements

More information

APPM 2460 Matlab Basics

APPM 2460 Matlab Basics APPM 2460 Matlab Basics 1 Introduction In this lab we ll get acquainted with the basics of Matlab. This will be review if you ve done any sort of programming before; the goal here is to get everyone on

More information

CSC 121 Spring 2017 Howard Rosenthal

CSC 121 Spring 2017 Howard Rosenthal CSC 121 Spring 2017 Howard Rosenthal Agenda To be able to define computer program, algorithm, and highlevel programming language. To be able to list the basic stages involved in writing a computer program.

More information

PHYS 210: Introduction to Computational Physics Octave/MATLAB Exercises 1

PHYS 210: Introduction to Computational Physics Octave/MATLAB Exercises 1 PHYS 210: Introduction to Computational Physics Octave/MATLAB Exercises 1 1. Problems from Gilat, Ch. 1.10 Open a terminal window, change to directory /octave, and using your text editor, create the file

More information

LECTURE 0: Introduction and Background

LECTURE 0: Introduction and Background 1 LECTURE 0: Introduction and Background September 10, 2012 1 Computational science The role of computational science has become increasingly significant during the last few decades. It has become the

More information

Dr Richard Greenaway

Dr Richard Greenaway SCHOOL OF PHYSICS, ASTRONOMY & MATHEMATICS 4PAM1008 MATLAB 2 Basic MATLAB Operation Dr Richard Greenaway 2 Basic MATLAB Operation 2.1 Overview 2.1.1 The Command Line In this Workshop you will learn how

More information

Numerical Methods Lecture 1

Numerical Methods Lecture 1 Numerical Methods Lecture 1 Basics of MATLAB by Pavel Ludvík The recommended textbook: Numerical Methods Lecture 1 by Pavel Ludvík 2 / 30 The recommended textbook: Title: Numerical methods with worked

More information

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB MAT 75 Laboratory Matrix Computations and Programming in MATLAB In this laboratory session we will learn how to. Create and manipulate matrices and vectors.. Write simple programs in MATLAB NOTE: For your

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 4: Programming in Matlab Yasemin Bekiroglu (yaseminb@kth.se) Florian Pokorny(fpokorny@kth.se) Overview Overview Lecture 4: Programming in Matlab Wrap Up More on Scripts and Functions Wrap Up Last

More information

Computational Finance

Computational Finance Computational Finance Introduction to Matlab Marek Kolman Matlab program/programming language for technical computing particularly for numerical issues works on matrix/vector basis usually used for functional

More information

2 Computation with Floating-Point Numbers

2 Computation with Floating-Point Numbers 2 Computation with Floating-Point Numbers 2.1 Floating-Point Representation The notion of real numbers in mathematics is convenient for hand computations and formula manipulations. However, real numbers

More information

2 Computation with Floating-Point Numbers

2 Computation with Floating-Point Numbers 2 Computation with Floating-Point Numbers 2.1 Floating-Point Representation The notion of real numbers in mathematics is convenient for hand computations and formula manipulations. However, real numbers

More information

4.0 Programming with MATLAB

4.0 Programming with MATLAB 4.0 Programming with MATLAB 4.1 M-files The term M-file is obtained from the fact that such files are stored with.m extension. M-files are alternative means of performing computations so as to expand MATLAB

More information

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple Chapter 2 Interactive MATLAB use only good if problem is simple Often, many steps are needed We also want to be able to automate repeated tasks Automated data processing is common in Earth science! Automated

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up More on Scripts and Functions Basic Programming Lecture 2 Lecture 3 Lecture 4 Wrap Up Last time Loading data from file: load( filename ) Graphical input and

More information

UNDERSTANDING PROBLEMS AND HOW TO SOLVE THEM BY USING COMPUTERS

UNDERSTANDING PROBLEMS AND HOW TO SOLVE THEM BY USING COMPUTERS UNDERSTANDING PROBLEMS AND HOW TO SOLVE THEM BY USING COMPUTERS INTRODUCTION TO PROBLEM SOLVING Introduction to Problem Solving Understanding problems Data processing Writing an algorithm CONTINUE.. Tool

More information

Branches, Conditional Statements

Branches, Conditional Statements Branches, Conditional Statements Branches, Conditional Statements A conditional statement lets you execute lines of code if some condition is met. There are 3 general forms in MATLAB: if if/else if/elseif/else

More information

MATH2070: LAB 3: Roots of Equations

MATH2070: LAB 3: Roots of Equations MATH2070: LAB 3: Roots of Equations 1 Introduction Introduction Exercise 1 A Sample Problem Exercise 2 The Bisection Idea Exercise 3 Programming Bisection Exercise 4 Variable Function Names Exercise 5

More information

EP375 Computational Physics

EP375 Computational Physics EP375 Computational Physics Topic 1 MATLAB TUTORIAL BASICS Department of Engineering Physics University of Gaziantep Feb 2014 Sayfa 1 Basic Commands help command get help for a command clear all clears

More information

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB MATLAB sessions: Laboratory MAT 75 Laboratory Matrix Computations and Programming in MATLAB In this laboratory session we will learn how to. Create and manipulate matrices and vectors.. Write simple programs

More information

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

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah) Introduction ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah) MATLAB is a powerful mathematical language that is used in most engineering companies today. Its strength lies

More information

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Programming Basics and Practice GEDB029 Decision Making, Branching and Looping Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Decision Making and Branching C language possesses such decision-making capabilities

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t. CS 221 Lecture Tuesday, 4 October 2011 There are 10 kinds of people in this world: those who know how to count in binary, and those who don t. Today s Agenda 1. Announcements 2. You Can Define New Functions

More information

Chapter 4 Branching Statements & Program Design

Chapter 4 Branching Statements & Program Design EGR115 Introduction to Computing for Engineers Branching Statements & Program Design from: S.J. Chapman, MATLAB Programming for Engineers, 5 th Ed. 2016 Cengage Learning Topics Introduction: Program Design

More information

H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block

H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block Chapter 4 Branching Statements and Program Design Programming 1: Logical Operators, Logical Functions, and the IF-block Learning objectives: 1. Write simple program modules to implement single numerical

More information

Lecture 1: Hello, MATLAB!

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

More information

Short Version of Matlab Manual

Short Version of Matlab Manual Short Version of Matlab Manual This is an extract from the manual which was used in MA10126 in first year. Its purpose is to refamiliarise you with the matlab programming concepts. 1 Starting MATLAB 1.1.1.

More information

OrbBasic Lesson 1 Goto and Variables: Student Guide

OrbBasic Lesson 1 Goto and Variables: Student Guide OrbBasic Lesson 1 Goto and Variables: Student Guide Sphero MacroLab is a really cool app to give the Sphero commands, but it s limited in what it can do. You give it a list of commands and it starts at

More information

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering 1 Introduction A computer program is a sequence of computer

More information

Practice Reading for Loops

Practice Reading for Loops ME 350 Lab Exercise 3 Fall 07 for loops, fprintf, if constructs Practice Reading for Loops For each of the following code snippets, fill out the table to the right with the values displayed when the code

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab Andreas C. Kapourani (Credit: Steve Renals & Iain Murray) 9 January 08 Introduction MATLAB is a programming language that grew out of the need to process matrices. It is used extensively

More information

Our Strategy for Learning Fortran 90

Our Strategy for Learning Fortran 90 Our Strategy for Learning Fortran 90 We want to consider some computational problems which build in complexity. evaluating an integral solving nonlinear equations vector/matrix operations fitting data

More information

OrbBasic 1: Student Guide

OrbBasic 1: Student Guide OrbBasic 1: Student Guide Sphero MacroLab is a really cool app to give the Sphero commands, but it s limited in what it can do. You give it a list of commands and it starts at the top and goes to the bottom,

More information

The MathType Window. The picture below shows MathType with all parts of its toolbar visible: Small bar. Tabs. Ruler. Selection.

The MathType Window. The picture below shows MathType with all parts of its toolbar visible: Small bar. Tabs. Ruler. Selection. Handle MathType User Manual The MathType Window The picture below shows MathType with all parts of its toolbar visible: Symbol palettes Template palettes Tabs Small bar Large tabbed bar Small tabbed bar

More information

An introduction to plotting data

An introduction to plotting data An introduction to plotting data Eric D. Black California Institute of Technology February 25, 2014 1 Introduction Plotting data is one of the essential skills every scientist must have. We use it on a

More information

Basics of Computational Geometry

Basics of Computational Geometry Basics of Computational Geometry Nadeem Mohsin October 12, 2013 1 Contents This handout covers the basic concepts of computational geometry. Rather than exhaustively covering all the algorithms, it deals

More information

Getting Started With Mathematica

Getting Started With Mathematica Getting Started With Mathematica First Steps This semester we will make use of the software package Mathematica; this package is available on all Loyola networked computers. You can access Mathematica

More information

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003 Math 64 - Scientific Computing - Matlab Intro and Exercises: Spring 2003 Professor: L.G. de Pillis Time: TTh :5pm 2:30pm Location: Olin B43 February 3, 2003 Matlab Introduction On the Linux workstations,

More information

(Refer Slide Time 3:31)

(Refer Slide Time 3:31) Digital Circuits and Systems Prof. S. Srinivasan Department of Electrical Engineering Indian Institute of Technology Madras Lecture - 5 Logic Simplification In the last lecture we talked about logic functions

More information

LOOPS. Repetition using the while statement

LOOPS. Repetition using the while statement 1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while

More information

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as 1 Matlab Primer The purpose of these notes is a step-by-step guide to solving simple optimization and root-finding problems in Matlab To begin, the basic object in Matlab is an array; in two dimensions,

More information

Math 230 Final Exam December 22, 2015

Math 230 Final Exam December 22, 2015 Math 230 Final Exam December 22, 2015 General Directions. This is an open- book, open- notes, open- computer test. However, you may not communicate with any person, except me, during the test. You have

More information

How to succeed in Math 365

How to succeed in Math 365 Table of Contents Introduction... 1 Tip #1 : Physical constants... 1 Tip #2 : Formatting output... 3 Tip #3 : Line continuation '...'... 3 Tip #4 : Typeset any explanatory text... 4 Tip #5 : Don't cut

More information

(Refer Slide Time: 01:12)

(Refer Slide Time: 01:12) Internet Technology Prof. Indranil Sengupta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture No #22 PERL Part II We continue with our discussion on the Perl

More information

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

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain Introduction to Matlab By: Dr. Maher O. EL-Ghossain Outline: q What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flow Control

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB The Desktop When you start MATLAB, the desktop appears, containing tools (graphical user interfaces) for managing files, variables, and applications associated with MATLAB. The following

More information

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name: 1 Matlab Tutorial 1- What is Matlab? Matlab is a powerful tool for almost any kind of mathematical application. It enables one to develop programs with a high degree of functionality. The user can write

More information

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie 21-Loops Part 2 text: Chapter 6.4-6.6 ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie While Loop Infinite Loops Break and Continue Overview Dr. Henry Louie 2 WHILE Loop Used to

More information

Algebra 2 Common Core Summer Skills Packet

Algebra 2 Common Core Summer Skills Packet Algebra 2 Common Core Summer Skills Packet Our Purpose: Completion of this packet over the summer before beginning Algebra 2 will be of great value to helping students successfully meet the academic challenges

More information

ECE 202 LAB 3 ADVANCED MATLAB

ECE 202 LAB 3 ADVANCED MATLAB Version 1.2 1 of 13 BEFORE YOU BEGIN PREREQUISITE LABS ECE 201 Labs EXPECTED KNOWLEDGE ECE 202 LAB 3 ADVANCED MATLAB Understanding of the Laplace transform and transfer functions EQUIPMENT Intel PC with

More information

The Fortran Basics. Handout Two February 10, 2006

The Fortran Basics. Handout Two February 10, 2006 The Fortran Basics Handout Two February 10, 2006 A Fortran program consists of a sequential list of Fortran statements and constructs. A statement can be seen a continuous line of code, like b=a*a*a or

More information

C/C++ Programming for Engineers: Matlab Branches and Loops

C/C++ Programming for Engineers: Matlab Branches and Loops C/C++ Programming for Engineers: Matlab Branches and Loops John T. Bell Department of Computer Science University of Illinois, Chicago Review What is the difference between a script and a function in Matlab?

More information

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics Assignment 1: Turtle Graphics Page 1 600.112: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics Peter H. Fröhlich phf@cs.jhu.edu Joanne Selinski joanne@cs.jhu.edu Due Date: Wednesdays

More information

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.

More information

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB SECTION 2: PROGRAMMING WITH MATLAB MAE 4020/5020 Numerical Methods with MATLAB 2 Functions and M Files M Files 3 Script file so called due to.m filename extension Contains a series of MATLAB commands The

More information

Question. Insight Through

Question. Insight Through Intro Math Problem Solving October 10 Question about Accuracy Rewrite Square Root Script as a Function Functions in MATLAB Road Trip, Restaurant Examples Writing Functions that Use Lists Functions with

More information

RECURSION, RECURSION, (TREE) RECURSION! 2

RECURSION, RECURSION, (TREE) RECURSION! 2 RECURSION, RECURSION, (TREE) RECURSION! 2 COMPUTER SCIENCE 61A February 5, 2015 A function is recursive if it calls itself. Below is a recursive factorial function. def factorial(n): if n == 0 or n ==

More information

Assignment #2: False Position Method

Assignment #2: False Position Method University of Puerto Rico Mayaguez Campus Department of Electrical & Computer Engineering Assignment #2: False Position Method Osvaldo M. Cardona 841-08-0990 Diana Rivera Negrón 802-08-6908 Ricardo I.

More information

REPETITIVE EXECUTION: LOOPS

REPETITIVE EXECUTION: LOOPS Contents REPETITIVE EXECUTION: LOOPS... 1 for Loops... 1 while Loops... 6 The break and continue Commands... 8 Nested Loops... 10 Distinguishing Characteristics of for and while Loops Things to Remember...

More information

(Refer Slide Time: 00:02:02)

(Refer Slide Time: 00:02:02) Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 20 Clipping: Lines and Polygons Hello and welcome everybody to the lecture

More information

Programming in MATLAB Part 2

Programming in MATLAB Part 2 Programming in MATLAB Part 2 A computer program is a sequence of computer commands. In a simple program the commands are executed one after the other in the order they are typed. MATLAB provides several

More information

1 Introduction to Matlab

1 Introduction to Matlab 1 Introduction to Matlab 1. What is Matlab? Matlab is a computer program designed to do mathematics. You might think of it as a super-calculator. That is, once Matlab has been started, you can enter computations,

More information

EP578 Computing for Physicists

EP578 Computing for Physicists EP578 Computing for Physicists Topic 3 Selection & Loops Department of Engineering Physics University of Gaziantep Course web page wwwgantepedutr/~bingul/ep578 Oct 2011 Sayfa 1 1 Introduction This lecture

More information

Appendix A: MATLAB Basics

Appendix A: MATLAB Basics Appix A: MATLAB Basics MATLAB numbers and numeric formats All numerical variables are stored in MATLAB in double precision floatingpoint form. (In fact it is possible to force some variables to be of other

More information

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed. Example 4 Printing and Plotting Matlab provides numerous print and plot options. This example illustrates the basics and provides enough detail that you can use it for typical classroom work and assignments.

More information

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators Flow Control We can control when how and the number of times calculations are made based on values of input data and/or data calculations in the program. Statements We Will Use in Flow Control for loops

More information

The Very Basics of the R Interpreter

The Very Basics of the R Interpreter Chapter 2 The Very Basics of the R Interpreter OK, the computer is fired up. We have R installed. It is time to get started. 1. Start R by double-clicking on the R desktop icon. 2. Alternatively, open

More information

VBScript: Math Functions

VBScript: Math Functions C h a p t e r 3 VBScript: Math Functions In this chapter, you will learn how to use the following VBScript functions to World Class standards: 1. Writing Math Equations in VBScripts 2. Beginning a New

More information

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Introduction to Matlab. By: Hossein Hamooni Fall 2014 Introduction to Matlab By: Hossein Hamooni Fall 2014 Why Matlab? Data analytics task Large data processing Multi-platform, Multi Format data importing Graphing Modeling Lots of built-in functions for rapid

More information

This is the basis for the programming concept called a loop statement

This is the basis for the programming concept called a loop statement Chapter 4 Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times did you solve it? What if you had millions of data points

More information

Important Things to Remember on the SOL

Important Things to Remember on the SOL Notes Important Things to Remember on the SOL Evaluating Expressions *To evaluate an expression, replace all of the variables in the given problem with the replacement values and use (order of operations)

More information

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk Control Statements ELEC 206 Prof. Siripong Potisuk 1 Objectives Learn how to change the flow of execution of a MATLAB program through some kind of a decision-making process within that program The program

More information

An Introduction to MATLAB

An Introduction to MATLAB An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu High level language Programing language and development environment Built-in development tools Numerical manipulation Plotting of

More information

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

hp calculators HP 33S Writing a Simple Program Programming the HP 33S Practice example: the area of a circle Tools for programming

hp calculators HP 33S Writing a Simple Program Programming the HP 33S Practice example: the area of a circle Tools for programming Programming the HP 33S Practice example: the area of a circle Tools for programming Practice example: the sinc function Using a program in another program Programming the HP 33S Doing a simple calculation

More information

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:

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: Contents VARIABLES... 1 Storing Numerical Data... 2 Limits on Numerical Data... 6 Storing Character Strings... 8 Logical Variables... 9 MATLAB S BUILT-IN VARIABLES AND FUNCTIONS... 9 GETTING HELP IN MATLAB...

More information

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch Computational Photonics Seminar 02, 30 April 2012 Programming in MATLAB controlling of a program s flow of execution branching loops loop control several programming tasks 1 Programming task 1 Plot the

More information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 7 Decision Structures Python Programming, 2/e 1 Objectives æ To understand the programming pattern simple decision and its implementation

More information

1.7 Limit of a Function

1.7 Limit of a Function 1.7 Limit of a Function We will discuss the following in this section: 1. Limit Notation 2. Finding a it numerically 3. Right and Left Hand Limits 4. Infinite Limits Consider the following graph Notation:

More information

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That 1. Introduction You have seen situations in which the way numbers are stored in a computer affects a program. For example, in the

More information

Chapter 7. Iteration. 7.1 Multiple assignment

Chapter 7. Iteration. 7.1 Multiple assignment Chapter 7 Iteration 7.1 Multiple assignment You can make more than one assignment to the same variable; effect is to replace the old value with the new. int bob = 5; System.out.print(bob); bob = 7; System.out.println(bob);

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information