Initial commit of "Rocket Workbench".
Taken from sources in CVS at:
https://sourceforge.net/projects/rocketworkbench/
Sources extracted in two steps:
1. Pull entire project tree into a subdir "rwb" via "rsync":
rsync -a a.cvs.sourceforge.net::cvsroot/rocketworkbench/ rwb/.
2. Export sources:
export CVSROOT=$(pwd)/rwb
SUBDIRS="analyser cpropep cpropep-web CVSROOT data libcompat libcpropep libnum libsimulation libthermo prop rocketworkbench rockflight"
mkdir rwbx; cd rwbx
cvs export -D now ${SUBDIRS}
After this (and some backups for safety), the directory content was
added to a Git repo:
git init .
git add *
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
ROOT = ../../..
|
||||
include $(ROOT)/config/presrc.mk
|
||||
@@ -0,0 +1,43 @@
|
||||
CC = bcc32
|
||||
CPP32 = cpp32
|
||||
LIBRARIAN = tlib
|
||||
LINKER = ilink32
|
||||
RC = brc32
|
||||
|
||||
|
||||
COPT = -3 -O2 -w-8012 -w-8004 -w-8057 -IC:\borland\bcc55\include
|
||||
OBJS = lu.obj rk4.obj general.obj print.obj sec.obj
|
||||
|
||||
TLIBNUM = +lu.obj +rk4.obj +general.obj +print.obj +sec.obj
|
||||
|
||||
LDOPT = -LC:\borland\bcc55\lib
|
||||
|
||||
PROG = test.exe
|
||||
PROGOBJS = test.obj
|
||||
|
||||
LIBNUM = libnum.lib
|
||||
|
||||
.SUFFIXES: .c
|
||||
|
||||
all: $(LIBNUM) $(PROG)
|
||||
|
||||
.c.obj:
|
||||
$(CC) $(DEF) $(COPT) -c $*.c -o $*.obj
|
||||
|
||||
$(LIBNUM): $(OBJS)
|
||||
tlib $@ $(TLIBNUM)
|
||||
|
||||
$(PROG): $(PROGOBJS)
|
||||
$(CC) $(LDOPT) $(LIBNUM) $(PROGOBJS)
|
||||
|
||||
clean:
|
||||
del *.obj
|
||||
del *.bak
|
||||
del *.tds
|
||||
|
||||
deep-clean: clean
|
||||
del $(LIBNUM)
|
||||
del $(PROG)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
/* num.h - Library of numerical method
|
||||
* $Id: num.h,v 1.1.1.1 2001/07/20 03:19:08 antoine Exp $
|
||||
* Copyright (C) 2000
|
||||
* Antoine Lefebvre <antoine.lefebvre@polymtl.ca>
|
||||
*
|
||||
* Licensed under the GPL
|
||||
*/
|
||||
|
||||
#ifndef num_h
|
||||
#define num_h
|
||||
|
||||
/* NOTE on matrix representation
|
||||
* -----------------------------
|
||||
* All matrix should be allocate the following way
|
||||
*
|
||||
* matrix = (double *) malloc (sizeof(double) * line * column)
|
||||
*
|
||||
* to access the value at line 2, column 4, you do it like that
|
||||
*
|
||||
* matrix[2 + 4*line]
|
||||
*/
|
||||
|
||||
|
||||
/* This type is used as function pointer for the
|
||||
* sysnewton algorithm.
|
||||
*/
|
||||
typedef double (*func_t)(double *x);
|
||||
|
||||
typedef struct status
|
||||
{
|
||||
|
||||
int itn;
|
||||
|
||||
} status_t;
|
||||
|
||||
#define NO_CONVERGENCE 1
|
||||
#define NO_SOLUTION 2
|
||||
|
||||
|
||||
/* Find the solution of linear system of equation using the
|
||||
* LU factorisation method as explain in
|
||||
* 'Advanced engineering mathematics' bye Erwin Kreyszig.
|
||||
*
|
||||
* This algorithm will also do column permutation to find
|
||||
* the larger pivot.
|
||||
*
|
||||
* ARGUMENTS
|
||||
* ---------
|
||||
* matrix: the augmented matrix of coefficient in the system
|
||||
* with right hand side value.
|
||||
*
|
||||
* solution: the solution vector
|
||||
*
|
||||
* neq: number of equation in the system
|
||||
*
|
||||
* Antoine Lefebvre
|
||||
* february 6, 2000 Initial version
|
||||
* october 20, 2000 revision of the permutation method
|
||||
*/
|
||||
int NUM_lu(double *matrix, double *solution, int neq);
|
||||
/*int old_lu(double *matrix, double *solution, int neq);*/
|
||||
|
||||
/* This function print the coefficient of the matrix to
|
||||
* the screen.
|
||||
*
|
||||
* matrix: should be an augmented matrix
|
||||
* neq : number of equation
|
||||
*/
|
||||
int NUM_print_matrix(double *matrix, int neq);
|
||||
|
||||
/* Print the coefficient of the square matrix to the screen
|
||||
*/
|
||||
int NUM_print_square_matrix(double *matrix, int neq);
|
||||
|
||||
|
||||
/* This function print the contents of the vector to
|
||||
* the screen.
|
||||
*
|
||||
* vec: vector containing neq element
|
||||
*/
|
||||
int NUM_print_vec(double *vec, int neq);
|
||||
|
||||
|
||||
/**************************************************************
|
||||
FUNCTION: This function solve systems of ODE of the first order
|
||||
with the Runge-Kutta method of the fourth order.
|
||||
|
||||
PARAMETER: The first parameter is a pointer to the function we
|
||||
we want to solve. This function take five parameter
|
||||
neq is the number of equations in the system
|
||||
time is the time at which we want to evaluate
|
||||
y is an array containing an initial value
|
||||
dy will store the result of the function
|
||||
ierr any error field
|
||||
|
||||
step is the time variation
|
||||
duration is the total time of the simulation
|
||||
ic are the initial conditions
|
||||
**y is an array containing all the data
|
||||
|
||||
void * is nay user data
|
||||
|
||||
COMMENTS: **y must be properly allocated, [number of points]X[neq]
|
||||
|
||||
It could be interesting to add a tolerance and use
|
||||
variable step to reach our tolerance instead of using a
|
||||
fixed step.
|
||||
|
||||
AUTHOR: Antoine Lefebvre
|
||||
|
||||
DATE: February 11
|
||||
*****************************************************************/
|
||||
int NUM_rk4(int (*f)(int neq, double time, double *y, double *dy, void *data),
|
||||
int neq, double step, double duration, float *ic,
|
||||
double **y, void *data );
|
||||
|
||||
int NUM_rkf(int (*f)(int neq, double time, double *y, double *dy, void *data),
|
||||
int neq, double step, double duration, float *ic,
|
||||
double **y, double epsil, void *data);
|
||||
|
||||
/* this function return the nearest integer to a */
|
||||
/* it is a replacement of rint which is not ANSI complient */
|
||||
int Round(double a);
|
||||
|
||||
double epsilon(void);
|
||||
|
||||
int NUM_sec(double (*f)(double x), double x0, double x1, int nmax,
|
||||
double epsilon, double *ans);
|
||||
|
||||
int NUM_newton(double (*f)(double x), double (*df)(double x), double x0,
|
||||
int nmax, double epsilon, double *ans);
|
||||
|
||||
int NUM_ptfix(double (*f)(double x), double x0,
|
||||
double nmax, double epsilon, double *ans);
|
||||
|
||||
int NUM_sysnewton(func_t *Jac, func_t *R, double *x, int nvar,
|
||||
int nmax, double eps);
|
||||
|
||||
|
||||
int trapeze(double *data, int n_point, int col, int off, double *integral);
|
||||
|
||||
int simpson(double *data, int n_point, int col, int off, double *integral);
|
||||
|
||||
int create_spline(double *data, int n_point, double *spline);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ROOT = ../../../..
|
||||
include $(ROOT)/config/makedef
|
||||
include $(ROOT)/config/libsrc.mk
|
||||
@@ -0,0 +1,42 @@
|
||||
|
||||
ROOT = ../../../..
|
||||
|
||||
include $(ROOT)/config/makedef
|
||||
|
||||
|
||||
LIB = -lnum -lm
|
||||
LIBDIR = -L$(ROOT)/lib/
|
||||
INCDIR = -I$(ROOT)/source/libraries/libnum/include/
|
||||
|
||||
DEF = -DLINUX
|
||||
|
||||
PROG = test
|
||||
OBJS = test.o
|
||||
|
||||
LIBOBJS = lu.o rk4.o rkf.o general.o print.o sec.o newton.o ptfix.o\
|
||||
sysnewton.o trapeze.o simpson.o spline.o
|
||||
|
||||
LIBNUM = libnum.a
|
||||
|
||||
all: $(LIBNUM) $(PROG)
|
||||
|
||||
.c.o:
|
||||
@echo $<
|
||||
$(CC) $(DEF) $(INCDIR) $(COPT) -c $< -o $@
|
||||
|
||||
$(LIBNUM): $(LIBOBJS)
|
||||
ar -r $@ $(LIBOBJS)
|
||||
ranlib $@
|
||||
mv $(LIBNUM) $(ROOT)/lib/
|
||||
|
||||
|
||||
$(PROG): $(LIBNUM) $(OBJS)
|
||||
$(CC) $(COPT) $(OBJS) $(LIBDIR) $(LIB) -o $@
|
||||
|
||||
|
||||
clean:
|
||||
@rm -f $(PROG) *.o *~
|
||||
|
||||
deep-clean: clean
|
||||
rm -f $(ROOT)/lib/$(LIBNUM)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
int Round(double a)
|
||||
{
|
||||
int t = a;
|
||||
|
||||
if (a - (double)t < 0.5)
|
||||
return t;
|
||||
else if (a - (double)t > 0.5)
|
||||
return t + 1;
|
||||
else
|
||||
return (t + (t % 2));
|
||||
}
|
||||
|
||||
|
||||
double epsilon(void)
|
||||
{
|
||||
double epsilon;
|
||||
double temp;
|
||||
|
||||
epsilon = 1.0;
|
||||
temp = epsilon + 1.0;
|
||||
|
||||
while (temp > 1.0)
|
||||
{
|
||||
epsilon = epsilon / 2.0;
|
||||
temp = epsilon + 1.0;
|
||||
}
|
||||
return (epsilon * 2.0);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/* lu.c - PA = LU factorisation with pivoting
|
||||
* $Id: lu.c,v 1.1.1.1 2001/07/20 03:19:08 antoine Exp $
|
||||
* Copyright (C) 2000
|
||||
* Antoine Lefebvre <antoine.lefebvre@polymtl.ca>
|
||||
*
|
||||
*
|
||||
* Licensed under the GPLv2
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
/*
|
||||
|
||||
This algorithm will compute an LU factorisation on the augmented
|
||||
matrix (A) passed in arguments.
|
||||
|
||||
This algorithm assumed the element on the diagonal of the lower
|
||||
triangular matrix set to 1.
|
||||
|
||||
In order to save memory space, every coefficient of both matrix
|
||||
L and U are written in the original matrix overwriting initial
|
||||
values of A.
|
||||
|
||||
*/
|
||||
|
||||
int NUM_lu(double *matrix, double *solution, int neq)
|
||||
{
|
||||
int i, j, k;
|
||||
|
||||
int idx; /* index of the larger pivot */
|
||||
double big; /* the larger pivot found */
|
||||
double tmp = 0.0;
|
||||
|
||||
int *P; /* keep memory of permutation (column permutation) */
|
||||
double *y;
|
||||
|
||||
P = (int *) calloc (neq, sizeof(int));
|
||||
y = (double *) calloc (neq, sizeof(double));
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
solution[i] = 0; /* reset the solution vector */
|
||||
P[i] = i; /* initialize permutation vector */
|
||||
}
|
||||
|
||||
/* LU Factorisation */
|
||||
|
||||
for (i = 0; i < neq - 1; i++) /* line */
|
||||
{
|
||||
for (j = i; j < neq; j++) /* column */
|
||||
{
|
||||
tmp = 0.0;
|
||||
for (k = 0; k < i; k++)
|
||||
tmp += matrix[i + neq*P[k]] * matrix[k + neq*P[j]];
|
||||
|
||||
matrix[i + neq*P[j]] = matrix[i + neq*P[j]] - tmp;
|
||||
}
|
||||
|
||||
/* find the larger pivot and interchange the columns */
|
||||
big = 0.0;
|
||||
idx = i;
|
||||
for (j = i; j < neq; j++)
|
||||
{
|
||||
if (big < fabs(matrix[i + neq*P[j]])) /* we found a larger pivot */
|
||||
{
|
||||
idx = j;
|
||||
big = fabs(matrix[i + neq*P[j]]);
|
||||
}
|
||||
}
|
||||
/* check if we have to interchange the lines */
|
||||
if (idx != i)
|
||||
{
|
||||
tmp = P[i];
|
||||
P[i] = P[idx];
|
||||
P[idx] = tmp;
|
||||
}
|
||||
|
||||
if (matrix[i + neq*P[i]] == 0.0)
|
||||
{
|
||||
printf("LU: matrix is singular, no unique solution.\n");
|
||||
return NO_SOLUTION;
|
||||
}
|
||||
|
||||
for (j = i+1; j < neq; j++)
|
||||
{
|
||||
tmp = 0.0;
|
||||
for (k = 0; k < i; k++)
|
||||
tmp += matrix[j + neq*P[k]] * matrix[k + neq*P[i]];
|
||||
|
||||
matrix[j + neq*P[i]] = (matrix[j + neq*P[i]] - tmp)/matrix[i + neq*P[i]];
|
||||
}
|
||||
}
|
||||
|
||||
i = neq - 1;
|
||||
tmp = 0.0;
|
||||
for (k = 0; k < neq-1; k++)
|
||||
tmp += matrix[i + neq*P[k]] * matrix[k + neq*P[i]];
|
||||
|
||||
matrix[i + neq*P[i]] = matrix[i + neq*P[i]] - tmp;
|
||||
|
||||
|
||||
/* End LU-Factorisation */
|
||||
|
||||
/* substitution for y Ly = b*/
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
tmp = 0.0;
|
||||
for (j = 0; j < i; j++)
|
||||
tmp += matrix[i + neq*P[j]] * y[j];
|
||||
|
||||
y[i] = matrix[i + neq*neq] - tmp;
|
||||
}
|
||||
|
||||
/* substitution for x Ux = y*/
|
||||
for (i = neq - 1; i >=0; i--)
|
||||
{
|
||||
if (matrix[i + neq*P[i]] == 0.0)
|
||||
{
|
||||
printf("LU: No unique solution exist.\n");
|
||||
return NO_SOLUTION;
|
||||
}
|
||||
|
||||
tmp = 0.0;
|
||||
for (j = i; j < neq; j++)
|
||||
tmp += matrix[i + neq*P[j]] * solution[P[j]];
|
||||
|
||||
solution[P[i]] = (y[i] - tmp)/matrix[i + neq*P[i]];
|
||||
}
|
||||
|
||||
free (y);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* This function will divide each row of the matrix
|
||||
* by the highest element of this row.
|
||||
* This kind of scaling could improve precision while
|
||||
* solving some difficult matrix.
|
||||
*/
|
||||
int NUM_matscale(double *matrix, int neq)
|
||||
{
|
||||
int i; /* line */
|
||||
int j; /* column */
|
||||
|
||||
double val;
|
||||
double tmp;
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
val = 0;
|
||||
/* find the highest value */
|
||||
for (j = 0; j < neq; j++)
|
||||
{
|
||||
tmp = abs(matrix[i + neq*j]);
|
||||
val = (tmp > val) ? tmp : val;
|
||||
}
|
||||
|
||||
/* divide element of the line by this value
|
||||
* including the right side
|
||||
* if the max value is defferent than zero
|
||||
*/
|
||||
if (val != 0.0)
|
||||
{
|
||||
for (j = 0; j < neq+1; j++)
|
||||
matrix[i + neq*j] = matrix[i + neq*j]/val;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
LIBNAME = libnum.a
|
||||
LIBOBJS = lu.o rk4.o rkf.o general.o print.o sec.o newton.o ptfix.o\
|
||||
sysnewton.o trapeze.o simpson.o spline.o
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
int NUM_newton(double (*f)(double x), double (*df)(double x), double x0,
|
||||
int nmax, double eps, double *ans)
|
||||
{
|
||||
|
||||
int i = 0;
|
||||
double x1;
|
||||
double delta;
|
||||
|
||||
do
|
||||
{
|
||||
if (i >= nmax)
|
||||
{
|
||||
return NO_CONVERGENCE;
|
||||
}
|
||||
|
||||
x1 = x0 - f(x0)/df(x0);
|
||||
delta = fabs(x1 - x0)/fabs(x1);
|
||||
|
||||
x0 = x1;
|
||||
i++;
|
||||
|
||||
} while (delta > eps);
|
||||
|
||||
*ans = x1;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/* print.c
|
||||
* $Id: print.c,v 1.1.1.1 2001/07/20 03:19:08 antoine Exp $
|
||||
* Copyright (C) 2000
|
||||
* Antoine Lefebvre <antoine.lefebvre@polymtl.ca>
|
||||
*
|
||||
* Licensed under the GPL
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
int NUM_print_square_matrix(double *matrix, int neq)
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
for (j = 0; j < neq; j++)
|
||||
printf("% .5f ", matrix[i + neq*j]);
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int NUM_print_matrix(double *matrix, int neq)
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
for (j = 0; j <= neq; j++)
|
||||
printf("% .5e ", matrix[i + neq*j]);
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int NUM_print_vec(double *vec, int neq)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < neq; i++)
|
||||
printf("% .5e ", vec[i]);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
int NUM_ptfix(double (*f)(double x), double x0, double nmax,
|
||||
double epsilon, double *ans)
|
||||
{
|
||||
|
||||
int i = 0;
|
||||
double x1;
|
||||
double delta;
|
||||
|
||||
do
|
||||
{
|
||||
if (i >= nmax)
|
||||
{
|
||||
return NO_CONVERGENCE;
|
||||
}
|
||||
|
||||
x1 = f(x0);
|
||||
|
||||
delta = fabs(x1 - x0)/fabs(x1);
|
||||
x0 = x1;
|
||||
i++;
|
||||
|
||||
} while (delta > epsilon);
|
||||
|
||||
*ans = x1;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
|
||||
int NUM_rk4(int (*f)(int neq, double time, double *y, double *dy, void *data),
|
||||
int neq, double step, double duration, float *ic,
|
||||
double **y, void *data)
|
||||
{
|
||||
int i;
|
||||
int n;
|
||||
|
||||
int status;
|
||||
|
||||
int length;
|
||||
|
||||
double t = 0.0;
|
||||
|
||||
double *tmp;
|
||||
double *dy;
|
||||
double *K1, *K2, *K3, *K4;
|
||||
|
||||
double *a;
|
||||
|
||||
tmp = (double *) malloc(sizeof(double) * neq);
|
||||
dy = (double *) malloc(sizeof(double) * neq);
|
||||
|
||||
K1 = (double *) malloc(sizeof(double) * neq);
|
||||
K2 = (double *) malloc(sizeof(double) * neq);
|
||||
K3 = (double *) malloc(sizeof(double) * neq);
|
||||
K4 = (double *) malloc(sizeof(double) * neq);
|
||||
|
||||
/* allocation of the answer vector */
|
||||
length = (int)ceil(duration/step) + 1;
|
||||
a = *y = (double *) malloc(sizeof(double) * neq * length);
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
tmp[i] = a[i + neq*0] = (double) ic[i]; /* initials conditions */
|
||||
}
|
||||
|
||||
for (n = 0; n < Round(duration/step); n++)
|
||||
{
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t, tmp, dy, data);
|
||||
|
||||
if (status)
|
||||
return status;
|
||||
|
||||
K1[i] = step * dy[i];
|
||||
|
||||
tmp[i] = a[i + neq*n] + K1[i]/2; /* for the next step */
|
||||
}
|
||||
|
||||
/* FIXME: verify the coefficient t + step/2 */
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t + step/2, tmp, dy, data);
|
||||
|
||||
if (status)
|
||||
return -1;
|
||||
|
||||
K2[i] = step * dy[i];
|
||||
|
||||
tmp[i] = a[i + neq*n] + K2[i]/2;
|
||||
}
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t + step/2, tmp, dy, data);
|
||||
|
||||
if (status)
|
||||
return status;
|
||||
|
||||
K3[i] = step * dy[i];
|
||||
|
||||
tmp[i] = a[i + neq*n] + K3[i];
|
||||
}
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t + step, tmp, dy, data);
|
||||
if (status)
|
||||
return -1;
|
||||
|
||||
K4[i] = step * dy[i];
|
||||
}
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
a[i + neq*(n+1)] = a[i + neq*n] +
|
||||
(1.0/6.0)*(K1[i] + 2.0*K2[i] + 2.0*K3[i] + K4[i]);
|
||||
|
||||
t = t + step;
|
||||
}
|
||||
|
||||
free(tmp);
|
||||
free(dy);
|
||||
free(K1);
|
||||
free(K2);
|
||||
free(K3);
|
||||
free(K4);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
/* neq: number of equations
|
||||
* step: initial time step
|
||||
* duration: total duration of the simulation
|
||||
* ic: initial conditions vector
|
||||
* y: solution vector
|
||||
* epsil: precision on the data
|
||||
* data: pointer to data structure
|
||||
*/
|
||||
|
||||
/* Runge-Kutta-Fehlberg 4-5 order */
|
||||
|
||||
int NUM_rkf(int (*f)(int neq, double time, double *y, double *dy, void *data),
|
||||
int neq, double step, double duration, float *ic,
|
||||
double **y, double epsil, void *data)
|
||||
{
|
||||
int i, n;
|
||||
int col;
|
||||
int status;
|
||||
|
||||
double h;
|
||||
double t = 0.0;
|
||||
|
||||
double *a;
|
||||
|
||||
double *tmp;
|
||||
double *dy;
|
||||
double *K1, *K2, *K3, *K4, *K5, *K6;
|
||||
|
||||
double *E; /* Error vector of the error */
|
||||
|
||||
double err; /* maximum error */
|
||||
double beta;
|
||||
|
||||
tmp = (double *) malloc(sizeof(double) * neq);
|
||||
dy = (double *) malloc(sizeof(double) * neq);
|
||||
|
||||
E = (double *) malloc(sizeof(double) * neq);
|
||||
|
||||
K1 = (double *) malloc(sizeof(double) * neq);
|
||||
K2 = (double *) malloc(sizeof(double) * neq);
|
||||
K3 = (double *) malloc(sizeof(double) * neq);
|
||||
K4 = (double *) malloc(sizeof(double) * neq);
|
||||
K5 = (double *) malloc(sizeof(double) * neq);
|
||||
K6 = (double *) malloc(sizeof(double) * neq);
|
||||
|
||||
|
||||
h = step;
|
||||
n = 0;
|
||||
|
||||
col = neq + 1;
|
||||
|
||||
a = *y = (double *) malloc(sizeof(double) * col * (n + 1));
|
||||
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
tmp[i] = a[i + col*0] = (double) ic[i]; /* initial conditions */
|
||||
}
|
||||
a[neq] = t;
|
||||
|
||||
while (t < duration)
|
||||
{
|
||||
a = *y = (double *) realloc(*y, sizeof(double) * col * (n + 2));
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t, tmp, dy, data);
|
||||
K1[i] = h * dy[i];
|
||||
|
||||
tmp[i] = a[i + col*n] + K1[i]/4.0; /* for the next step */
|
||||
}
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t + h/4.0, tmp, dy, data);
|
||||
K2[i] = h * dy[i];
|
||||
|
||||
tmp[i] = a[i + col*n] + 3.0*K1[i]/32.0 + 9.0*K2[i]/32.0;
|
||||
|
||||
}
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t + 3.0*h/8.0, tmp, dy, data);
|
||||
K3[i] = h * dy[i];
|
||||
|
||||
tmp[i] = a[i + col*n] + 1932.0*K1[i]/2197.0 - 7200.0*K2[i]/2197.0 +
|
||||
7296.0*K3[i]/2197.0;
|
||||
}
|
||||
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t + 12.0*h/13.0, tmp, dy, data);
|
||||
K4[i] = h * dy[i];
|
||||
|
||||
tmp[i] = a[i + col*n] + 439.0*K1[i]/216.0 - 8.0*K2[i] +
|
||||
3680.0*K3[i]/513.0 - 845.0*K4[i]/4104.0;
|
||||
}
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t + h, tmp, dy, data);
|
||||
K5[i] = h * dy[i];
|
||||
|
||||
tmp[i] = a[i + col*n] - 8.0*K1[i]/27.0 + 2.0*K2[i] -
|
||||
3544.0*K3[i]/2565.0 + 1859.0*K4[i]/4104.0 - 11.0*K5[i]/40.0;
|
||||
}
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
status = f(neq, t + h/2.0, tmp, dy, data);
|
||||
K6[i] = h * dy[i];
|
||||
}
|
||||
|
||||
err = 0.0;
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
E[i] = fabs(K1[i]/360.0 - 128.0*K3[i]/4275.0 - 2197.0*K4[i]/75240.0 +
|
||||
K5[i]/50.0 + 2.0*K6[i]/55.0); /* /h ?? */
|
||||
/*printf("E[%d] = %f\n", i, E[i]);*/
|
||||
err = ((E[i] > err) ? E[i] : err);
|
||||
}
|
||||
|
||||
/*printf("err = %e\n", err);*/
|
||||
|
||||
if ((err < epsil) || (h <= step/1000) )
|
||||
{
|
||||
t += h;
|
||||
|
||||
for (i = 0; i < neq; i++)
|
||||
{
|
||||
a[i + col*(n+1)] = a[i + col*n] + 25.0*K1[i]/216.0 +
|
||||
1408.0*K3[i]/2565.0 + 2197.0*K4[i]/4104.0 - 0.2*K5[i];
|
||||
}
|
||||
/* store the time */
|
||||
a[neq + col*(n+1)] = t;
|
||||
n++;
|
||||
}
|
||||
|
||||
beta = pow(epsil/(2*err), 0.25);
|
||||
|
||||
if (beta < 0.1)
|
||||
{
|
||||
h = 0.1* h;
|
||||
}
|
||||
else if (beta > 4)
|
||||
{
|
||||
h = 4.0*h;
|
||||
}
|
||||
else
|
||||
{
|
||||
h = beta * h;
|
||||
}
|
||||
|
||||
/* we have to prevent too little h*/
|
||||
if (h < step/1000)
|
||||
h = step/1000;
|
||||
|
||||
/* prevent too big */
|
||||
if (h > duration/16)
|
||||
h = duration/16;
|
||||
|
||||
if (t + h > duration)
|
||||
{
|
||||
h = duration - t;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return n+1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
/* Resolution of non-linear equation of the for
|
||||
f(x) = 0 with the secante method which is a modified
|
||||
newton method using secante instead of derivative */
|
||||
int NUM_sec(double (*f)(double x), double x0, double x1, int nmax,
|
||||
double epsilon, double *ans)
|
||||
{
|
||||
int i = 0;
|
||||
double x2;
|
||||
double delta;
|
||||
|
||||
do
|
||||
{
|
||||
if (i >= nmax)
|
||||
{
|
||||
return NO_CONVERGENCE;
|
||||
}
|
||||
|
||||
x2 = x1 - f(x1)*(x1 - x0)/(f(x1) - f(x0));
|
||||
|
||||
delta = fabs(x2 - x1)/fabs(x2);
|
||||
x0 = x1;
|
||||
x1 = x2;
|
||||
i++;
|
||||
} while (delta > epsilon);
|
||||
|
||||
*ans = x2;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
int simpson(double *data, int n_point, int col, int off, double *integral)
|
||||
{
|
||||
|
||||
int i;
|
||||
|
||||
double val = 0;
|
||||
|
||||
double x0, x1, x2, fx0, fx1, fx2;
|
||||
|
||||
double dd_x0x1;
|
||||
double dd_x1x2;
|
||||
double dd_x0x1x2;
|
||||
|
||||
int beg = 0;
|
||||
if ((n_point%2) != 1)
|
||||
{
|
||||
beg = 1;
|
||||
x0 = data[0 + 0*col];
|
||||
x1 = data[0 + 1*col];
|
||||
fx0 = data[off + 0*col];
|
||||
fx1 = data[off + 1*col];
|
||||
val += (fx0+fx1)*(x1-x0)/2;
|
||||
}
|
||||
for (i = beg; i < n_point - 2; i += 2)
|
||||
{
|
||||
x0 = data[0 + (i+0)*col];
|
||||
x1 = data[0 + (i+1)*col];
|
||||
x2 = data[0 + (i+2)*col];
|
||||
|
||||
fx0 = data[off + (i+0)*col];
|
||||
fx1 = data[off + (i+1)*col];
|
||||
fx2 = data[off + (i+2)*col];
|
||||
|
||||
dd_x0x1 = (fx1 - fx0)/(x1 - x0);
|
||||
dd_x1x2 = (fx2 - fx1)/(x2 - x1);
|
||||
|
||||
dd_x0x1x2 = (dd_x1x2 - dd_x0x1)/(x2 - x0);
|
||||
|
||||
val += (fx0 - dd_x0x1*x0 + dd_x0x1x2*x0*x1)*(x2 - x0) +
|
||||
(dd_x0x1 - dd_x0x1x2*(x0+x1))*(pow(x2, 2)-pow(x0, 2))/2 +
|
||||
dd_x0x1x2*(pow(x2, 3) - pow(x0,3))/3;
|
||||
}
|
||||
|
||||
*integral = val;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/* spline.c - Cubic spline interpolation
|
||||
* $Id: spline.c,v 1.1.1.1 2001/07/20 03:19:09 antoine Exp $
|
||||
* Copyright (C) 2000
|
||||
* Antoine Lefebvre <antoine.lefebvre@polymtl.ca>
|
||||
*
|
||||
*
|
||||
* Licensed under the GPLv2
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
#define OUT_OF_RANGE -1
|
||||
|
||||
/* Computation of natural spline.
|
||||
|
||||
*/
|
||||
|
||||
int create_spline(double *data, int n_point, double *spline)
|
||||
{
|
||||
int i;
|
||||
int col;
|
||||
double hi0, hi1, hi2;
|
||||
double fi0, fi1, fi2;
|
||||
double *matrix;
|
||||
|
||||
col = 2;
|
||||
|
||||
matrix = (double *) calloc (n_point*(n_point+1), sizeof(double));
|
||||
|
||||
/* create the linear system */
|
||||
matrix[0 + 0] = 1;
|
||||
|
||||
for (i = 1; i < n_point - 1; i++)
|
||||
{
|
||||
hi0 = data[0 + (i+0)*col] - data[0 + (i-1)*col];
|
||||
hi1 = data[0 + (i+1)*col] - data[0 + (i-0)*col];
|
||||
hi2 = data[0 + (i+1)*col] - data[0 + (i-1)*col];
|
||||
|
||||
fi0 = data[1 + (i-1)*col]; /* f(x_(i-1)) */
|
||||
fi1 = data[1 + (i-0)*col]; /* f(x_i) */
|
||||
fi2 = data[1 + (i+1)*col]; /* f(x_(i+1)) */
|
||||
|
||||
matrix[i + (i-1)*n_point] = hi0/(hi0+hi1);
|
||||
matrix[i + (i-0)*n_point] = 2;
|
||||
matrix[i + (i+1)*n_point] = hi1/(hi0+hi1);
|
||||
|
||||
matrix[i + n_point*n_point] = 6*( (fi2-fi1)/hi1 - (fi1-fi0)/hi0)/hi2;
|
||||
}
|
||||
|
||||
matrix[n_point - 1 + (n_point - 1)*n_point] = 1;
|
||||
|
||||
NUM_print_matrix(matrix, n_point);
|
||||
NUM_lu(matrix, spline, n_point);
|
||||
NUM_print_vec(spline, n_point);
|
||||
|
||||
free(matrix);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int eval_spline(double *data, double *spline, int n_point, double x, double *y)
|
||||
{
|
||||
|
||||
int i;
|
||||
|
||||
int col = 2;
|
||||
|
||||
double hi0;
|
||||
double xi0;
|
||||
double xi1;
|
||||
double fi0;
|
||||
double fi1;
|
||||
double d2f0;
|
||||
double d2f1;
|
||||
|
||||
/* we must find the interval in which x is located */
|
||||
|
||||
i = 0;
|
||||
|
||||
if ((x < data[0]) || (x > data[0 + (n_point - 1)*col]))
|
||||
{
|
||||
printf("Out of range: %f\n", x);
|
||||
return OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
for (i = 0; x > data[0 + i*col]; i++);
|
||||
|
||||
xi0 = data[0 + (i-1)*col];
|
||||
xi1 = data[0 + i*col];
|
||||
hi0 = xi1 - xi0;
|
||||
fi0 = data[1 + (i-1)*col];
|
||||
fi1 = data[1 + i*col];
|
||||
d2f0 = spline[i-1];
|
||||
d2f1 = spline[i];
|
||||
|
||||
/* evaluation of the spline at the specified point */
|
||||
*y = -d2f0*pow(x-xi1, 3)/(6*hi0) + d2f1*pow(x-xi0, 3)/(6*hi0)
|
||||
-(fi0/hi0 - hi0*d2f0/6)*(x-xi1) + (fi1/hi0 - hi0*d2f1/6)*(x-xi0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/* sysnewton.c - Solve system of non-linear equations with newton's method
|
||||
* $Id: sysnewton.c,v 1.1.1.1 2001/07/20 03:19:09 antoine Exp $
|
||||
* Copyright (C) 2000
|
||||
* Antoine Lefebvre <antoine.lefebvre@polymtl.ca>
|
||||
*
|
||||
* Licensed under the GPL
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "libnum/include/num.h"
|
||||
|
||||
|
||||
double norme(double *x, int n);
|
||||
|
||||
/* Jac : Pointer to a matrix of pointer to function (Jacobian matrix)
|
||||
* R : Pointer to a vector of pointer to function (Residue vector)
|
||||
* x : Initial estimate of the solution (will be overwrite by the answer)
|
||||
* nvar: Number of variable in the system
|
||||
* nmax: Maximal number of iterations
|
||||
* eps : Precision on the answer
|
||||
*/
|
||||
int NUM_sysnewton(func_t *Jac, func_t *R, double *x, int nvar,
|
||||
int nmax, double eps)
|
||||
{
|
||||
int i, j, l;
|
||||
|
||||
double *r;
|
||||
double *dx; /* solution of the system */
|
||||
double *matrix; /* the matrix to be solve */
|
||||
|
||||
/* Allow space for the left hand side */
|
||||
matrix = (double *) malloc (sizeof(double) * nvar * (nvar + 1));
|
||||
|
||||
r = (double *) malloc (sizeof(double) * nvar);
|
||||
dx = (double *) malloc(sizeof(double) * nvar);
|
||||
|
||||
l = 0;
|
||||
do
|
||||
{
|
||||
/* set up the matrix */
|
||||
for (i = 0; i < nvar; i++) /* line */
|
||||
{
|
||||
for (j = 0; j < nvar; j++) /* column */
|
||||
{
|
||||
matrix[i + nvar*j] = Jac[i + nvar*j](x);
|
||||
}
|
||||
r[i] = matrix[i + nvar*nvar] = -R[i](x);
|
||||
}
|
||||
|
||||
/* if the matrix is singular */
|
||||
if (NUM_lu(matrix, dx, nvar))
|
||||
return NO_CONVERGENCE;
|
||||
|
||||
for (i = 0; i < nvar; i++)
|
||||
{
|
||||
x[i] = x[i] + dx[i];
|
||||
}
|
||||
|
||||
if ((norme(dx, nvar)/norme(x, nvar) < eps) &&
|
||||
(norme(r, nvar) <= eps))
|
||||
{
|
||||
/* the solution converged */
|
||||
return 0;
|
||||
}
|
||||
|
||||
l++;
|
||||
} while (l < nmax);
|
||||
|
||||
return NO_CONVERGENCE;
|
||||
|
||||
}
|
||||
|
||||
|
||||
double norme(double *x, int n)
|
||||
{
|
||||
int i;
|
||||
double a = 0.0;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
a += pow(x[i], 2);
|
||||
}
|
||||
return sqrt(a);
|
||||
}
|
||||
@@ -0,0 +1,344 @@
|
||||
/* test.c - Testing the functionnality of the various method
|
||||
* $Id: test.c,v 1.1.1.1 2001/07/20 03:19:09 antoine Exp $
|
||||
* Copyright (C) 2000
|
||||
* Antoine Lefebvre <antoine.lefebvre@polymtl.ca>
|
||||
*
|
||||
* Licensed under the GPL
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
//#include <time.h>
|
||||
|
||||
#include "num.h"
|
||||
|
||||
FILE * errorfile;
|
||||
FILE * outputfile;
|
||||
|
||||
int test_rk4(void);
|
||||
int test_lu(void);
|
||||
int test_sysnewton(void);
|
||||
int test_sec(void);
|
||||
int test_newton(void);
|
||||
int test_ptfix(void);
|
||||
int test_spline(void);
|
||||
|
||||
/* g1(x) = x + 1 - ln(x) */
|
||||
double g1(double x) {
|
||||
return x + 1 - log(x);
|
||||
}
|
||||
/* f1(x) = ln(x) - 1 */
|
||||
double f1(double x) {
|
||||
return log(x) - 1;
|
||||
}
|
||||
/* f1'(x) = 1/x */
|
||||
double df1(double x) {
|
||||
return 1/x;
|
||||
}
|
||||
double dg1(double x) {
|
||||
return 1 - 1/x;
|
||||
}
|
||||
/* f(x) = (x-1)^3 */
|
||||
double f(double x) {
|
||||
return pow(x-1, 3);
|
||||
}
|
||||
|
||||
int function(int neq, double time, double *y, double *dy,
|
||||
void *data)
|
||||
{
|
||||
|
||||
dy[0] = y[1];
|
||||
dy[1] = -9.8;
|
||||
dy[2] = y[3];
|
||||
dy[3] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* functions to test the sysnewton algorythm
|
||||
*
|
||||
* The system to be solve is the following
|
||||
* r1(x1, x2) = e^x1 - x2 = 0
|
||||
* r2(x1, x2) = x1^2 + x2^2 - 16 = 0
|
||||
*
|
||||
*/
|
||||
double r1(double *x)
|
||||
{
|
||||
return exp(x[0]) - x[1];
|
||||
}
|
||||
double r2(double *x)
|
||||
{
|
||||
return pow(x[0], 2) + pow(x[1], 2) - 16;
|
||||
}
|
||||
/* We need partial derivative of these function with each variable
|
||||
* dr1_dx1(x1, x2) = e^x1
|
||||
* dr1_dx2(x1, x2) = -1
|
||||
* dr2_dx1(x1, x2) = 2*x1
|
||||
* dr2_dx2(x1, x2) = 2*x2
|
||||
*/
|
||||
double dr1_dx1(double *x)
|
||||
{
|
||||
return exp(x[0]);
|
||||
}
|
||||
double dr1_dx2(double *x)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
double dr2_dx1(double *x)
|
||||
{
|
||||
return 2*x[0];
|
||||
}
|
||||
double dr2_dx2(double *x)
|
||||
{
|
||||
return 2*x[1];
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
|
||||
test_lu();
|
||||
test_spline();
|
||||
|
||||
test_rk4();
|
||||
test_sysnewton();
|
||||
|
||||
test_sec();
|
||||
test_newton();
|
||||
test_ptfix();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_sec(void)
|
||||
{
|
||||
double ans;
|
||||
printf("Testing Secante method\n");
|
||||
if (NUM_sec(f1, 1, 2, 100, 0.0001, &ans))
|
||||
printf("No solution: error in the method.\n\n");
|
||||
else
|
||||
printf("Solution: %f\n\n", ans);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_newton(void)
|
||||
{
|
||||
double ans;
|
||||
printf("Testing Newton method\n");
|
||||
if (NUM_newton(f1, df1, 1, 100, 0.0001, &ans))
|
||||
printf("No solution: error in the method.\n\n");
|
||||
else
|
||||
printf("Solution: %f\n\n", ans);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_ptfix(void)
|
||||
{
|
||||
double ans;
|
||||
printf("Testing fixed point method\n");
|
||||
if (NUM_ptfix(g1, 1, 100, 0.0001, &ans))
|
||||
printf("No solution: error in the method.\n");
|
||||
else
|
||||
printf("Solution: %f\n\n", ans);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_sysnewton(void)
|
||||
{
|
||||
func_t *jac;
|
||||
func_t *r;
|
||||
int nvar = 2;
|
||||
|
||||
double x[2]; /* solution vector, contain initially the initial conditions */
|
||||
|
||||
printf("Testing newton method for solving"
|
||||
" non-linear system of equations.\n");
|
||||
|
||||
jac = (func_t *) malloc (sizeof(func_t) * nvar * nvar);
|
||||
r = (func_t *) malloc (sizeof(func_t) * nvar);
|
||||
|
||||
/* Initialize the functions pointers array */
|
||||
r[0] = r1;
|
||||
r[1] = r2;
|
||||
|
||||
jac[0] = dr1_dx1;
|
||||
jac[1] = dr2_dx1;
|
||||
jac[2] = dr1_dx2;
|
||||
jac[3] = dr2_dx2;
|
||||
|
||||
/* set the initial estimate */
|
||||
x[0] = 2.8;
|
||||
x[1] = 2.8;
|
||||
|
||||
/* call the sysnewton function */
|
||||
|
||||
NUM_sysnewton(jac, r, x, 2, 100, 1e-8);
|
||||
|
||||
/* print the solution */
|
||||
|
||||
printf("Solution: x1 = %f, x2 = %f\n", x[0], x[1]);
|
||||
|
||||
printf("\n");
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int test_lu(void)
|
||||
{
|
||||
int i;
|
||||
double *matrix;
|
||||
double *solution;
|
||||
int size = 8;
|
||||
|
||||
printf("Testing the LU factorisation algotythm.\n");
|
||||
matrix = (double *) malloc (sizeof(double)*size*(size+1));
|
||||
solution = (double *) malloc (sizeof(double)*size);
|
||||
|
||||
matrix[0] = 4.77088e-02; matrix[8] = 1.17204e-01; matrix[16] = 1.88670e-02;
|
||||
matrix[1] = 1.17204e-01; matrix[9] = 4.07815e-01; matrix[17] = 1.25752e-02;
|
||||
matrix[2] = 1.88670e-02; matrix[10] = 1.25752e-02; matrix[18] = 4.40215e-02;
|
||||
matrix[3] = 0.00000e+00; matrix[11] = 0.00000e+00; matrix[19] = 0.00000e+00;
|
||||
matrix[4] = 0.00000e+00; matrix[12] = 0.00000e+00; matrix[20] = 0.00000e+00;
|
||||
matrix[5] = 1.00000e+00; matrix[13] = 0.00000e+00; matrix[21] = 3.00000e+00;
|
||||
matrix[6] = 2.97603e-02; matrix[14] = 8.67031e-02; matrix[22] = 2.51546e-02;
|
||||
matrix[7] = 5.15962e+01; matrix[15] = 1.67940e+02; matrix[23] = -7.46975e+01;
|
||||
|
||||
matrix[24] = 0.00000e+00;
|
||||
matrix[25] = 0.00000e+00;
|
||||
matrix[26] = 0.00000e+00;
|
||||
matrix[27] = 1.28681e-02;
|
||||
matrix[28] = 0.00000e+00;
|
||||
matrix[29] = 0.00000e+00;
|
||||
matrix[30] = 6.43406e-03;
|
||||
matrix[31] = -7.23674e-01;
|
||||
|
||||
matrix[32] = 0.0000e+00; matrix[40] = 1.00000e+00; matrix[48] = 2.97603e-02;
|
||||
matrix[33] = 0.0000e+00; matrix[41] = 0.00000e+00; matrix[49] = 8.67031e-02;
|
||||
matrix[34] = 0.0000e+00; matrix[42] = 3.00000e+00; matrix[50] = 2.51546e-02;
|
||||
matrix[35] = 0.0000e+00; matrix[43] = 0.00000e+00; matrix[51] = 6.43406e-03;
|
||||
matrix[36] = 0.0000e+00; matrix[44] = 2.00000e+00; matrix[52] = 0.00000e+00;
|
||||
matrix[37] = 2.0000e+00; matrix[45] = 0.00000e+00; matrix[53] = 0.00000e+00;
|
||||
matrix[38] = 0.0000e+00; matrix[46] = 0.00000e+00; matrix[54] = 1.45616e-02;
|
||||
matrix[39] = 0.0000e+00; matrix[47] =-9.68727e+03; matrix[55] =-3.06747e+01;
|
||||
|
||||
matrix[56] = 5.15962e+01; matrix[64] =-1.12677e+01;
|
||||
matrix[57] = 1.67940e+02; matrix[65] = 8.29437e+00;
|
||||
matrix[58] =-7.46975e+01; matrix[66] =-7.39145e+01;
|
||||
matrix[59] =-7.23674e-01; matrix[67] =-5.99249e-01;
|
||||
matrix[60] = 0.00000e+00; matrix[68] = 1.58804e-02;
|
||||
matrix[61] =-9.68727e+03; matrix[69] =-9.62706e+03;
|
||||
matrix[62] =-3.06747e+01; matrix[70] =-4.63904e+01;
|
||||
matrix[63] = 4.68590e+05; matrix[71] = 2.37298e+05;
|
||||
|
||||
//NUM_matscale(matrix, size);
|
||||
NUM_print_matrix(matrix, size);
|
||||
|
||||
if (NUM_lu(matrix, solution, size))
|
||||
printf("No solution: Error in the numerical method,\n");
|
||||
else
|
||||
NUM_print_vec(solution, size);
|
||||
/*
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
if (solution[i] != 1.0)
|
||||
{
|
||||
printf("Error found in the solution.\n");
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int test_rk4(void)
|
||||
{
|
||||
int i, n;
|
||||
double *ans;
|
||||
double *ic;
|
||||
|
||||
printf("\nTesting the RK4 and RKF algorythm.\n");
|
||||
|
||||
ic = (double *) malloc(sizeof(double) * 4);
|
||||
|
||||
ic[0] = 0;
|
||||
ic[1] = 100;
|
||||
ic[2] = 0;
|
||||
ic[3] = 10;
|
||||
|
||||
/* it return the length of the answer vector */
|
||||
//n = NUM_rk4 (function, 4, 0.1, 10, ic, &ans, NULL);
|
||||
|
||||
//for (i = 0; i < n; i++)
|
||||
//{
|
||||
// printf("%f %f %f %f \n", ans[4*i], ans[1 + 4*i], ans[2+4*i], ans[3+4*i]);
|
||||
//}
|
||||
|
||||
n = NUM_rkf (function, 4, 0.1, 20, ic, &ans, 1e-4, NULL);
|
||||
|
||||
printf("n = %i\n", n);
|
||||
|
||||
for( i = 0; i < n; i++)
|
||||
{
|
||||
printf("%f %f %f %f %f\n", ans[4 + 5*i], ans[5*i],
|
||||
ans[1+5*i], ans[2 + 5*i], ans[3 + 5*i]);
|
||||
}
|
||||
|
||||
free(ic);
|
||||
free(ans);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_spline(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
double *data;
|
||||
|
||||
double *spline;
|
||||
|
||||
int size = 10;
|
||||
|
||||
double ans;
|
||||
|
||||
printf("Testing the spline function.\n\n");
|
||||
data = malloc (2 * size * sizeof(double));
|
||||
spline = malloc (size * sizeof(double));
|
||||
|
||||
data[0] = 0; /* x0 */
|
||||
data[1] = 55; /* y0 */
|
||||
data[2] = 5; /* x1 */
|
||||
data[3] = 60; /* y1 */
|
||||
data[4] = 10; /* ... */
|
||||
data[5] = 58;
|
||||
data[6] = 15;
|
||||
data[7] = 54;
|
||||
data[8] = 20;
|
||||
data[9] = 55;
|
||||
data[10] = 25;
|
||||
data[11] = 60;
|
||||
data[12] = 30;
|
||||
data[13] = 54;
|
||||
data[14] = 35;
|
||||
data[15] = 57;
|
||||
data[16] = 40;
|
||||
data[17] = 52;
|
||||
data[18] = 45;
|
||||
data[19] = 49;
|
||||
|
||||
create_spline(data, size, spline);
|
||||
|
||||
for (i = 0; i < 45; i++)
|
||||
{
|
||||
eval_spline(data, spline, size, (double)i, &ans);
|
||||
printf("%d %f\n", i, ans);
|
||||
}
|
||||
|
||||
printf("Spline test finish\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
int trapeze(double *data, int n_point, int col, int off, double *integral)
|
||||
{
|
||||
int i;
|
||||
double val = 0.0;
|
||||
|
||||
for (i = 0; i < n_point - 1; i++)
|
||||
{
|
||||
val += (data[off + i*col] + data[off + (i + 1)*col])*
|
||||
(data[0 + (i + 1)*col] - data[0 + i*col])/2;
|
||||
}
|
||||
|
||||
*integral = val;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user