讲解EEEN20010、辅导C/C++设计、讲解Computer Engineering、辅导C/C++程序语言

- 首页 >> C/C++编程

EEEN20010 Computer Engineering I

“Find root of a cubic function”

(root)

You are expected to demonstrate a working C program according to the description below. Your solution should be

uploaded (via Blackboard) before the deadline. You must use the file name specified in the problem description below.

Late submissions will not be corrected and will receive zero credit, so even if your program is not running correctly, you

should submit your “best attempt”.

Program description

The cubic function

f x  ax bx cx d

3 2

( )

achieves the value 0 for certain values of x (called the roots of the

cubic) – these depend on the parameters a, b, c, and d. Note that unlike a quadratic function, a cubic function is

guaranteed to have at least one real root. Your job is to numerically find one root of the cubic using an efficient search

method called “the bisection method”. This works as follows.

The bisection method operates by creating an interval that contains the root we seek, and then repeatedly shrinking that

interval so that we “home in” on the solution. First, we choose two numbers,

l

(marked as “guess 1” on the diagram

below), and

u

(marked as “guess 2” on the diagram). For starters, you may choose, for example,

l 10

and

u 10.

These guesses are chosen such that

f (l)

and

f (u)

have different signs (this may be seen in the diagram). This means

the root we seek must lie in the interval between

l and u . The trick underpinning the bisection method is to consider the

midpoint

(also marked on the diagram). If, as shown in the diagram,

f (x)

changes sign between

m and u , then the root must lie between these two points, and therefore our interval should shrink accordingly – we can

achieve this by setting

m

to be the new value of

l

. If, on the other hand,

f (x)

changes sign between

l

and

m , then

the root must lie between these two points – so we set

m

to be the new value of

u . We continue this procedure until the

search interval becomes extremely small, at which point the value of

m

will provide a good estimate of the root.

The program should be called root.c and program execution should look like the following (user input in bold):

Welcome to the cubic root estimator.

This estimates the value of one root of

f(x)=ax^3+bx^2+cx+d.

Enter the coefficients in the form “a b c d”: 1 -3 -3 -4

There is a root at: x = 4.000

Do you wish to try another cubic [y/n]: n

Note 1: You may set your upper and lower limits to +N and -N respectively; note that for a sufficiently large

value of N, the cubic

f (x)

must change sign between these limits. For extra credit, you can try to design a

method which will ensure that the initial interval is chosen to be large enough so that a root is always found.


站长地图