Kennesaw State University CS 4491 Assigment 5
Kennesaw
State University
Department
of Computer Science
Advanced
Topic in Computer Science CS 4491/2
Assignment
# 5 / Polynomial Function
Dalibor
Labudovic
01/16/2013
Initial
Problem Statement:
CS4491
Assignment No. 5 (Due March 12, 2013)
1.
In a typical spring day, the temperature varies according to the data
recorded in Table 1. Develop a C program to compute first and second
differences. Formulate the difference equations and functional
equations for the temperature. Is this discrete or continuous data?
Discuss. Plot the relevant data with Gnuplot.
Table
1: Temperature changes in 12-hour period.
Time |
7 |
8 |
9 |
10 |
11 |
12 |
1 |
2 |
3 |
4 |
5 |
6 |
Temp.
(F) |
51 |
56 |
60 |
65 |
73 |
78 |
85 |
86 |
84 |
81 |
80 |
70 |
2.
Develop a C program to evaluate the polynomial function y=
2x^6 –
1.5x^5
+ 5x^4
–
6.5x^3
+ 6x^2
–
3x + 4.5. Find an appropriate interval of x for which
the function evaluation is done and plot the relevant data.
Develop
a C program and use the data in Table 2 and apply curve fitting
to derive a polynomial function of degree 2. Use this function to
compute estimates of intermediate values of the number of patients.
Produce a plot of the given and estimated data.
Table
2: 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 summary of this
assignment is the proper use of additional libraries within the C
programming community and the essential functionality of GSL
libraries. The purpose of this assignment is use the given data and
manipulate it into easy to read diagrams. The functionality of the C
programming and the results is process of evolving raw data into
useful information.
Detail
description of the solution and used in the project:
1)
SOURCE
CODE
#include
<stdio.h>
#include
"basic_lib.h"
int
main(){
const
int N = 12; /* 12 data points */
int
x[N];
int
j;
/*
Array with original*/
double
T[] = { 51,56,60,65,73,78,85,86,84,81,80,70};/*temperture*/
double
*d; /* first difference */
double
*d2; /* second difference */
x[0]
= 1;
for
(j=1; j<N; j++)
x[j]
= x[j-1] + 1;
/*compute
first differences*/
d
= diff(T,N);
/*compute
second differences*/
d2
= diff(d, N-1);
/*
data for plotting */
printf("\nData
of differences for plotting");
printf("\nTime
First Diff");
for(j=0;j<N-1;j++)
printf("\n%i
%lf", x[j],d[j]);
printf("\n\n\n");
printf("\nTime
Second Diff");
for(j=0;j<N-1;j++)
printf("\n%i
%lf", x[j],d2[j]);
return
0;
}
CGP
FILE
set
title "First Differences" font "Verdana, 14"
set
xlabel "Time" font "Verdana, 12"
set
ylabel "Values of first differences" font "Verdana,
12"
set
size 1.0, 1.0
#set
terminal postscript portrait enhanced mono dashed lw 1 "Helvetica"
14
set
terminal pngcairo size 800, 600 enhanced
set
output "difference.png"
set
samples 50
unset
key
plot
"difference.gpl" u 1:2 w linespoints
GPL
FILE
#Data
of differences for plotting
#Time
First Diff
1
5.000000
2
4.000000
3
5.000000
4
8.000000
5
5.000000
6
7.000000
7
1.000000
8
-2.000000
9
-3.000000
10
-1.000000
11
-10.000000
#Time
Second Diff
#1
-1.000000
#2
1.000000
#3
3.000000
#4
-3.000000
#5
2.000000
#6
-6.000000
#7
-3.000000
#8
-1.000000
#9
2.000000
#10
-9.000000
#11
0.000000
Second
Difference CGP FILE
set
title "Second Differences" font "Verdana, 14"
set
xlabel "Time" font "Verdana, 12"
set
ylabel "Values of second differences" font "Verdana,
12"
set
size 1.0, 1.0
#set
terminal postscript portrait enhanced mono dashed lw 1 "Helvetica"
14
set
terminal pngcairo size 800, 600 enhanced
set
output "difference2.png"
set
samples 50
unset
key
plot
"difference2.gpl" u 1:2 w linespoints
Second
Difference GPL FILE
#Data of differences
for plotting
#Time First Diff
#1 5.000000
#2 4.000000
#3 5.000000
#4 8.000000
#5 5.000000
#6 7.000000
#7 1.000000
#8 -2.000000
#9 -3.000000
#10 -1.000000
#11
-10.000000
#Time Second
Diff
1 -1.000000
2 1.000000
3 3.000000
4 -3.000000
5 2.000000
6 -6.000000
7 -3.000000
8 -1.000000
9 2.000000
10 -9.000000
#11 0.000000
2.) SOURCE
CODE
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <gsl/gsl_poly.h>
#include "basic_lib.h"
#define N 7 // number of coefficients
#define M 20
void polyval(double p[], double xv[], double yv[], int n, int m);
int main() {
int i;
int j;
/* coefficient of poly */
double a[N] = {4.5,-3,6,-6.5,5,1.5,2}; /* coefficients */
double xi, xf;
double x[M];
double y[M];
/*Evaluate the poly*/
xi = -6.0; /* 1st value of x */
xf = 7.0; /* 2nd value of x */
linspace(x, xi, xf, M); /*vector of x values*/
polyval(a,x,y,N,M);
printf("\n");
printf("Data points calcualted of the poly x y \n");
for(j=0;j<M;j++)
printf("%+.18f %+.18f \n", x[j], y[j]);
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;
}
CGP
CODE
set title "Polynomail Function" font "Verdana, 14"
set xlabel "X" font "Verdana, 12"
set ylabel "Y" font "Verdana, 12"
set size 1.0, 1.0
#set terminal postscript portrait enhanced mono dashed lw 1
"Helvetica" 14
set terminal pngcairo size 800, 600 enhanced
set output "poly.png"
set samples 50
unset key
plot "poly.gpl" u 1:2 w linespoints
GPL
CODE
#Data points calcualted of the poly x y
-6.000000000000000000 +89770.500000000000000000
-5.315789473684210620 +43918.919850804792076815
-4.631578947368421240 +19639.394747788868698990
-3.947368421052631859 +7852.172010329663862649
-3.263157894736842479 +2730.641875884949058673
-2.578947368421053099 +802.106291632630473032
-1.894736842105263719 +196.288694529920917375
-1.210526315789474339 +41.584779791880251310
-0.526315789473684847 +9.054257789326985773
+0.157894736842104644 +4.153599366116664804
+0.842105263157894135 +6.209769575789209739
+1.526315789473683626 +55.634949837585040200
+2.210526315789473006 +388.882248511829686777
+2.894736842105262387 +1721.142399894687741835
+3.578947368421051767 +5676.781451632283278741
+4.263157894736841591 +15367.519440554198808968
+4.947368421052630971 +36118.350056926319666672
+5.631578947368420351 +76341.201297123072436079
+6.315789473684209732 +148556.337104719015769660
+6.999999999999999112 +270561.499999999825377017
3.) SOURCE CODE
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_interp.h>
#include "basic_lib.h"
//number of discrete points
#define N 8
void linterp(double x[], double y[], double nx[], double ny[], int m,
int n);
int main(){
int i;
int n;
double h;
double x[N];
double xi, xf;
double y[N] = {5500, 8500, 13500, 20500, 29500, 40500, 53500,
68500};
double xint[30];
double yint[30];
//data for linear interpolation
xi = 0.0;
xf = 7.0;
n = arraycol(x, xi, xf, 1.0);
printf("Discrete Points for Interpolation\n");
printf(" i x y\n");
for(i=0;i<N;i++)
printf("%3d %25.17e %25.17e\n", i, x[i], y[i]);
printf("\n");
h= 0.25;
n = arraycol(xint, xi, xf, h);
linterp(x, y, xint, yint, N, n);
printf(" x interpolated y
\n");
for(i=0;i<n; i++)
printf("%25.17e %25.17e \n", xint[i], yint[i]);
return 0;
}
void linterp(double x[], double y[], double nx[], double ny[], int m,
int n){
int i;
gsl_interp *workspace;
gsl_interp_accel *accel;
// define type of interpolation lienar
workspace = gsl_interp_alloc(gsl_interp_linear, m);
accel = gsl_interp_accel_alloc();
// initialize workspace
gsl_interp_init(workspace, x, y, m);
for(i = 0;i < n;i++)
ny[i] = gsl_interp_eval(workspace, x, y, nx[i], NULL);
//free memory
gsl_interp_free(workspace);
}
CGP
CODE
set title "Interpolation: Number of Patients" font
"Verdana, 14"
set xlabel "X" font "Verdana, 12"
set ylabel "Y" font "Verdana, 12"
set size 1.0, 1.0
#set terminal postscript portrait enhanced mono dashed lw 1
"Helvetica" 14
set terminal pngcairo size 800, 600 enhanced
set output "Interpol_interpol.png"
set samples 50
unset key
plot "interpol.gpl" u 1:2 w linespoints
GPL
CODE
#Discrete Points for Interpolation
# i x y
# 0 0.00000000000000000e+00 5.50000000000000000e+03
# 1 1.00000000000000000e+00 8.50000000000000000e+03
# 2 2.00000000000000000e+00 1.35000000000000000e+04
# 3 3.00000000000000000e+00 2.05000000000000000e+04
# 4 4.00000000000000000e+00 2.95000000000000000e+04
# 5 5.00000000000000000e+00 4.05000000000000000e+04
# 6 6.00000000000000000e+00 5.35000000000000000e+04
# 7 7.00000000000000000e+00 6.85000000000000000e+04
# x interpolated y
0.00000000000000000e+00 5.50000000000000000e+03
2.50000000000000000e-01 6.25000000000000000e+03
5.00000000000000000e-01 7.00000000000000000e+03
7.50000000000000000e-01 7.75000000000000000e+03
1.00000000000000000e+00 8.50000000000000000e+03
1.25000000000000000e+00 9.75000000000000000e+03
1.50000000000000000e+00 1.10000000000000000e+04
1.75000000000000000e+00 1.22500000000000000e+04
2.00000000000000000e+00 1.35000000000000000e+04
2.25000000000000000e+00 1.52500000000000000e+04
2.50000000000000000e+00 1.70000000000000000e+04
2.75000000000000000e+00 1.87500000000000000e+04
3.00000000000000000e+00 2.05000000000000000e+04
3.25000000000000000e+00 2.27500000000000000e+04
3.50000000000000000e+00 2.50000000000000000e+04
3.75000000000000000e+00 2.72500000000000000e+04
4.00000000000000000e+00 2.95000000000000000e+04
4.25000000000000000e+00 3.22500000000000000e+04
4.50000000000000000e+00 3.50000000000000000e+04
4.75000000000000000e+00 3.77500000000000000e+04
5.00000000000000000e+00 4.05000000000000000e+04
5.25000000000000000e+00 4.37500000000000000e+04
5.50000000000000000e+00 4.70000000000000000e+04
5.75000000000000000e+00 5.02500000000000000e+04
6.00000000000000000e+00 5.35000000000000000e+04
6.25000000000000000e+00 5.72500000000000000e+04
6.50000000000000000e+00 6.10000000000000000e+04
6.75000000000000000e+00 6.47500000000000000e+04
7.00000000000000000e+00 6.85000000000000000e+04
ORIGINAL
DATA
CGP
CODE
#Discrete Points for
Interpolation
# i x
y
0
0.00000000000000000e+00 5.50000000000000000e+03
1
1.00000000000000000e+00 8.50000000000000000e+03
2
2.00000000000000000e+00 1.35000000000000000e+04
3
3.00000000000000000e+00 2.05000000000000000e+04
4
4.00000000000000000e+00 2.95000000000000000e+04
5
5.00000000000000000e+00 4.05000000000000000e+04
6
6.00000000000000000e+00 5.35000000000000000e+04
7
7.00000000000000000e+00 6.85000000000000000e+04
# x
interpolated y
#
0.00000000000000000e+00 5.50000000000000000e+03
#
2.50000000000000000e-01 6.25000000000000000e+03
#
5.00000000000000000e-01 7.00000000000000000e+03
#
7.50000000000000000e-01 7.75000000000000000e+03
#
1.00000000000000000e+00 8.50000000000000000e+03
#
1.25000000000000000e+00 9.75000000000000000e+03
#
1.50000000000000000e+00 1.10000000000000000e+04
#
1.75000000000000000e+00 1.22500000000000000e+04
#
2.00000000000000000e+00 1.35000000000000000e+04
#
2.25000000000000000e+00 1.52500000000000000e+04
#
2.50000000000000000e+00 1.70000000000000000e+04
#
2.75000000000000000e+00 1.87500000000000000e+04
#
3.00000000000000000e+00 2.05000000000000000e+04
#
3.25000000000000000e+00 2.27500000000000000e+04
#
3.50000000000000000e+00 2.50000000000000000e+04
#
3.75000000000000000e+00 2.72500000000000000e+04
#
4.00000000000000000e+00 2.95000000000000000e+04
#
4.25000000000000000e+00 3.22500000000000000e+04
#
4.50000000000000000e+00 3.50000000000000000e+04
#
4.75000000000000000e+00 3.77500000000000000e+04
#
5.00000000000000000e+00 4.05000000000000000e+04
#
5.25000000000000000e+00 4.37500000000000000e+04
#
5.50000000000000000e+00 4.70000000000000000e+04
#
5.75000000000000000e+00 5.02500000000000000e+04
#
6.00000000000000000e+00 5.35000000000000000e+04
#
6.25000000000000000e+00 5.72500000000000000e+04
#
6.50000000000000000e+00 6.10000000000000000e+04
#
6.75000000000000000e+00 6.47500000000000000e+04
#
7.00000000000000000e+00 6.85000000000000000e+04
ORIGINAL DATE
GLP CODE
set title "Original
Data: Number of Patients" font "Verdana, 14"
set xlabel "X"
font "Verdana, 12"
set ylabel "Y"
font "Verdana, 12"
set size 1.0, 1.0
#set terminal
postscript portrait enhanced mono dashed lw 1 "Helvetica"
14
set terminal
pngcairo size 800, 600 enhanced
set output
"interpol_original.png"
set samples 50
unset key
plot
"interpol_original.gpl" u 1:3 w linespoints
Table
of results:
1)
2.)
3.)
Discussion
of how the system modeled can be improved and extended to include
other service:
If the process of data input or loading of data into a seemingly
single program which allows the user to chose the type of function
desired to render the graphical model such as a graph.
Comments
and Conclusion:
The
difficulty of the programming this type of assignment is high, but
the type of experience developed during this assignment is a real
world experience which can be very helpful in the future. The process
of raw data transformation into useful information is a task of a lot
of hard work.
Script:
Script started on Sun 31 Mar 2013 04:36:48 PM EDT #]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P1#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P1$ nano d##[K##[K##[K##[K##[K##[Knano ds##[K##[Kscript##[K##[K##[K##[K##[K##[Kdifference.c #[?1049h#[1;24r#(B#[m#[4l#[?7h#[?12l#[?25h#[?1h#=#[?1h#=#[?1h#=#[39;49m#[39;49m#(B#[m#[H#[2J#(B#[0;7m GNU nano 2.2.6 File: difference.c #[3;1H#(B#[0;1m#[36m#include#[39m#(B#[m #(B#[0;1m#[33m<stdio.h> difference.c:2:23: fatal error: basic_lib.h: No such file or directory compilation terminated. #]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P1#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P1$ gcc -Wall difference.c -o diff #[?1049h#[1;24r#(B#[m#[4l#[?7h#[?12l#[?25h#[?1h#=#[?1h#=#[?1h#=#[39;49m#[39;49m#(B#[m#[H#[2J#(B#[0;7m GNU nano 2.2.6 File: difference.c #[3;1H#(B#[0;1m#[36m#include#[39m#(B#[m #(B#[0;1m#[33m<stdio.h> gcc: error: basic_lib.h: No such file or directory #]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P1#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P1$ ./a##[Kdiffernce.c##[Ko bash: ./differnce.o: No such file or directory #]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P1#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P1$ d##[K./a##[Kdifference.o bash: ./difference.o: cannot execute binary file #]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P1#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P1$ ./difference.o bash: ./difference.o: cannot execute binary file #]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P1#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P1$ ./differnece##[K##[K##[K##[K##[K##[K##[K##[Kfference Data of differences for plotting Time First Diff 1 5.000000 2 4.000000 3 5.000000 4 8.000000 5 5.000000 6 7.000000 7 1.000000 8 -2.000000 9 -3.000000 10 -1.000000 11 -10.000000 Time Second Diff 1 -1.000000 2 1.000000 3 3.000000 4 -3.000000 5 2.000000 6 -6.000000 7 -3.000000 8 -1.000000 9 2.000000 10 -9.000000 11 0.000000#]0;dlabudovic@ubuntu: Script started on Sun 31 Mar 2013 04:45:27 PM EDT #]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P2#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P2$ nano sc##[K##[K##[K poly.c #[?1049h#[1;24r#(B#[m#[4l#[?7h#[?12l#[?25h#[?1h#=#[?1h#=#[?1h#=#[39;49m#[39;49m#(B#[m#[H#[2J#(B#[0;7m GNU nano 2.2.6 File: poly.c #[4;1H#(B#[0;1m#[36m#include#[39m#(B#[m #(B#[0;1m#[33m<stdio.h> 491/assign_05/P2$ poly.out ba#[1@s#[A -o interpol.o#[1P#[A gcc: error: basic_lib.o: No such file or directory #]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P2#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P2$ ./poly.out Data points calcualted of the poly x y -6.000000000000000000 +89770.500000000000000000 -5.315789473684210620 +43918.919850804792076815 -4.631578947368421240 +19639.394747788868698990 -3.947368421052631859 +7852.172010329663862649 -3.263157894736842479 +2730.641875884949058673 -2.578947368421053099 +802.106291632630473032 -1.894736842105263719 +196.288694529920917375 -1.210526315789474339 +41.584779791880251310 -0.526315789473684847 +9.054257789326985773 +0.157894736842104644 +4.153599366116664804 +0.842105263157894135 +6.209769575789209739 +1.526315789473683626 +55.634949837585040200 +2.210526315789473006 +388.882248511829686777 +2.894736842105262387 +1721.142399894687741835 +3.578947368421051767 +5676.781451632283278741 +4.263157894736841591 +15367.519440554198808968 +4.947368421052630971 +36118.350056926319666672 +5.631578947368420351 +76341.201297123072436079 +6.315789473684209732 +148556.337104719015769660 +6.999999999999999112 +270561.499999999825377017 #]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P2#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P2$ ##exit exit Script done on Sun 31 Mar 2013 04:46:46 PM EDT
Script started on Sun 31 Mar 2013 04:48:41 PM EDT
#]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P3#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P3$ exit####./poly.out##########gcc -Wall poly.c -o poly.out b
gcc: error: basic_lib.o: No such file or directory
#]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P3#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P3$ .inte##[K##[K##[K##[K/interpol.out
Discrete Points for Interpolation
i x y
0 0.00000000000000000e+00 5.50000000000000000e+03
1 1.00000000000000000e+00 8.50000000000000000e+03
2 2.00000000000000000e+00 1.35000000000000000e+04
3 3.00000000000000000e+00 2.05000000000000000e+04
4 4.00000000000000000e+00 2.95000000000000000e+04
5 5.00000000000000000e+00 4.05000000000000000e+04
6 6.00000000000000000e+00 5.35000000000000000e+04
7 7.00000000000000000e+00 6.85000000000000000e+04
x interpolated y
0.00000000000000000e+00 5.50000000000000000e+03
2.50000000000000000e-01 6.25000000000000000e+03
5.00000000000000000e-01 7.00000000000000000e+03
7.50000000000000000e-01 7.75000000000000000e+03
1.00000000000000000e+00 8.50000000000000000e+03
1.25000000000000000e+00 9.75000000000000000e+03
1.50000000000000000e+00 1.10000000000000000e+04
1.75000000000000000e+00 1.22500000000000000e+04
2.00000000000000000e+00 1.35000000000000000e+04
2.25000000000000000e+00 1.52500000000000000e+04
2.50000000000000000e+00 1.70000000000000000e+04
2.75000000000000000e+00 1.87500000000000000e+04
3.00000000000000000e+00 2.05000000000000000e+04
3.25000000000000000e+00 2.27500000000000000e+04
3.50000000000000000e+00 2.50000000000000000e+04
3.75000000000000000e+00 2.72500000000000000e+04
4.00000000000000000e+00 2.95000000000000000e+04
4.25000000000000000e+00 3.22500000000000000e+04
4.50000000000000000e+00 3.50000000000000000e+04
4.75000000000000000e+00 3.77500000000000000e+04
5.00000000000000000e+00 4.05000000000000000e+04
5.25000000000000000e+00 4.37500000000000000e+04
5.50000000000000000e+00 4.70000000000000000e+04
5.75000000000000000e+00 5.02500000000000000e+04
6.00000000000000000e+00 5.35000000000000000e+04
6.25000000000000000e+00 5.72500000000000000e+04
6.50000000000000000e+00 6.10000000000000000e+04
6.75000000000000000e+00 6.47500000000000000e+04
7.00000000000000000e+00 6.85000000000000000e+04
#]0;dlabudovic@ubuntu: ~/Documents/CS4491/assign_05/P3#dlabudovic@ubuntu:~/Documents/CS4491/assign_05/P3$ exit
exit
Script done on Sun 31 Mar 2013 04:49:14 PM EDT
Comments
Post a Comment