1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225

Size: px
Start display at page:

Download "1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225"

Transcription

1 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225

2 Functions The first thing of the design of algorithms is to divide and conquer. A large and complex problem would be solved by couples of methods for its subproblems. In order to reuse the algorithm without copying the codes, the best way is to make functions. 1 The idea is similar to the math function, which is typically written in form of y = f (x), where x is the input value and y is the output value. 1 Make bricks for your castle. Zheng-Liang Lu 173 / 225

3 A function is a piece of computer code that accepts an input argument from the caller and returns output argument for the specific job the caller dealing with. Functions allow you to modularize a program by separating its tasks into self-contained units. We can program more efficiently and avoid rewriting the computer code for calculations that are performed frequently. 2 2 Remember that the bug propagates when you copy and paste the codes. It is a serious problem especially when you are working on a medium- or huge-level project. Zheng-Liang Lu 174 / 225

4 Built-in Arithmetic Functions Zheng-Liang Lu 175 / 225

5 Built-in Rounding Functions Can you organize an algorithm for the function ceil and floor? How to check if the input is an integer? (Try.) Zheng-Liang Lu 176 / 225

6 Built-in Discrete Math Functions Zheng-Liang Lu 177 / 225

7 Built-in Max Functions Zheng-Liang Lu 178 / 225

8 Built-in Max Functions (Cont d) There are also min functions to find the minimal elements in arrays, similar to max. Zheng-Liang Lu 179 / 225

9 Built-in Average Functions Zheng-Liang Lu 180 / 225

10 Variance and Standard Deviation Zheng-Liang Lu 181 / 225

11 Built-in Size Functions Zheng-Liang Lu 182 / 225

12 Random Number Generators To generate random integers, you may use randi(n, m, n) to produce an m-by-n random integer matrix ranging from 1 to N. Zheng-Liang Lu 183 / 225

13 You can write a program to generate a random variable which follows a standard uniform distribution, say by a linear congruential generator. 3 Be aware that there is no true random number generator in the machines. 4 Widely used in Monte Carlo simulation 5 and random number generation of other distributions 6. 3 See 4 For now, the modern computers are all deterministic. Quantum computers share theoretical similarities with non-deterministic and probabilistic computers. 5 See Glasserman (2003). 6 See the acceptance-rejection method and Metropolis-Hastings algorithm. Zheng-Liang Lu 184 / 225

14 User-Defined Functions The syntax of a user-defined function is given by 1 function [outputvar] = funcname(inputvar) 2 % comment section 3 end The output variables, if there exist, are enclosed in square brackets. The input variables, if there exist, must be enclosed with parentheses. funcname should start with a letter, and be the same as the file name in which it is saved. Before this function can be used, it must be saved into the current folder 7. 7 If not, change the current folder or add to the path pool. Zheng-Liang Lu 185 / 225

15 Functions without Input and Output 1 function [] = star( ) % [] and () can be dropped. 2 theta = pi / 2 : 0.8 * pi : 4.8 * pi; 3 r = ones(1, 6); 4 polar(theta, r) % plot in polar coordinate 5 end Zheng-Liang Lu 186 / 225

16 Example: Addition of Two Numbers 1 function z = myadd(x, y) 2 % input: x, y (two numbers) 3 % output: z (sum of x and y) 4 z = x + y; 5 end It seems bloody trivial. Actually, the plus sign is a kind of syntactic sugar. 8 8 Recall how to use an addition instruction in assembly codes. Zheng-Liang Lu 187 / 225

17 Example: Mean of A Sequence 1 function y = mymean(x) 2 % input: x (array) 3 % output: y (mean) 4 5 sum = 0; 6 for i = 1 : length(x) 7 sum = myadd(sum, x(i)); % call myadd 8 end 9 y = sum / length(x); 10 end Zheng-Liang Lu 188 / 225

18 Numbers of Arguments The variable nargin determines the number of input arguments in a function when executed. Similarly, the variable nargout determines the number of output arguments from a function when executed. The variable varargin is a special word with two roles: The variable varargin declares a function with any number of arguments. 9 The variable varargin itself is a cell array containing the optional arguments to the function. The variable varargout is a special word similar to varargin but for outputs. 9 Note that varargin must be declared as the last input argument and collects all the inputs from that point onwards. Zheng-Liang Lu 189 / 225

