Kennesaw State University CS 4491 Assigment 6
Kennesaw
State University
Department
of Computer Science
Advanced
Topic in Computer Science CS 4491/2
Assignment
# 6 / Water / Patients
Dalibor
Labudovic
04/23/2013
Initial
Problem Statement:
CS4491
Assignment No. 5 (Due March 12, 2013)
1.
In the original water treatment problem Case Study 2, compute the
levels
of impurities after 8 repetitions of the application of solvents.
Use
the growth factor from the case study and develop a C program
for
these computations.
2.
Develop a C program and use the data in Table 1 and apply curve
fitting
to derive a polynomial function of degree 5. Use this function
to
compute estimates of intermediate values of the number of patients.
Produce
a plot of the given and estimated data.
Table
1. Number of patients for years 1995-2002.
Year
|
1995
|
1996
|
1997
|
1998
|
1999
|
2000
|
2001
|
2002
|
Patients
|
5,500
|
8,500
|
13,500
|
20,500
|
29,500
|
40,500
|
53,500
|
68,500
|
Increase
|
0
|
3,000
|
5,000
|
7,000
|
9,000
|
11,000
|
13,000
|
15,000
|
Summary
and purpose of the assignment activity:
The outcome of the
first problem is the manipulation of the case study to calculate
water purity at a different chemical treatment application. The
purpose of this first problem is that calculating this type of water
treatment is more efficient using a well structured formula then
manipulating data to get the desired results.
The outcome of the
second problem is the using given data to to set a plot then using
interpolation to get a more fitted data to plot a graph. The purpose
of this problem is the use of finite differences and intermediate
points to form a graph.
Detail
description of the solution and used in the project:
1.) The solution to
the first problem is to incorporate basic_lib library to allocate the
differences using a function from the sources code below. The
program will define number of applications, constant factor and
initial impurity of water. With the calculation of the acceptable
impurity of water, the number of application can be adjusted.
2.) The solution to
the second problem is to incorporate finite differences to form a
foundation of data plots then utilizing the intermediate points to
populate additional estimated points for a more accurate picture. The
source code makes use of a library polyfitgsl and basic_lib.
1)
SOURCE
CODE
#include <stdio.h>
#include <math.h>
#include
"basic_lib.h"
int main(){
const int N = 8;
//number of applications
double s[N];
double x[N];
double f = 0.35;
//constant factor
int n;
int j;
double xf;
s[0] = 405.0;//
initial impurity of water
xf = N;
n = arraycol(x,
1.0, xf, 1.0);
printf("Initial
impurity in water: %f\n", s[0]);
printf("Number
of applicatons: %d\n",N);
printf("\n\nFactor:
%f \n", f);
for (j = 0;j <
N-1;j++)
s[j+1] = f * s[j];
printf("Impurities
after each applications \n");
for(j=0;j<N;j++)
printf("%f %f
\n", x[j], s[j]);
return 0;
}
2.) SOURCE
CODE
#include <stdio.h>
#include
<gsl/gsl_poly.h>
#include
"basic_lib.h"
#include
"polyfitgsl.h"
#define NC 5 /*
size of coefficient vector */
void polyval(double
p[], double xv[], double yv[], int n, int m);
int main()
{
const int NP = 8;
// Number of given data points
const int M = 30;
// number of calcualted data points
/* const int NC =
3; Number of coefficients */
double x[] = {0.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
double y[] = {0.0,
3000.0,5000.0,7000.0,9000.0,11000.0,13000.0,15000.0};
double coeff[NC];
// vector of coefficients
int i;
double xc[M]; /*
computed values for x using polynomial */
double yc[M];
printf("Program
finds best fit polynomial of degree %d \n", NC-1);
printf("Data
points (x,y): \n");
for (i = 0; i <
NP; i++)
printf("
%f %f \n", x[i], y[i]);
polynomialfit(NP,
NC, x, y, coeff);
printf("\n\nCoefficients
of polynomial found\n");
for(i=0; i <
NC; i++) {
printf("%lf\n",
coeff[i]);
}
/* Evaluate the
fitted polynomial */
linspace(xc, 0.0,
7.0, M);
polyval(coeff, xc,
yc, NC, M);
printf("\nData
points calculated with the polynomial \n");
for(i=0; i < M;
i++)
printf("%d
%+.18f %+.18f \n", i, xc[i], yc[i]);
return 0;
}
void polyval (double
p[], double xv[], double yv[], int n, int m) {
int j;
yv[0] =
gsl_poly_eval(p, n, xv[0]);
for (j = 1; j <
m; j++) {
yv[j] =
gsl_poly_eval(p, n, xv[j]);
}
return;
}
unset key
plot
"interpol_original.gpl" u 1:3 w linespoints
Table
of results:
1.)
Initial
impurity in water: 405.000000
Number
of applicatons: 8
Factor:
0.350000
Impurities
after each applications
1.000000
405.000000
2.000000
141.750000
3.000000
49.612500
4.000000
17.364375
5.000000
6.077531
6.000000
2.127136
7.000000
0.744498
8.000000
0.260574
2.)
Program
finds best fit polynomial of degree 4
Data
points (x,y):
0.000000 0.000000
1.000000 3000.000000
2.000000 5000.000000
3.000000 7000.000000
4.000000 9000.000000
5.000000
11000.000000
6.000000
13000.000000
7.000000
15000.000000
Coefficients
of polynomial found
26.515152
3404.671717
-629.734848
110.479798
-6.628788
Data
points calculated with the polynomial
0
+0.000000000000000000 +26.515151515142889593
1
+0.241379310344827597 +813.172863273697998920
2
+0.482758620689655193 +1535.456370840211548057
3
+0.724137931034482762 +2201.878117492446563119
4
+0.965517241379310387 +2820.410482131026583374
5
+1.206896551724137900 +3398.485779279436883371
6
+1.448275862068965525 +3942.996259084022312891
7
+1.689655172413793149 +4460.294107313990934927
8
+1.931034482758620774 +4956.191445361408113968
9
+2.172413793103448398 +5435.960330241205156199
10
+2.413793103448275801 +5904.332754591169759806
11
+2.655172413793103203 +6365.500646671953290934
12
+2.896551724137930606 +6823.115870367066236213
13
+3.137931034482758008 +7280.290225182881840738
14
+3.379310344827585411 +7739.595446248635198572
15
+3.620689655172412813 +8203.063204316418705275
16
+3.862068965517240215 +8672.185105761187514872
17
+4.103448275862067618 +9147.912692580759539851
18
+4.344827586206895020 +9630.657442395811813185
19
+4.586206896551722423 +10120.290768449882307323
20
+4.827586206896549825 +10616.144019609369934187
21
+5.068965517241377228 +11117.008480363538183155
22
+5.310344827586204630 +11621.135370824502388132
23
+5.551724137931032033 +12126.235846727246098453
24
+5.793103448275859435 +12629.480999429617440910
25
+6.034482758620686838 +13127.501855912318205810
26
+6.275862068965514240 +13616.389378778912941925
27
+6.517241379310341642 +14091.694466255828956491
28
+6.758620689655169045 +14548.427952192345401272
29
+6.999999999999996447 +14981.060606060626014369
Comments
and Conclusion:
In conclusion this
assignment has thought me a real world application of mathematical
model in a computational environment. The case study is
scientifically applicable and I see how the use of computing can
contribute to the wellness of people. The data analysis in the second
people has showed me a different way of manipulating the given data
to give me a visualization of the raw data. Even without the
polynomial function and only with given points the assignment has
caught me to how to form a function.
Script:
Script
started on Thu 18 Apr 2013 05:07:41 PM EDT
]0;dlabudovic@ubuntu:
~/Documents/CS4491/assign_06/P1dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P1$
nano water.c
File: water.c
#include
<stdio.h>
#include
<math.h>
#include
"basic_lib.h"
int
main(){
const
int N = 8; //number of applications
double
s[N];
double
x[N];
double
f = 0.35; //constant factor
int
n;
int
j;
double
xf;
s[0]
= 405.0;// initial impurity of water
xf
= N;
n
= arraycol(x, 1.0, xf, 1.0);
printf("Initial
impurity in water: %f\n", s[0]);
printf("Number
of applicatons: %d\n",N);
printf("\n\nFactor:
%f \n", f);
for
(j = 0;j < N-1;j++)
s[j+1]
= f * s[j];
printf("Impurities
after each applications \n");
for(j=0;j<N;j++)
printf("%f
%f \n", x[j], s[j]);
return
0;
}
dlabudovic@ubuntu:
~/Documents/CS4491/assign_06/P1dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P1$
[K]0;dlabudovic@ubuntu:
~/Documents/CS4491/assign_06/P1dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P1$
[K]0;dlabudovic@ubuntu:
~/Documents/CS4491/assign_06/P1dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P1$
nano water.cls[Ktypescript > script.txtscript[K[4Plscd
Documents/CS4491/assign_06/P2exit[Kgnuplot./patients.out >
patients.txt[Kgcc -Wall patients.c -o patients.out basic_lib.h
polyfitgsl.c -lgsl -lgslcblas -lm Cnano patients.c[Kgcc -Wall
patients.c -o patients.out basic_lib.h polyfitgsl.c -lgsl -lgslcblas
-lm Cnano patients.c[K[8Pgnuplot./patients.out >
patients.txt[14Pnano patients.c[9Pscript./patients.outls[Kgcc -Wall
patients.c -o patients.out basic_lib.h polyfitgsl.c -lgsl -lgslcblas
-lm nano polyfitgsl.c[K[5Ppolyfitgsl.hnano patients.cls[Kgcc -Wall
patients.c -o partients.out basic_lib.h polyfitgsl.h -lgsl -lgslcblas
-lm[1Ptients.out basic_lib.o polyfitgsl.o interpol.c -o interpol.out
basic_lib patients.c -o patients.out -basic_lib.h -polyfitgsl.h
nano
basic_lib.h[K[1Ppatients.cls[Kcd
Documents/CS4491/assign_06/P2exit[K./patients.outgcc -Wall patients.c
-o patients.out basic_lib.h polyfitgsl.c -lgsl -lgslcblas -lm
nano
patients.c[K[3Pwater.cgcc -Wall water.c -o water.out basic_lib.h -lm
water.c:
In function :
water.c:11:6:
warning: variable set but not used [-Wunused-but-set-variable]
]0;dlabudovic@ubuntu:
~/Documents/CS4491/assign_06/P1dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P1$
./water.out
Initial
impurity in water: 405.000000
Number
of applicatons: 8
Factor:
0.350000
Impurities
after each applications
1.000000
405.000000
2.000000
141.750000
3.000000
49.612500
4.000000
17.364375
5.000000
6.077531
6.000000
2.127136
7.000000
0.744498
8.000000
0.260574
]0;dlabudovic@ubuntu:
~/Documents/CS4491/assign_06/P1dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P1$
exit
exit
Script done on Thu 18 Apr 2013 05:08:38 PM EDT Script started on Thu 18 Apr 2013 04:43:40 PM EDT ?]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_06/P2?dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P2$ nano patients.c #include <stdio.h> #include <gsl/gsl_poly.h> #include "basic_lib.h" #include "polyfitgsl.h" #define NC 5 /* size of coefficient vector */ void polyval(double p[], double xv[], double yv[], int n, int m); int main() { const int NP = 8; // Number of given data points const int M = 30; // number of calcualted data points /* const int NC = 3; Number of coefficients */ double x[] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}; double y[] = {0.0, 3000.0,5000.0,7000.0,9000.0,11000.0,13000.0,15000.0}; double coeff[NC]; // vector of coefficients int i; double xc[M]; /* computed values for x using polynomial */ double yc[M]; printf("Program finds best fit polynomial of degree %d \n", NC-1); printf("Data points (x,y): \n"); for (i = 0; i < NP; i++) printf(" %f %f \n", x[i], y[i]); polynomialfit(NP, NC, x, y, coeff); printf("\n\nCoefficients of polynomial found\n"); for(i=0; i < NC; i++) { printf("%lf\n", coeff[i]); } /* Evaluate the fitted polynomial */ linspace(xc, 0.0, 7.0, M); polyval(coeff, xc, yc, NC, M); printf("\nData points calculated with the polynomial \n"); for(i=0; i < M; i++) printf("%d %+.18f %+.18f \n", i, xc[i], yc[i]); return 0; } void polyval (double p[], double xv[], double yv[], int n, int m) { int j; yv[0] = gsl_poly_eval(p, n, xv[0]); for (j = 1; j < m; j++) { yv[j] = gsl_poly_eval(p, n, xv[j]); } return; } dlabudovic@ubuntu: ~/Documents/CS4491/assign_06/P2?dlabudovic@ubuntu:~/gcc -Wall patients.c -o patients.out basic_lib.h polyfitgsl.c -lgsl -lgslcblas -lm ?]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_06/P2?dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P2$ ./patients.out Program finds best fit polynomial of degree 4 Data points (x,y): 0.000000 0.000000 1.000000 3000.000000 2.000000 5000.000000 3.000000 7000.000000 4.000000 9000.000000 5.000000 11000.000000 6.000000 13000.000000 7.000000 15000.000000 Coefficients of polynomial found 26.515152 3404.671717 -629.734848 110.479798 -6.628788 Data points calculated with the polynomial 0 +0.000000000000000000 +26.515151515142889593 1 +0.241379310344827597 +813.172863273697998920 2 +0.482758620689655193 +1535.456370840211548057 3 +0.724137931034482762 +2201.878117492446563119 4 +0.965517241379310387 +2820.410482131026583374 5 +1.206896551724137900 +3398.485779279436883371 6 +1.448275862068965525 +3942.996259084022312891 7 +1.689655172413793149 +4460.294107313990934927 8 +1.931034482758620774 +4956.191445361408113968 9 +2.172413793103448398 +5435.960330241205156199 10 +2.413793103448275801 +5904.332754591169759806 11 +2.655172413793103203 +6365.500646671953290934 12 +2.896551724137930606 +6823.115870367066236213 13 +3.137931034482758008 +7280.290225182881840738 14 +3.379310344827585411 +7739.595446248635198572 15 +3.620689655172412813 +8203.063204316418705275 16 +3.862068965517240215 +8672.185105761187514872 17 +4.103448275862067618 +9147.912692580759539851 18 +4.344827586206895020 +9630.657442395811813185 19 +4.586206896551722423 +10120.290768449882307323 20 +4.827586206896549825 +10616.144019609369934187 21 +5.068965517241377228 +11117.008480363538183155 22 +5.310344827586204630 +11621.135370824502388132 23 +5.551724137931032033 +12126.235846727246098453 24 +5.793103448275859435 +12629.480999429617440910 25 +6.034482758620686838 +13127.501855912318205810 26 +6.275862068965514240 +13616.389378778912941925 27 +6.517241379310341642 +14091.694466255828956491 28 +6.758620689655169045 +14548.427952192345401272 29 +6.999999999999996447 +14981.060606060626014369 ?]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_06/P2?dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P2$ ./patients.out > patients.txt ?]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_06/P2?dlabudovic@ubuntu:~/Documents/CS4491/assign_06/P2$ gnuplot G N U P L O T Version 4.6 patchlevel 0 last modified 2012-03-04 Build System: Linux x86_64 Copyright (C) 1986-1993, 1998, 2004, 2007-2012 Thomas Williams, Colin Kelley and many others gnuplot home: http://www.gnuplot.info faq, bugs, etc: type "help FAQ" immediate help: type "help" (plot window: hit 'h') Terminal type set to 'wxt' gnuplot> o??[K?load 'patients.cgp' gnuplot> exit Script done on Thu 18 Apr 2013 04:46:23 PM EDT
Comments
Post a Comment