Lecture 03 Matrices - NYU Polytechnic School of Engineering

Download Report

Transcript Lecture 03 Matrices - NYU Polytechnic School of Engineering

Lecture 4
Structure plan
Program
design process
Basic elements of code:
–Input (assignment of variables)
–Sequence of expressions
–Output (graphical or tabular)
Structured
© 2007 Daniel Valentine. All rights reserved. Published by
Elsevier.
approach to design
Review of basic elements

Input
– Initialize, define, request, or assign numerical
values to variables.

Set of commands
– Operations applied to input variables that lead
to the desired result.

Output
– Display (graphically or numerically) result.
A technical computing problem
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:

a .* x.^2 + b .* x + c = 0

The structure plan is provided in the text and
will be utilized in this lecture.
Structure plan

1.
2.
3.
4.
5.
6.
7.
8.
Develop the structure plan, e.g.:
Start (summary of Fig. 3.3 in the text)
Input data (a, b, c)
If a = 0, b = 0, and c = 0, indeterminate.
If a = b = 0, no solution.
If a = 0, x = -c/b; one root; equation is linear.
If b^2 < 4ac, roots are complex.
If b^2 = 4ac, roots are equal, x = b/(2a).
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.
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.)
% 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 ')
disp(' Equal roots')
elseif a==0 & b==0
else % b.^2 > 4.*a.*c
disp(' There is no solution ')
x1 = (-b + sqrt(b.^2-4.*a.*c) )./(2.*a);
elseif a==0
x2 = (-b - sqrt(b.^2-4.*a.*c) )./(2.*a);
x = -c/b
disp(' x1,x2 the two distinct roots')
disp(' Only one root: equation is linear.')
disp([x1 x2])
elseif b.^2 < 4.*a.*c
end
disp(' Complex roots')
format
elseif b.^2 == 4.*a.*c
% Step 4: Stop
x = -b./(2.*a)
Evaluation of the program
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:

coef = [a b c];
roots_of_quad = polyval(coef,[x1,x2])
Note: If results are zeros, then check is complete!
Exercise
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!
Quadratic equation code with
complex roots displayed & check
% START OF CODE 
SLIDE 1 of 3
% Quadratic equation roots based
% on the theoretical formula for
% the roots.
D.T.V. .. Feb.2007.
%
%
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);
% 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
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.
Summary
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.
 Graphical & tabular forms of output were
shown.
 Illustrated array dot-operation, repetition
(for) and decision (if) programming
tools.