19 Example 1 function ret = myadd(varargin) 2 switch nargin 3 case 0 4 disp('no input.') 5 case 1 6 x = varargin{1}; 7 ret = x; 8 case 2 9 x = varargin{1}; 10 y = varargin{2}; 11 ret = x + y; 12 case 3 13 x = varargin{1}; 14 y = varargin{2}; 15 z = varargin{3}; 16 ret = x + y + z; 17 otherwise Zheng-Liang Lu 190 / 225

20 18 error('\ntoo many inputs.\n'); 19 end 20 end The special words nargin, nargout, varargin and varargout provide flexibility for user-defined functions. Note that the special word varargin and varargout should be the last item in the argument list. Zheng-Liang Lu 191 / 225

21 Scope of Variables The variables used in function m-files are known as local variables. Any variable defined within the function exists only for the function to use. The only way a function can communicate between other functions is through input arguments and the outputs it returns. Zheng-Liang Lu 192 / 225

22 Example 1 clear; clc; 2 3 x = 0; 4 for i = 1 : 10 5 addone(x); 6 disp(x); % output? 7 end 1 function addone(x) 2 x = x + 1; 3 end Zheng-Liang Lu 193 / 225

23 Global Variables Unlike local variables, global variables are available to all parts of a computer program. Use global to declare x as a global variable. Note that it is not a good practice to define many global variables. 10 For example, the universal constant, say,. 11 However, you have to take the responsibly of not changing its value. 10 This violets the modularity. 11 Planck constant = in Quantum theories. Zheng-Liang Lu 194 / 225

24 Example 1 function h = falling(t) 2 global GRAVITY 3 h = 1 / 2 * GRAVITY * t.ˆ 2; 4 end 1 clear; clc; 2 global GRAVITY 3 GRAVITY = 9.8; 4 y = falling(0 : 0.1 : 5); What if you take global out of the function? As a more robust alternative, redefine the function to accept GRAVITY as an input. Zheng-Liang Lu 195 / 225

25 Types of User-Defined Functions Primary functions and subfunctions Anonymous functions Nested functions Private functions Zheng-Liang Lu 196 / 225

26 Primary Functions and Subfunctions A function m-file may contain more than one user-defined function. The first defined function in the file is called the primary function, whose name is the same as the m-file name. All other functions in the file are called subfunctions. Subfunctions are normally visible only to the primary function and other subfunctions in the same file. Note that the order of the subfunctions does not matter, but function names must be unique within the m-file. Zheng-Liang Lu 197 / 225

27 Example 1 function [a c] = circle(r) % primary function 2 a = area(r); 3 c = perimeter(r); 4 end 5 6 function y = area(x) % subfunction 1 7 y = pi * x.ˆ 2; 8 end 9 10 function v = perimeter(u) % subfunction 2 11 v = 2 * pi * u; 12 end Zheng-Liang Lu 198 / 225

28 1 >> [a c] = circle(2) 2 3 a = c = Note that if only one variable is assigned as output of the function, herein, circle, then the area is returned while the perimeter is ignored. Zheng-Liang Lu 199 / 225

29 Example: Alternative to Infinite Loops 12 One subfunction can be called by other subfunctions in addition to the primary function. 1 function test1 2 fprintf('hi, there. This is test1.\n'); 3 test2; 4 end 5 6 function test2 7 fprintf('hi, there. This is test2.\n'); 8 test1; 9 end Notice that two subfunctions calling each other can lead to a loop, resulting a fatal crash. 12 Thanks to a lively class discussion (MATLAB-238) on June 14, Zheng-Liang Lu 200 / 225

30 Anonymous Functions 14 Anonymous functions, which are the expression followed by operator, enable you to create a simple function without needing to create an m-file for it. A function handle is a data type that stores an association to a function. 13 For example, 1 >> f x.ˆ 2 + x + 1 % x.ˆ2 allows... vectorization. 2 >> f([1 10]) 3 4 ans = See 14 See anonymous-functions.html. Zheng-Liang Lu 201 / 225

31 Furthermore, you can make the existing function become a function handle, for example, y You can create anonymous functions having more than one input. One anonymous function can call another to implement function composition. 1 % f is a function handle 2 >> f y) sqrt(x.ˆ 2 + y.ˆ 2); 3 % g is a composite function 4 >> g y) f(x, y).ˆ 2; 5 >> g(3, 4) 6 7 ans = Zheng-Liang Lu 202 / 225

