Exercises II - using functions
■ Write a program for numerical integration of a given function using some prescribed method which will be given by a formula. Follow these 3 steps:
-
write a function f which returns a value of the given function
(like esin(2x+3) or square root of (x2 + 7x)) -
write a function Numint for numerical integration using the given formula (like left or right Riemann sum or trapezoidal rule):
- input:
a, b ... the interval of integration
n ... the number of integration points inside the interval - output:
ierr ... indicator of an error (if n < 0 or f(x) not defined in some of the integration points)
sumf ... definite integral of f(x) from a to b (only if no error occured)
- input:
-
use the function Numint in the main program, which
- asks for the values of a, b and n
- prints the value of the integral (or information about the error)
■ Write a program for solving the equation x = f(x) for a given function f(x), using the method of simple iteration (fixed point iterations). Follow these 3 steps:
- write a function f which returns a value of the given function (like 0.2 sin(2x+3) or 0.5 cos(x))
-
write a function Solve for solving the equation x = f(x) using the function f defined above and the method of fixed point iterations: xi+1 = f(xi)
- input:
xo ... the first approximation of the solution
eps ... the precision required: |f(x) - x| < eps
maxit ... maximal number of iterations - output:
x ... the approximate solution
nit ... number of iterations (or 0, if maxit would be exceeded)
- input:
-
use the function Solve in the main program, which
- asks for value of xo and eps (maxit will be defined as a constant in the main program)
- prints the value of the solution x and number of iterations required for its computation (or a message, that solution with precision eps was not found in maxit iterations)
Set maxit = 100 and test the program for eps = 0.0001 and the starting value xo = 0
■ Write a program for computation of quantities and price of a fertilizer for a given size of a garden. The fertilizer is sold in 2 types of cans: either 1 kg for 5$ or 3 kg for 12$. One square meter of a garden needs 0.1 kg of the fertilizer. Follow these two steps:
-
write a function fert which computes the cheapest combination of cans for a given size of a garden:
- input:
w1, w2 ... weights of cans (kg)
p1, p2 ... prices of cans ($)
g_size ... size of a garden (m2)
d_m ... dose in kg for m2 - output:
nw1, nw2 ... number of cans of the two types
t_am ... total amount
- input:
-
use the function fert in the main program, which in a loop
- asks for the size of a garden (weights and prices of the cans and the dosage of the fertilizer are expected to be defined as constants in the main program)
- prints numbers of 1 kg and 3 kg types of cans needed and the total price
■ Write a program which solves vector equation x = U x + v using the method of simple iteration, where U is n x n matrix and x and v are column vectors of dimension n. Follow these two steps:
- write a function Iter for solving the equation x = U x + v using the method of fixed point iterations (i.e. xi+1 = U xi + v ) until the condition |xi+1 - xi| < eps is satisfied or until the maximal number of iterations maxit is exceeded
- input:
xo ... the first approximation of the solution
U, v ... the given matrix and column vector
eps ... the precision required: |xi+1 - xi| < eps
maxit ... maximal number of iterations - output:
x ... the approximate solution
nit ... number of iterations (or 0, if maxit was exceeded)
- input:
-
use the function Iter in the main program, which
- reads the matrix U and the vector v from the keyboard or from a file (maxit and eps are expected to be defined as constants in the program)
- chooses an initial approximation xo as zero vector and solves approximately the equation x = U x + v
- prints the approximation of the solution x and how many iterations was needed for its computation (or a message, that solution with precision eps was not found in maxit iterations)