My seminar paper for the Numerical Methods class of professor Desa Radunović, during my undergraduate studies of
Mathematics and Computer Science at the University of Belgrade, Faculty of Mathematics. It solves the boundary value problem p(x)u″ + q(x)u′ + r(x)u = f(x), u(a) = c, u(b) = d, approximately, by a
polynomial of degree n that meets the two boundary conditions, found by three variational methods: Galerkin's,
collocation, and least squares. It draws the result in VGA graphics and, given the exact solution, draws that too and
measures the error for every degree from 2 to 8. The functions are typed in as text, like sin(x)+x*x. It runs
here as it did in DOS, in Serbian, with the English translation beside it. Click the screen and type, or use the
buttons.
The menu (MENU.C) and the expression evaluator (EVALL.C) are Vuksan Pejović's
libraries, as is the symbolic differentiation in DIFF/, which an earlier version of the program used.
P saves the equation to a file and loads it back; after a solution, D saves it to a file of results, adding to it. The files are kept here while the page is open.
The solution is sought as u = w + Σ ckφk, k = 2 … n. Here w = −koef1 − koef2·x is the
straight line through the two boundary values, and φk(x) = xk−2(x − a)(x − b) are zero at both ends,
so any ck keep the boundary conditions. Putting u into the equation leaves a residual, and the three methods
choose the ck differently: Galerkin's makes the residual orthogonal to every φj; collocation makes it
zero at n − 1 points spread over [a, b]; least squares makes the integral of its square as small as possible. Each gives n − 1
linear equations. The program builds the functions it integrates as text, as ((1)*2+(2)*(2*x-1)+(3)*(x*x-x))*(x*x-x),
has Vuksan's evaluator parse them, integrates them by Simpson's rule, doubling the pieces until the estimated error is below
0.00001, and solves the equations by Gaussian elimination.
(x*x-x-2), read as
x² − x + 2. Then every method is wrong: for u″ + u = 0, u(−1) = u(2) = 1, all three give about 1.6 at x = 0.5, where
the exact solution is 14.14. The evaluator also can't take more than 31 numbers in one expression, since it stands each
for a character below 32, fails a power of a power after anything longer than one character, as in
(x+1)^2^3, and writes one byte past the memory it asks for.if (odstoj[n2-2]>0)
makes only the first line conditional, so where the error is 0, or the system couldn't be solved, a bar is drawn
anyway, with the height of the one before; the first one with a height never set at all. The errors are also capped at
10, to keep the chart readable, for every n but 2.Fixed corrects all of these, with a − b − c and a / b / c read left to right, any number of constants, and exp(x) in the small parameter's solution. At the higher degrees, Galerkin's method can still go astray, as the basis functions grow nearly alike; that is the method with this basis, not a bug.
The port was checked against the original C, compiled with a simulated DOS screen, keyboard and BGI, in 112 sessions: every method and degree, equations, intervals, exact solutions, the error charts, saving and loading. In 109 the screens, the drawing and the files are the same. Borland C computed in 80 bits and JavaScript computes in 64, so the C was also compiled with 80-bit numbers. The one place where that changed what the program shows is the largest error above, and the page does that in 80 bits too; that accounts for two of the other three sessions. In the third, five pixels of a curve fall exactly between two rows. The 80-bit build differs besides only in the bars drawn with a height that was never set.