32 More Examples 16 Give input values, return a function handle. 15 For example, 1 function y = f(a, b, c) 2 y a * x.ˆ 2 + b * x + c; 3 end Acts like a parabolic function generator! 15 Contribution by Ms. Queenie Chang (MAT25108) on March 18, Thanks to a lively class discussion (MATLAB-244) on August 22, Zheng-Liang Lu 203 / 225

33 Give a function handle as an input, return a value. 17 For example, 1 function y = diff(f, x) 2 eps = 1e-9; 3 y = (f(x + eps) - f(x)) / eps; 4 end Give a function handle as an input, return a function handle. For example, 1 function y = diff(f) 2 eps = 1e-9; 3 y (f(x + eps) - f(x)) / eps; 4 end 17 Thanks to a lively class discussion (MATLAB-260) on September 16, Zheng-Liang Lu 204 / 225

34 Nested Functions Functions are said to be nested if the functions are defined within the parent function. A nested function can access the variables of its parent function. 1 function f = parabola(a, b, c) 2 f %f is a function handle of p 3 function y = p(x) % p shares a, b, and c 4 y = a * x.ˆ2 + b * x + c; 5 end 6 end Zheng-Liang Lu 205 / 225

35 1 >> y = parabola(1, 2, 1); 2 >> y(5) 3 4 ans = Note that the nested functions are defined anywhere within the main function. (Why?) Zheng-Liang Lu 206 / 225

36 Private Functions Private functions are useful when you want to limit the scope of the function. A private functions resides in subfolder with the special name private. The private function is visible only to functions in the parent directory. Note that you cannot call the private function from the command line or from functions outside the parent of the private folder. Zheng-Liang Lu 207 / 225

37 Precedence When Calling Functions 1. Nested functions 2. Subfunctions within the same file 3. Private functions 4. Local functions in the same directory 5. Built-in functions 6. Standard m files in PATH Zheng-Liang Lu 208 / 225

38 Example: Bisection Method The bisection method in mathematics is a root-finding method that repeatedly bisects an interval and then selects a subinterval in which a root must lie for further processing. (Why?) It is often used to obtain an approximate solution. Zheng-Liang Lu 209 / 225

39 Zheng-Liang Lu 210 / 225

40 Outline for Bisection Method 1. At each step, the algorithm divides the interval in two by computing the midpoint c = (a + b)/2 of the interval and the value of the function f (c) at that point. 2. Unless c is itself a root (which is very unlikely, but possible), there are now two possibilities: either f (a) and f (c) have opposite signs and bracket a root, or f (c) and f (b) have opposite signs and bracket a root. Zheng-Liang Lu 211 / 225

41 3. The method selects the subinterval that is a bracket as a new interval to be used in the next step. 4. In this way the interval that contains a zero of f is reduced in width by 1/2 at each step. 5. The process is continued until the interval is sufficiently small How small? A certain number you give is to be the stop criterion. Zheng-Liang Lu 212 / 225

42 Problem Formulation Input - Target function f (x) - Endpoints of the interval [a, b] for any real numbers a < b - Minimal interval length ɛ = b a Output - the approximate root ˆr Note that a and b will be updated iteratively. Zheng-Liang Lu 213 / 225

43 Solution 1 clear; clc; format long; 2 3 a = input('a =?\n'); 4 b = input('b =?\n'); 5 f x.ˆ 3 - x - 2; % target function 6 eps = 1e-9; 7 iter = 0; % the number of iterations 8 9 if f(a) == 0 10 r = a; % lucky a 11 elseif f(b) == 0 12 r = b; % lucky b 13 else 14 while b - a > eps 15 iter = iter + 1; 16 c = (a + b) / 2; % middle point 17 if f(c) == 0 Zheng-Liang Lu 214 / 225

44 18 r = c; % lucky c 19 break; 20 elseif (f(a) * f(c) < 0) 21 b = c; 22 elseif (f(b) * f(c) < 0) 23 a = c; 24 else 25 error('failure: f(a) * f(c) > 0 and... f(c) * f(b) > 0.'); 26 end 27 fprintf('%d: %f\n', iter, c); 28 end 29 r = c % approximate solution 30 end Zheng-Liang Lu 215 / 225

45 c = Zheng-Liang Lu 216 / 225

