Download presentation
Presentation is loading. Please wait.
1
Week 10 - Programming III So far: –Branches/condition testing –Loops Today: –Problem solving methodology –Examples Textbook chapter 7
2
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)
3
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
4
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
5
method of least squares:
6
Method of least squares Fit line, y = mx + b, to points Find m and b to Minimize the error, Solve for m and b
7
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
8
% 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!
9
% 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;
10
% 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')
11
Typical result: Simple check to see if it works
12
Example 2: sorting Given N numbers (x 1,…x N ), sort them into increasing order 3 1 4 5 2 7 1 0 1 2 3 4 5 7
13
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
14
31452703145270 01452730145273 01452730145273 01254730125473 01234750123475 01234750123475 01234570123457 start done Program employs nested loops:
15
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
16
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
17
Example 3: equation solving 3 x + 7 y = 0 and 2 x – 5 y = 4 (simultaneous linear equations) x 2 + 4 x + 7 = 0 (rooting polynomials) x – sin x – 0.0236 = 0 (transcendental equations ?? )
18
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 )
19
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 )
20
Back to the example f(x) = x – sin x – 0.0236 NR iteration is x k+1 = x k – f(x k ) / f ’(x k ) or implement this recursion; compare different values for first guess
21
x = -1 diff = 1; while diff > 0.001 xnew = x - ( x - sin(x) - 0.0236 )/( 1 - cos(x) ); diff = abs(xnew-x); x = xnew end compute until it converges abs to detect size of difference
22
Comparison of starting points: x – sin x – 0.0236 = 0 Start at x 0 = – 1.0 Start at x 0 = 0.1 iteration value
23
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
24
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
25
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.