Download Program assignment 4 CS 46 A

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Program assignment 1
Math 109
Due
Feb. 18
This will be graded separately and is worth 10 points. Printout your code
and your test runs and turn them at the beginning of class.
The classic quadratic formula, taught in every algebra class, sometimes is
not a wise way to solve a quadratic equation ax  bx  c  0 since, in
certain cases it can lose accuracy. A better method for solving a
quadratic equation is to compute
2


q   b  sgn( b) b 2  4ac / 2 where sgn(b) is 1 for b  0 and sgn(b) is -1
otherwise. Then two solutions of the quadratic equation are x1  q / a and
x2  c / q .
Implement this idea to solve ax  bx  c  0 .
Structure your code so that
it is a function in file quad_roots.m. The function should receive a, b,
and c as parameters and should return the roots of the equation as a
vector. For all possible values of a, b and c have your function identify
the kind of equation and print a statement that identifies the equation.
You will need to code the cases:
2
Case:
message:
a = 0, b = 0, c = 0: The equation is 0 = 0, any x solves the equation
a = 0, b = 0, c  0 : The equation c = 0 with c not 0, has no solution.
a = 0, b  0 : The equation is a linear equation.
a  0 : The equation is a quadratic equation.
Do not print out the roots from inside the function, only print out the
above messages.
Note that in the case of a linear equation you would not use the quadratic
formula (what formula is appropriate?) and x will have only one component.
In the case of a quadratic equation have your function return both the x
values from the new formula and the x values from the traditional formula
by using
function [x, xtraditional] = quad_roots(a,b,c)
as the first line of your m file. In the case of functions that are not
quadratic set xtraditional = [] in your code. Also in the case that there
is no solution or an infinite number of solutions set x = []. To call a
function with two output arguments from the Matlab prompt type, for
example,
[x, xtraditional]= quad_roots(1,3,2).
Both x and xtraditional will be returned to the Matlab workspace. Also
note that the names are not important and so one could also use the call:
[y,z]= quad_roots(1,3,2).
For functions argument passing in MATLAB (and, for functions, in nearly
every language) is by position not name. Note that for script files the
names are important since script file variables share the MATLAB workspace.
Also note that when b is 0 sgn(b) is different than MATLAB’s sign function.
Therefore if you choose to use MATLAB’s sign to calculate sgn(b) you will
need to modify MATLAB’s answer in the case that b is zero.
Finally I suggest that you code the traditional formula using:
d = b * b - 4 * a * c ;
xtraditional(1) = ( - b - sqrt(d) ) / ( 2 * a ) ;
xtraditional(2) = ( - b + sqrt(d) ) / ( 2 * a ) ;
xtraditional = sort(xtraditional);
The last line here will sort the components in increasing order. I
recommend that you also sort x in the same manner after x is calculated:
x = sort(x);
Use good programming style in your program: indent loops and if statements
appropriately, have comments through the program (every 5 to 10 lines or
so) and have leading comments in the style I suggested, with a clear
description of each input variable and each output variable.
Do the following 8 test runs:
a
0
0
1
1
1
1
1
1
b
0
3
2
-6
-7
.99999999999999
-1
-1.0e20
c
2
5
2
9
12
-1.0e-14
1.0e-14
1.0e20
In each case display x and xtraditional (by, for example, leaving off a
semicolon in the line calling your function) using format long e. For the
last three cases indicate which answer (new or traditional) is more
accurate. Also briefly describe how one can tell, without knowing the true
roots, which solution is better. (Hint: (x – r1)(x – r2) = x2 – (r1 + r2)x +
r1 r2 )