46 Remarks Time complexity: O(log 2 n) What is n? In this case, n = b a. (Why?) ɛ So, it is an algorithm which runs in log time. This means that you need to make a trade-off between the numerical precision and the computation time. Be aware that this algorithm works well only with the premise that the behavior in [a, b] is mild. Approximate solutions may be significantly influenced by the initial interval [a, b]. 19 f (c) 0 but not equal to exactly 0. (Why?) 19 You may try another algorithm for the root finding problem, say the Newton-Raphson method. Zheng-Liang Lu 217 / 225

47 Exercise: Square Roots Using the aforesaid bisection algorithm, write a function which determines n for all n R. It is easy to see that n = x is equivalent to x 2 n = 0. So n is the positive root of x 2 n = 0, which is the input of the bisection algorithm. Intuitively, a = 0 and b = n. Zheng-Liang Lu 218 / 225

48 Recursive Functions Recursion is when something is defined in terms of itself. A recursive function is a function that calls itself. Recursion is an alternative form of program control. It is essentially repetition without a loop. Zheng-Liang Lu 219 / 225

49 Example The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 4! = = 4 3! So we can say f (n) = n f (n 1), for n > 0. Also, we need a base case such that the recursion can stop instead of being an infinite loop. In this example, 0! = 1. Zheng-Liang Lu 220 / 225

50 Exercise Write a program which determines return a factorial of n. 1 function y = recursivefactorial(n) 2 if n > 0 3 y = n * recursivefactorial(n - 1); 4 else 5 y = 1; % base case 6 end 7 end We can use a loop to calculate n!. (Try.) Zheng-Liang Lu 221 / 225

51 Call Stack Zheng-Liang Lu 222 / 225

52 Equivalence 1 function y = loopfactorial(n) 2 y = 1; 3 for i = n : -1 : 1 4 y = y * i; 5 end 6 end One more intriguing question is, Can we always turn a recursive method into an iterative one? Yes, theoretically. 20 Which is better? 20 The Church-Turing thesis proves it if the memory serves. Zheng-Liang Lu 223 / 225

53 Example: Fibonacci Numbers 21 The Fibonacci sequence is a sequence with the following pattern: 0, 1, 1, 2, 3, 5, 8, 13, 21, Alternatively, the Fibonacci sequence is defined by the recurrence relation F n = F n 1 + F n 2, where F 0 = 0, F 1 = 1, and n 2. Given a positive integer n, determine F n. Input: n 0 Output: F n 21 See Fibonacci numbers. Zheng-Liang Lu 224 / 225

54 Solution 1 function y = recursivefib(n) 2 if n == 0 3 y = 0; 4 elseif n == 1 5 y = 1; 6 else 7 y = recursivefib(n - 1) + recursivefib(n ); 8 end 9 end It is a binary recursion, because there are two calls in the function. Time complexity: O(2 n ) (Why?) Can we implement a linear recursion for this problem? Zheng-Liang Lu 225 / 225

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable.

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable. Scope of Variables The variables used in function m-files are known as local variables. Any variable defined within the function exists only for the function to use. The only way a function can communicate

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 Functions Recall that an algorithm is a feasible solution to the specific problem. 1 A function is a piece of computer code that accepts

More information

ˆ Note that we often make a trade-off between time and space. ˆ Time complexity ˆ Space complexity. ˆ Unlike time, we can reuse memory.

ˆ Note that we often make a trade-off between time and space. ˆ Time complexity ˆ Space complexity. ˆ Unlike time, we can reuse memory. ˆ We use O-notation to describe the asymptotic 1 upper bound of complexity of the algorithm. ˆ So O-notation is widely used to classify algorithms by how they respond to changes in its input size. 2 ˆ

More information

Global Variables. ˆ Unlike local variables, global variables are available to all functions involved.

Global Variables. ˆ Unlike local variables, global variables are available to all functions involved. Global Variables ˆ Unlike local variables, global variables are available to all functions involved. ˆ Use global to declare x as global. ˆ For example, the universal constant, say,. 1 ˆ However, it is

More information

Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order).

Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order). Exercise: Sorting 1 ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order). ˆ For example, A = [5, 4, 1, 2, 3]. ˆ Then the sorted array is [1, 2, 3, 4, 5]. ˆ You

More information

Jump Statements. ˆ The break statement exits a for or while loop completely.

Jump Statements. ˆ The break statement exits a for or while loop completely. Jump Statements ˆ The break statement exits a for or while loop completely. ˆ No longer in the loop. ˆ Aka early termination. ˆ To skip the rest of the instructions in the loop and begin the next iteration,

More information

The return Statement

