Information Technology Reference
In-Depth Information
for object-oriented programming, while Matlab offers this programming technique
as a more recent add-on to the language. On the other hand, Matlab allows the use
of Java classes mixed with Matlab statements. This is a convenient way of introduc-
ing object-oriented programming in Matlab code. In our example, we could reuse
theJavaclasses Integration prm , Integration , Trapezoidal and so on, from
Sect. 6.5.4 , but we shall not go further into this topic here.
6.6
Exercises
Exercise 6.1. Write a mathematical pseudo code for solving the quadratic algebraic
equation
ax 2 C bxC c D 0:
The algorithm should take a , b ,and c as input, and return one or two roots.
˘
Exercise 6.2. A straightforward mathematical pseudo code from Exercise 6.1 can
fail if a D 0 . Also, if a D 0 and b D 0 , c must equal zero. Extend the pseudo code
with appropriate tests on a D 0 and a D b D 0 .
˘
Exercise 6.3. The roots in the algorithm in Exercise 6.1 can be real or complex,
depending on the sign of b 2 4ac . Many programming languages handle the square
roots of negative numbers correctly, i.e., compute the roots as complex numbers.
However, some languages do not support complex numbers well, and the algorithm
should in such cases ensure that only real arithmetic is involved. Modify the pseudo
code in Exercise 6.1 such that the roots are represented by four real numbers, the
real and imaginary parts of the two roots. Make sure that all operations involve real
numbers only.
˘
Exercise 6.4. Implement Algorithm 6.6 in a computer program. Find a suitable test
problem for verifying the implementation.
˘
Exercise 6.5. Work through Algorithm 6.9 by hand for the choices a D 0 , b D 1 ,
f.x/ D x 3 , and an initial set of points between x 0 D 0 and x 1 D 1 .Let E
correspond to the error in the trapezoidal rule (6.12), and set D 0:01 .
˘
Exercise 6.6. Implement Algorithm 6.9 in a computer program. Find a test problem
that can be used to verify the implementation.
˘
Exercise 6.7. Calling the heun function in Algorithm 6.11 N times, integrating
from 0 to some time level in each call, was pointed out as a very inefficient way
of producing a set of data points ( it; u .it/ ). Find out how you can produce these
data points more efficiently, still using the heun function in Algorithm 6.11 (hint:
perform one step at a time). Count the number of f -function evaluations in the two
approaches.
˘
Search WWH ::




Custom Search