Week 10 - Programming III So far: –Branches/condition testing –Loops Today: –Problem solving methodology –Examples Textbook chapter 7.

Slides:



Advertisements
Similar presentations
Fixed point iterations and solution of non-linear functions
Advertisements

Computational Statistics. Basic ideas  Predict values that are hard to measure irl, by using co-variables (other properties from the same measurement.
Open Methods Chapter 6 The Islamic University of Gaza
FTP Biostatistics II Model parameter estimations: Confronting models with measurements.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Optimization 吳育德.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
Chapter 1 Introduction The solutions of engineering problems can be obtained using analytical methods or numerical methods. Analytical differentiation.
CHAPTER 11 Sorting.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 20 Solution of Linear System of Equations - Iterative Methods.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 19 Solution of Linear System of Equations - Iterative Methods.
ENGG 1801 Engineering Computing MATLAB Lecture 7: Tutorial Weeks Solution of nonlinear algebraic equations (II)
Week 11 - Programming IV Today 1. Vectorization to simplify programs 2. Structured software Definition: array operations versus loops 1.masks (indicator.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter ,
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Open Methods Chapter 6 The Islamic University of Gaza
ECIV 301 Programming & Graphics Numerical Methods for Engineers REVIEW II.
Searching and Sorting Arrays
Lecture Notes Dr. Rakhmad Arief Siregar Universiti Malaysia Perlis
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Chapter Point slope Form.
Exercise problems for students taking the Programming Parallel Computers course. Janusz Kowalik Piotr Arlukowicz Tadeusz Puzniakowski Informatics Institute.
First, some left-overs… Lazy languages Nesting 1.
CMPSC 200 Spring 2013 Lecture 38 November 18 or 20, 2013 (depending on section)
Lecture Notes Dr. Rakhmad Arief Siregar Universiti Malaysia Perlis
Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 1.
Hydroinformatics: Session4 Dr Ivan Stoianov Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Loop Application: Numerical Methods, Part 1 The power of Matlab Mathematics + Coding.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 ~ Roots of Equations ~ Open Methods Chapter 6 Credit:
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Solution of a System of ODEs with POLYMATH and MATLAB, Boundary Value Iterations with MATLAB For a system of n simultaneous first-order ODEs: where x is.
Even more problems.. Mean (average) I need a program that calculates the average of student test scores. I need a program that calculates the average.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
Scientific Computing General Least Squares. Polynomial Least Squares Polynomial Least Squares: We assume that the class of functions is the class of all.
Numerical Methods for Engineering MECN 3500
Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
Newton’s Method, Root Finding with MATLAB and Excel
Circuits Theory Examples Newton-Raphson Method. Formula for one-dimensional case: Series of successive solutions: If the iteration process is converged,
Applications of Loops: The power of MATLAB Mathematics + Coding
Numerical Methods Solution of Equation.
CHAPTER 10 Widrow-Hoff Learning Ming-Feng Yeh.
Chapter 3- Model Fitting. Three Tasks When Analyzing Data: 1.Fit a model type to the data. 2.Choose the most appropriate model from the ones that have.
Lecture 6 - Single Variable Problems & Systems of Equations CVEN 302 June 14, 2002.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
CSE 330: Numerical Methods. What is true error? True error is the difference between the true value (also called the exact value) and the approximate.
CSE 330: Numerical Methods. Introduction The bisection and false position method require bracketing of the root by two guesses Such methods are called.
CMPSC 200 Fall 2013 Lecture 37 November 18, 2013.
Searching and Sorting Algorithms
Applied Discrete Mathematics Week 2: Functions and Sequences
Linear Regression Special Topics.
Numerical Methods.
Chapter 7 Functions and Graphs.
Numerical Methods.
ENGG 1801 Engineering Computing
Lecture 7 MATLAB programming (5)
6.5 Taylor Series Linearization
Section 3.1 Graphs of Linear Equations.
Presentation transcript:

Week 10 - Programming III So far: –Branches/condition testing –Loops Today: –Problem solving methodology –Examples Textbook chapter 7

Problem Solving Methodology 1.Define the problem and your goals 2.Identify information (inputs) and desired results (outputs) 3.Select mathematical approach (assumptions, scientific principles, solution method) 4.Write a program (use array operations) 5.Test your program (with a simple case)

Example 1: line fitting Given N pairs of values (x i,y i ), find the best straight line fit y = m x + b m ≡ slope of line, b ≡ y intercept

Problem solving methodology: 1.State the problem: find best line to fit data 2.Identify inputs and outputs: (x i,y i )  (m,b) 3.Describe algorithm: least squares 4.Write program 5.Test program

method of least squares:

Method of least squares Fit line, y = mx + b, to points Find m and b to Minimize the error, Solve for m and b

Program flow: 1.Read in data: How many data points Get paired data 2.Compute best fit for m and b 3.Provide output to user: Numerical values of both m and b Graphical presentation

% Comments on the program … % Get number of pairs, n_points n_points = input('How many (x,y) pairs? '); % Read in the input data x, y for ii = 1:n_points temp = input('Enter [x y]: '); x(ii) = temp(1); y(ii) = temp(2); end typical use of a loop, but could have user just enter vectors!

% Calculate m and b m = ( sum(x.*y) – sum(x)*sum(y)/n_points )/( … sum(x.^2) – sum(x)^2/n_points ); b = ( sum(y) – m*sum(x) )/n_points;

% Plot the data plot(x,y,'bo') hold on twopts = [ min(x), max(x) ]; plot( twopts, m*twopts+b,'r-','Linewidth',2) hold off % Add labels, etc. gtext([' y = ',num2str(m), ' x + ',num2str(b)]) xlabel('x data'), ylabel('y data'), title('line fit')

Typical result: Simple check to see if it works

Example 2: sorting Given N numbers (x 1,…x N ), sort them into increasing order

Algorithm: Given N numbers, x 1 …x N : –Search through x 1 …x N for the smallest, swap with x 1 –Search through x 2 …x N for the smallest, swap with x 2 –Continue a total of N-1 times

start done Program employs nested loops:

function array = simpsort(array) % function to sort from smallest to largest for k = 1:length(array)-1 loc = k; for k2 = k:length(array) if array(k2)<array(loc) loc = k2; end if loc ~= k array([k,loc ]) = array([loc,k]); end locate the index (loc) of the smallest value swap values, if necessary

Another sort function y = upsort(x) % UPSORT sorts a vector x of any length. % UPSORTs output y is the vector x with elements arranged % in ascending order z = x;% Put x in a temporary vector z. for n=1:length(x) [y(n),k]=min(z);% y(n) is the smallest element of z. z(k)=[];% Erase the smallest element of z. end

Example 3: equation solving 3 x + 7 y = 0 and 2 x – 5 y = 4 (simultaneous linear equations) x x + 7 = 0 (rooting polynomials) x – sin x – = 0 (transcendental equations ?? )

Newton-Raphson method: Assuming an equation of form f(x) = 0 From the Taylor series expansion f(x) = f(x 1 ) + ( x – x 1 ) f ’(x 1 ) + … keep only the first 2 terms f(x)  f(x 1 ) + ( x – x 1 ) f ’(x 1 ) = 0 and solve for x x = x 1 – f(x 1 ) / f ’(x 1 )

Thinking of x 1 as a first guess, then x = x 1 – f (x 1 ) / f ’(x 1 ) yields a better estimate of the root Repeating is the NR iteration: x k+1 = x k – f (x k ) / f ’(x k )

Back to the example f(x) = x – sin x – NR iteration is x k+1 = x k – f(x k ) / f ’(x k ) or implement this recursion; compare different values for first guess

x = -1 diff = 1; while diff > xnew = x - ( x - sin(x) )/( 1 - cos(x) ); diff = abs(xnew-x); x = xnew end compute until it converges abs to detect size of difference

Comparison of starting points: x – sin x – = 0 Start at x 0 = – 1.0 Start at x 0 = 0.1 iteration value

Homework: tic-tac-toe analysis Find the winner in a 3-by-3 tic-tac-toe board. Assume representations: –Empty cell = 0 –X = +1 –O = – 1

Problem solving methodology: 1.State the problem: look for occurrences of +1 or – 1 in 3 adjacent cells 2.Identify inputs and outputs: board array  winner or not 3.Describe algorithm: 4.Write program 5.Test program

win for X, column of 1’s sums to 3 draw – no empty cells, and no winner Sum(board) and trace(board) are useful, where board is the 3x3 array representing the game.