The return Statement The return Statement The return statement is the end point of the method. A callee is a method invoked by a caller. The callee returns to the caller if the callee completes all the statements (w/o a return

More information

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226 Method Invocation Note that the input parameters are sort of variables declared within the method as placeholders. When calling the method, one needs to provide arguments, which must match the parameters

More information

Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be

Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be Arrays ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be ˆ row vectors: u R 1 n for any positive integer n ˆ

More information

Exercise. Write a program which allows the user to enter the math grades one by one (-1 to exit), and outputs a histogram.

Exercise. Write a program which allows the user to enter the math grades one by one (-1 to exit), and outputs a histogram. Exercise Write a program which allows the user to enter the math grades one by one (-1 to exit), and outputs a histogram. Zheng-Liang Lu Java Programming 197 / 227 1... 2 int[] hist = new int[5]; 3 //

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

Variable Scope. The variable scope is the range of the program where the variable can be referenced.

Variable Scope. The variable scope is the range of the program where the variable can be referenced. Variable Scope The variable scope is the range of the program where the variable can be referenced. Variables can be declared in class level, method level, and loop level. In general, a pair of curly brackets

More information

Selections. Zheng-Liang Lu 91 / 120

Selections. Zheng-Liang Lu 91 / 120 Selections ˆ Selection enables us to write programs that make decisions on. ˆ Selection structures contain one or more of the if, else, and elseif statements. ˆ The end statement denotes the end of selection

More information

Recursion 1. Recursion is the process of defining something in terms of itself.

Recursion 1. Recursion is the process of defining something in terms of itself. Recursion 1 Recursion is the process of defining something in terms of itself. A method that calls itself is said to be recursive. Recursion is an alternative form of program control. It is repetition

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

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, Cloning Arrays In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, 1... 2 T[] A = {...}; // assume A is an array 3 T[] B = A;

More information

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321 Sketchpad Graphics Language Reference Manual Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321 October 20, 2013 1. Introduction This manual provides reference information for using the SKL (Sketchpad

More information

Common Errors double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141

Common Errors double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141 Common Errors 2 double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141 Generating random numbers Example Write a program which generates

More information

MATLAB Lecture 4. Programming in MATLAB

MATLAB Lecture 4. Programming in MATLAB MATLAB Lecture 4. Programming in MATLAB In this lecture we will see how to write scripts and functions. Scripts are sequences of MATLAB statements stored in a file. Using conditional statements (if-then-else)

More information

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method. Reals 1 13 Reals Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method. 13.1 Floating-point numbers Real numbers, those declared to be

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

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Logic is the anatomy of thought. John Locke ( ) This sentence is false. Logic is the anatomy of thought. John Locke (1632 1704) This sentence is false. I know that I know nothing. anonymous Plato (In Apology, Plato relates that Socrates accounts for his seeming wiser than

More information

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

1 class Lecture3 { 2 3 Selections // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133 1 class Lecture3 { 2 3 "Selections" 4 5 } 6 7 // Keywords 8 if, else, else if, switch, case, default Zheng-Liang Lu Java Programming 88 / 133 Flow Controls The basic algorithm (and program) is constituted

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

Arithmetic Compound Assignment Operators

Arithmetic Compound Assignment Operators Arithmetic Compound Assignment Operators Note that these shorthand operators are not available in languages such as Matlab and R. Zheng-Liang Lu Java Programming 76 / 141 Example 1... 2 int x = 1; 3 System.out.println(x);

More information

SPARK-PL: Introduction

SPARK-PL: Introduction Alexey Solovyev Abstract All basic elements of SPARK-PL are introduced. Table of Contents 1. Introduction to SPARK-PL... 1 2. Alphabet of SPARK-PL... 3 3. Types and variables... 3 4. SPARK-PL basic commands...

More information

Variables and Assignments

Variables and Assignments Variables and Assignments ˆ A variable is used to keep a value or values. ˆ A box which contains something. ˆ In most languages, a statement looks like var = expression, where var is a variable and expression

More information

Structure Array 1 / 50

Structure Array 1 / 50 Structure Array A structure array is a data type that groups related data using data containers called fields. Each field can contain any type of data. Access data in a structure using dot notation of

More information

1 class Lecture4 { 2 3 "Loops" / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207

1 class Lecture4 { 2 3 Loops / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207 1 class Lecture4 { 2 3 "Loops" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207 Loops A loop can be used to make a program execute statements repeatedly without having

More information

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

1 class Lecture3 { 2 3 Selections // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137 1 class Lecture3 { 2 3 "Selections" 4 5 } 6 7 // Keywords 8 if, else, else if, switch, case, default Zheng-Liang Lu Java Programming 89 / 137 Flow Controls The basic algorithm (and program) is constituted

More information

Chapters 6-7. User-Defined Functions

Chapters 6-7. User-Defined Functions Chapters 6-7 User-Defined Functions User-Defined Functions, Iteration, and Debugging Strategies Learning objectives: 1. Write simple program modules to implement single numerical methods and algorithms

More information

10.4 Linear interpolation method Newton s method

10.4 Linear interpolation method Newton s method 10.4 Linear interpolation method The next best thing one can do is the linear interpolation method, also known as the double false position method. This method works similarly to the bisection method by

More information

Calculus I Review Handout 1.3 Introduction to Calculus - Limits. by Kevin M. Chevalier

Calculus I Review Handout 1.3 Introduction to Calculus - Limits. by Kevin M. Chevalier Calculus I Review Handout 1.3 Introduction to Calculus - Limits by Kevin M. Chevalier We are now going to dive into Calculus I as we take a look at the it process. While precalculus covered more static

More information

10 M-File Programming

10 M-File Programming MATLAB Programming: A Quick Start Files that contain MATLAB language code are called M-files. M-files can be functions that accept arguments and produce output, or they can be scripts that execute a series

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

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

switch-case Statements

switch-case Statements switch-case Statements A switch-case structure takes actions depending on the target variable. 2 switch (target) { 3 case v1: 4 // statements 5 break; 6 case v2: 7. 8. 9 case vk: 10 // statements 11 break;

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

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

ENGINEERING PROBLEM SOLVING WITH C++

ENGINEERING PROBLEM SOLVING WITH C++ ENGINEERING PROBLEM SOLVING WITH C++ Second Edition Delores M. Etter Electrical Engineering Department United States Naval Academy Jeanine A. Ingber Training Consultant Sandia National Laboratories Upper

More information

Scientific Computing with MATLAB

Scientific Computing with MATLAB Scientific Computing with MATLAB Dra. K.-Y. Daisy Fan Department of Computer Science Cornell University Ithaca, NY, USA UNAM IIM 2012 2 Focus on computing using MATLAB Computer Science Computational Science

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

IEEE Floating-Point Representation 1

IEEE Floating-Point Representation 1 IEEE Floating-Point Representation 1 x = ( 1) s M 2 E The sign s determines whether the number is negative (s = 1) or positive (s = 0). The significand M is a fractional binary number that ranges either

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

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

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras Module No. #01 Lecture No. #1.1 Introduction to MATLAB programming

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

Repetition Structures Chapter 9

Repetition Structures Chapter 9 Sum of the terms Repetition Structures Chapter 9 1 Value of the Alternating Harmonic Series 0.9 0.8 0.7 0.6 0.5 10 0 10 1 10 2 10 3 Number of terms Objectives After studying this chapter you should be

More information

Example. Generating random numbers. Write a program which generates 2 random integers and asks the user to answer the math expression.

Example. Generating random numbers. Write a program which generates 2 random integers and asks the user to answer the math expression. Example Generating random numbers Write a program which generates 2 random integers and asks the user to answer the math expression. For example, the program shows 2 + 5 =? If the user answers 7, then

More information

Question Points Score Total 100

Question Points Score Total 100 Name Signature General instructions: You may not ask questions during the test. If you believe that there is something wrong with a question, write down what you think the question is trying to ask and

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

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. 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. CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133 Scanner Objects It is not convenient to modify the source code and recompile it for a different radius. Reading from the console enables the program to receive an input from the user. A Scanner object

More information

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

1 class Lecture5 { 2 3 Methods / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199 1 class Lecture5 { 2 3 "Methods" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199 Methods 2 Methods can be used to define reusable code, and organize

More information

Java Programming. U Hou Lok. Java Aug., Department of Computer Science and Information Engineering, National Taiwan University

Java Programming. U Hou Lok. Java Aug., Department of Computer Science and Information Engineering, National Taiwan University Java Programming U Hou Lok Department of Computer Science and Information Engineering, National Taiwan University Java 272 8 19 Aug., 2016 U Hou Lok Java Programming 1 / 51 A Math Toolbox: Math Class The

More information

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

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices Introduction to Interactive Calculations Matlab is interactive, no need to declare variables >> 2+3*4/2 >> V = 50 >> V + 2 >> V Ans = 52 >> a=5e-3; b=1; a+b Most elementary functions and constants are

More information

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers) Review Final exam Final exam will be 12 problems, drop any 2 Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers) 2 hours exam time, so 12 min per problem (midterm 2 had

More information

Example. Write a program which generates 2 random integers and asks the user to answer the math expression.

Example. Write a program which generates 2 random integers and asks the user to answer the math expression. Generating random numbers Example Write a program which generates 2 random integers and asks the user to answer the math expression. For example, the program shows 2 + 5 =? If the user answers 7, then

More information

II (Sorting and) Order Statistics

II (Sorting and) Order Statistics II (Sorting and) Order Statistics Heapsort Quicksort Sorting in Linear Time Medians and Order Statistics 8 Sorting in Linear Time The sorting algorithms introduced thus far are comparison sorts Any comparison

More information

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

MATH 2650/ Intro to Scientific Computation - Fall Lab 1: Starting with MATLAB. Script Files MATH 2650/3670 - Intro to Scientific Computation - Fall 2017 Lab 1: Starting with MATLAB. Script Files Content - Overview of Course Objectives - Use of MATLAB windows; the Command Window - Arithmetic operations

More information

++x vs. x++ We will use these notations very often.

++x vs. x++ We will use these notations very often. ++x vs. x++ The expression ++x first increments the value of x and then returns x. Instead, the expression x++ first returns the value of x and then increments itself. For example, 1... 2 int x = 1; 3

More information

Example: Monte Carlo Simulation 1

Example: Monte Carlo Simulation 1 Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149

More information

Key Concepts: Economic Computation, Part II

Key Concepts: Economic Computation, Part II Key Concepts: Economic Computation, Part II Brent Hickman Fall, 2009 The purpose of the second section of these notes is to give you some further practice with numerical computation in MATLAB, and also

More information

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

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial 1 Matlab Tutorial 2 Lecture Learning Objectives Each student should be able to: Describe the Matlab desktop Explain the basic use of Matlab variables Explain the basic use of Matlab scripts Explain the

More information

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. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

LECTURE 1. What Is Matlab? Matlab Windows. Help

LECTURE 1. What Is Matlab? Matlab Windows. Help LECTURE 1 What Is Matlab? Matlab ("MATrix LABoratory") is a software package (and accompanying programming language) that simplifies many operations in numerical methods, matrix manipulation/linear algebra,

More information

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. 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. CSE 10 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Introduction to Engineering gii

Introduction to Engineering gii 25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2.

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

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

Fundamentals of Programming Session 13

Fundamentals of Programming Session 13 Fundamentals of Programming Session 13 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

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

Sprite an animation manipulation language Language Reference Manual

Sprite an animation manipulation language Language Reference Manual Sprite an animation manipulation language Language Reference Manual Team Leader Dave Smith Team Members Dan Benamy John Morales Monica Ranadive Table of Contents A. Introduction...3 B. Lexical Conventions...3

More information

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors. 1 LECTURE 3 OUTLINES Variable names in MATLAB Examples Matrices, Vectors and Scalar Scalar Vectors Entering a vector Colon operator ( : ) Mathematical operations on vectors examples 2 VARIABLE NAMES IN

More information

Divide and Conquer Strategy. (Page#27)

Divide and Conquer Strategy. (Page#27) MUHAMMAD FAISAL MIT 4 th Semester Al-Barq Campus (VGJW01) Gujranwala faisalgrw123@gmail.com Reference Short Questions for MID TERM EXAMS CS502 Design and Analysis of Algorithms Divide and Conquer Strategy

More information

Unit-5 Dynamic Programming 2016

Unit-5 Dynamic Programming 2016 5 Dynamic programming Overview, Applications - shortest path in graph, matrix multiplication, travelling salesman problem, Fibonacci Series. 20% 12 Origin: Richard Bellman, 1957 Programming referred to

More information

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors 1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors on an EREW PRAM: See solution for the next problem. Omit the step where each processor sequentially computes the AND of

More information

Problem Set 6. Part A:

Problem Set 6. Part A: Introduction to Algorithms: 6.006 Massachusetts Institute of Technology April 12, 2011 Professors Erik Demaine, Piotr Indyk, and Manolis Kellis Problem Set 6 Problem Set 6 This problem set is divided into

More information

More Complicated Recursion CMPSC 122

More Complicated Recursion CMPSC 122 More Complicated Recursion CMPSC 122 Now that we've gotten a taste of recursion, we'll look at several more examples of recursion that are special in their own way. I. Example with More Involved Arithmetic

More information

DOWNLOAD PDF BIG IDEAS MATH VERTICAL SHRINK OF A PARABOLA

DOWNLOAD PDF BIG IDEAS MATH VERTICAL SHRINK OF A PARABOLA Chapter 1 : BioMath: Transformation of Graphs Use the results in part (a) to identify the vertex of the parabola. c. Find a vertical line on your graph paper so that when you fold the paper, the left portion

More information

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 34. Recursion Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Introduction Example: Factorials Example: Fibonacci Numbers Recursion vs. Iteration References Introduction Introduction Recursion

More information

Matlab and Octave: Quick Introduction and Examples 1 Basics

Matlab and Octave: Quick Introduction and Examples 1 Basics Matlab and Octave: Quick Introduction and Examples 1 Basics 1.1 Syntax and m-files There is a shell where commands can be written in. All commands must either be built-in commands, functions, names of

More information

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct. Example Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct. 1... 2 Scanner input = new Scanner(System.in); 3 int x = (int) (Math.random()

More information

Structure of Programming Languages Lecture 5

Structure of Programming Languages Lecture 5 Structure of Programming Languages Lecture 5 CSCI 6636 4536 June, 2017 CSCI 6636 4536 Lecture 10... 1/16 June, 2017 1 / 16 Outline 1 Expressions and Evaluation 2 Control Structures Conditionals Repetition

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Advanced Algorithms and Data Structures

Advanced Algorithms and Data Structures Advanced Algorithms and Data Structures Prof. Tapio Elomaa tapio.elomaa@tut.fi Course Prerequisites A seven credit unit course Replaced OHJ-2156 Analysis of Algorithms We take things a bit further than

More information

Recursion Chapter 3.5

Recursion Chapter 3.5 Recursion Chapter 3.5-1 - Outline Induction Linear recursion Example 1: Factorials Example 2: Powers Example 3: Reversing an array Binary recursion Example 1: The Fibonacci sequence Example 2: The Tower

More information

User Defined Functions

User Defined Functions User Defined Functions 120 90 1 0.8 60 Chapter 6 150 0.6 0.4 30 0.2 180 0 210 330 240 270 300 Objectives Create and use MATLAB functions with both single and multiple inputs and outputs Learn how to store

More information

Bisection method. we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f

Bisection method. we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f Bisection method we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f 1 function [root] = bisect(f, a, b, tol) %BISECT Root finding

More information

MATLAB SUMMARY FOR MATH2070/2970

MATLAB SUMMARY FOR MATH2070/2970 MATLAB SUMMARY FOR MATH2070/2970 DUNCAN SUTHERLAND 1. Introduction The following is inted as a guide containing all relevant Matlab commands and concepts for MATH2070 and 2970. All code fragments should

More information

MBI REU Matlab Tutorial

MBI REU Matlab Tutorial MBI REU Matlab Tutorial Lecturer: Reginald L. McGee II, Ph.D. June 8, 2017 MATLAB MATrix LABoratory MATLAB is a tool for numerical computation and visualization which allows Real & Complex Arithmetics

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #43 Multidimensional Arrays In this video will look at multi-dimensional arrays. (Refer Slide Time: 00:03) In

More information

Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N. Lecture 11 Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

More information

Introduction. Like other programming languages, MATLAB has means for modifying the flow of a program

Introduction. Like other programming languages, MATLAB has means for modifying the flow of a program Flow control 1 Introduction Like other programming languages, MATLAB has means for modying the flow of a program All common constructs are implemented in MATLAB: for while then else switch try 2 FOR loops.

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

Computer Programming in MATLAB

Computer Programming in MATLAB Computer Programming in MATLAB Prof. Dr. İrfan KAYMAZ Atatürk University Engineering Faculty Department of Mechanical Engineering What is a computer??? Computer is a device that computes, especially a

More information

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

1 class Lecture2 { 2 3 Elementray Programming / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 41 / 68 Example Given the radius

More information

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving The Bisection Method and Newton s Method. If f( x ) a function, then a number r for which f( r) 0 is called a zero or a root of the function f( x ), or a solution to the equation f( x) 0. You are already

More information