Download presentation

Presentation is loading. Please wait.

Published byGonzalo Hathorn Modified about 1 year ago

1
Lecture 4 Structure plan Program design process Program design process Basic elements of code: Basic elements of code: –Input (assignment of variables) –Sequence of expressions –Output (graphical or tabular) Structured approach to design Structured approach to design © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

2
Review of basic elements Input Input –Initialize, define, request, or assign numerical values to variables. Set of commands Set of commands –Operations applied to input variables that lead to the desired result. Output Output –Display (graphically or numerically) result.

3
A technical computing problem Let us examine the problem of computing the roots of a quadratic equation. Let us examine the problem of computing the roots of a quadratic equation. We want to write a computer program to compute the roots of the following equation: We want to write a computer program to compute the roots of the following equation: a.* x.^2 + b.* x + c = 0 a.* x.^2 + b.* x + c = 0 The structure plan is provided in the text and will be utilized in this lecture. The structure plan is provided in the text and will be utilized in this lecture.

4
Structure plan Develop the structure plan, e.g.: Develop the structure plan, e.g.: 1. Start (summary of Fig. 3.3 in the text) 2. Input data (a, b, c) 3. If a = 0, b = 0, and c = 0, indeterminate. 4. If a = b = 0, no solution. 5. If a = 0, x = -c/b; one root; equation is linear. 6. If b^2 < 4ac, roots are complex. 7. If b^2 = 4ac, roots are equal, x = b/(2a). 8. If b^2 > 4ac, then the roots are: x1 = (-b + sqrt( b.^2 – 4.*a.*c)./(2.*a) x2 = (-b - sqrt( b.^2 – 4.*a.*c)./(2.*a) 9. Stop Translate this plan to a program: Next slide. Translate this plan to a program: Next slide.

5
% Quadratic equation % a.* x.^2 + b.* x + c = 0 % Step 1: Start format compact disp(' ') disp(' Quadratic roots finder ') disp(' ') % Step 2: Input data A = input(' Specify coefficients as follows: [a, b, c] = '); a = A(1);b = A(2);c = A(3); % Step 3: Evaluation of roots if a==0 & b==0 & c==0 disp(' Solution is indeterminate ') elseif a==0 & b==0 disp(' There is no solution ') elseif a==0 x = -c/b disp(' Only one root: equation is linear.') elseif b.^2 < 4.*a.*c disp(' Complex roots') elseif b.^2 == 4.*a.*c x = -b./(2.*a) Example: A program to solve for roots of quadratic equation given the coefficients a, b and c: Type this code in the editor and save it as quad_eqn.m.) disp(' Equal roots') else % b.^2 > 4.*a.*c x1 = (-b + sqrt(b.^2-4.*a.*c) )./(2.*a); x2 = (-b - sqrt(b.^2-4.*a.*c) )./(2.*a); disp(' x1,x2 the two distinct roots') disp([x1 x2]) end format % Step 4: Stop

6
Evaluation of the program A most important step: This program needs to be checked thoroughly! A most important step: This program needs to be checked thoroughly! This can be done with polyval(), a tool to evaluate polynomials. Substitute the coefficients and computed roots into polyval as follows: This can be done with polyval(), a tool to evaluate polynomials. Substitute the coefficients and computed roots into polyval as follows: coef = [a b c]; coef = [a b c]; roots_of_quad = polyval(coef,[x1,x2]) roots_of_quad = polyval(coef,[x1,x2]) Note: If results are zeros, then check is complete!

7
Exercise Evaluate the quad_eqn.m program as described in the previous slide: See next slide. Evaluate the quad_eqn.m program as described in the previous slide: See next slide. Executing the program numerous times evaluating the results and comparing with known answers is important. You must be convinced, that every time you use the code to compute roots of a quadratic equation, it does it correctly! Executing the program numerous times evaluating the results and comparing with known answers is important. You must be convinced, that every time you use the code to compute roots of a quadratic equation, it does it correctly!

8
% START OF CODE SLIDE 1 of 3 % Quadratic equation roots based % on the theoretical formula for % the roots. D.T.V... Feb % % a.* x.^2 + b.* x + c = 0 % Modifications are in yellow. % Step 1: Start clear;clc format compact disp(' ') disp(' Quadratic roots finder ') disp(' ') % Step 2: Input data A = input(' Specify coefficients as follows: [a, b, c] = '); a = A(1); b = A(2); c = A(3); Quadratic equation code with complex roots displayed & check

9
% Step 3: Evaluation of roots SLIDE 2 of 3 if a==0 & b==0 & c==0 disp(' Solution is indeterminate ') elseif a==0 & b==0 disp(' There is no solution ') elseif a==0 x = -c/b x1 = x; x2=x; disp(' Only one root: equation is linear.') elseif b.^2 < 4.*a.*c x1 = (-b + sqrt(b.^2-4.*a.*c) )./(2.*a) x2 = (-b - sqrt(b.^2-4.*a.*c) )./(2.*a) disp(' Complex roots') elseif b.^2 == 4.*a.*c x = -b./(2.*a) x1=x;x2=x; disp(' Equal roots') else % b.^2 > 4.*a.*c x1 = (-b + sqrt(b.^2-4.*a.*c) )./(2.*a); x2 = (-b - sqrt(b.^2-4.*a.*c) )./(2.*a); disp(' x1,x2 the two distinct roots') disp([x1 x2]) end

10
This is the check at the end: format disp('check') coef = [a b c]; roots_of_quad = polyval(coef,[x1,x2]) % Step 4: Stop SLIDE 3 of 3 Note: Evaluation and validation of a computer program is the most time consuming and necessary effort to ensure that the tool produces reliable and correct results.

11
Summary The structure plan approach to design computer codes for technical computing was introduced by an example. The structure plan approach to design computer codes for technical computing was introduced by an example. Input assignment was illustrated with and without the ‘input’ utility. Input assignment was illustrated with and without the ‘input’ utility. Graphical & tabular forms of output were shown. Graphical & tabular forms of output were shown. Illustrated array dot-operation, repetition ( for ) and decision ( if ) programming tools. Illustrated array dot-operation, repetition ( for ) and decision ( if ) programming tools.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google