You are on page 1of 10

#1

CURVE FITTING : POLYNOMIAL FUNCTIONS


THEORY:
INTRODUCTION
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit
to a series of data points, Curve fitting can involve either interpolation, where an exact fit to the data
is required, or smoothing in which a "smooth" function is constructed that approximately fits the data.
The main objectives of curve fitting are as follows:
1. to infer values of a function where no data are available
2. to summarize the relationships among two or more variables
3. as an aid for data visualization for scattered data
4. regression analysis of data.

There are various methods of curve fitting, among which the two of them aare discussed in
this lab session, viz linear curve fitting and exponential curve fitting.

Polynomial curve FITTING

Similar to the linear curve fitting technique, the polynomial curve fitting is also a curve fitting
method which finds the best fitting polynomial equation of nth order specified initially. In cases
when data collection are really scattered and sparse, linear curve fitting doesn’t serve to be quite
well and a higher degree polynomial equation is required which can fit the data.
For polynomial equation of order j , the general form of the equation is:

The general expression for any error using the least squares approach is:

To minimize the errors the equation is partially differentiated with respect to ao, a1,a2,……… and
setting to zero we derive the normal equations which can be written in a matrix form in general
as:
Thus, by calculating all the sums of the variables required, the normal equations when solved
gives the value of constants a0,a1,a2,……..ad which defines the polynomial equation that best fits the
points.

PSEUDOCODE:
1. Declare the variables: n, d, c
2. Declare the arrays: x[10],y[10],z[10], a[10][20]
3. Input the number of readings to be taken
4. Input the degree of equation
5. Input the data readings for xi an yi
6. Form a matrix which stores the necessary sums for normal equation:
For i=0 to d
For j= 0 to d+1
aij=0
for k=0 to n
if j=d+1
ai,d+1=∑(xk)i *yk
else
ai,j=∑(xk)i+j
end for
end for
7. Display the matrix
For i=0 to d
For j=0 to d+1
Print aij
8. Solve the equation in matrix form using Gauss Jordan method:

a. for j=0 to d
if(|ajj|<0.0005)
print error and exit
b. for i=1 to d
if(i!=j)
r=aij/ajj
for k=0 to d+1
aik=aik-r*ajk
c. for i=1 to d
xi=ai,n+1/aij
d. print xi

CODING:
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

int main()
{
int n ,d;
cout<< "\nEnter the no. of inputs and degree of equation:";
cin>>n >> d;
float c;
float x[10],y[10],z[10],a[10][20];
cout<< "\n enter the value of x and y:";
for(int i=0;i<n;i++)
cin>>x[i]>>y[i];
for(int i =0;i<=d;i++)
{
for(int j=0;j<=d+1;j++)
{ a[i][j]=0;
for(int k=0;k<n;k++)
{if (j==d+1)
a[i][d+1]+=(pow(x[k],i)*y[k]);
else
a[i][j]+=pow(x[k],(i+j));
}
}
}
for (int i=0;i<=d;i++)
{
cout<<"\n";
for (int j=0;j<=d+1;j++)
{
cout<<"\t"<<a[i][j];
}
}
for(int j=0; j<=d; j++)
{
if(fabs(a[j][j])<0.0005)
{
cout<<"\n error occured";
exit(0);
}
for(int i=0; i<=d; i++)
{
if(i!=j)
{
c=a[i][j]/a[j][j];
for(int k=0; k<=d+1; k++)
{
a[i][k]=a[i][k]-c*a[j][k];
}
}
}
}
for (int i=0;i<=d;i++)
{
cout<<"\n";
for (int j=0;j<=d+1;j++)
{
cout<<"\t"<<a[i][j];
}
}
cout<<"\nThe solution from higher to lower order coefficients:\n";
for(int i=0;i<=d;i++)
{
z[i]=a[i][d+1]/a[i][i];
cout<<"\n"<<z[i];
}
}
OUTPUT:

The coding above generated the following as output for different set of functions:

1. Enter the no. of inputs and degree of equation:7 2


enter the value of x and y:
1 22
Chart Title
2 29 80
70 y = 2.9035x2 + 0.5526x + 18.439
2 33 60
50
3 39
40
3 48 30
20
3 52 10
0
4 67 0 1 2 3 4 5

7 18 52 290
18 52 162 831
52 162 532 2593

7 5.32528e-006 8.64999e-007 129.071


-1.00387e-006 5.71428 8.39305e-007 3.15764
2.5034e-007 7.79969e-007 5.7 16.5501
The solution is:
18.4386
0.552587
2.90352

2. Enter the no. of inputs and degree of equation:5 2

enter the value of x and y:2 32


3 43
4 48
5 32
5 54

5 19 79 209
19 79 349 815
79 349 1603 3433

4.99997 -1.23022e-005 -1.73217e-


006 -79.9984
2.85793e-005 6.80001 1.86293e-006 217.25
-2.66468e-006 -9.64892e-007 4.58829 -18.4707
The solution from higher to lower order coefficients::

-15.9998
31.9485
-4.02561

3. Enter the no. of inputs and degree of equation:6 3

enter the value of x and y:2 12 80


2 23
y = 0.3561x3 - 5.241x2 + 35.978x - 36.568
3 32 60
4 49
5 55 40
6 68
20

6 22 94 448 239 0
22 94 448 2290 1045 0 1 2 3 4 5 6 7
94 448 2290 12232 5035
448 2290 12232 67234 25843

5.99968 -0.000579774 0.000115703 -9.56414e-006 -219.384


0.000646451 13.3345 -0.000236324 -2.08266e-005 479.66
-0.000219929 -0.000387667 16.5 -6.23487e-006 -86.4589
1.71141e-005 2.98009e-005 -6.11207e-006 15.1676 5.40005

The solution from higher to lower order coefficients::

-36.5659
35.9714
-5.23991
0.356026

DISCUSSION AND CONCLUSION :


Thus, from this lab session, we could simulate the method of curve fitting for finding the best
fitting equation for a given set of scattered data based on our knowledge on computer programming.
Moreover, the graphical concepts regarding the operation mechanism of those methods was also
clarified with the help of the above written code.
#2
RK-4: 2ND ORDER HOMOGENOUS EQUATIONS
THEORY:
The second order differential equation of the form :

𝑑2𝑦 𝑑𝑦
= 𝑓(𝑥, 𝑦, )
𝑑𝑥 2 𝑑𝑥

For two general 1st order ODE's


𝑑𝑦
= 𝑓(𝑥, 𝑦, 𝑧)
𝑑𝑥
𝑑𝑍
= 𝑔(𝑥, 𝑦, 𝑧)
𝑑𝑥

The 4th order Runge-Kutta formula's for a system of 2 ODE's are:


PSEUDOCODE:
1.Define function f(x,y) and variables x0,y0,z0,xn,h,k1,m1,k2,m2,k3,m3,k4,m4
2.Read the input variables:
a. enter the initial value :: x0,y0,z0
c. step size :: h
d the no of iterations of x at which the value of function is to be determined ::xn
3. Loop while (x0!=xn)
k1=h*f(xo,yo,zo)
m1=h*g(xo,yo,zo)
k2=h*f(xo+h/2,yo+k1/2,zo+m1/2)
m2=h*g(xo+h/2,yo+k1/2,zo+m1/2)
k3=h*f(xo+h/2,yo+k2/2,zo+m2/2)
m3=h*g(xo+h/2,yo+k2/2,zo+m2/2)
k4=h*f(xo+h,yo+k3,zo+m3)
m4=h*g(xo+h,yo+k3,zo+m3)
k=(k1+2*k2+2*k3+k4)/6
m=(m1+2*m2+2*m3+m4)/6
yo=yo+k
zo=zo+m
xo=xo+h
y0=y0+k
4. print the value of x0 , y0 , z0

CODING:
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
float f(float x , float y,float z)
{
return z;
}
float g(float x , float y,float z)
{
return x*z*z-y*y;
}
int main()
{
float xo,yo,zo,xn,h,k1,k2,k3,k4,k,m1,m2,m3,m4,m;
cout<<"enter xo,yo,zo,xn,h"<<endl;
cin>>xo>>yo>>zo>>xn>>h;
do
{
k1=h*f(xo,yo,zo);
m1=h*g(xo,yo,zo);
k2=h*f(xo+h/2,yo+k1/2,zo+m1/2);
m2=h*g(xo+h/2,yo+k1/2,zo+m1/2);
k3=h*f(xo+h/2,yo+k2/2,zo+m2/2);
m3=h*g(xo+h/2,yo+k2/2,zo+m2/2);
k4=h*f(xo+h,yo+k3,zo+m3);
m4=h*g(xo+h,yo+k3,zo+m3);
k=(k1+2*k2+2*k3+k4)/6;
m=(m1+2*m2+2*m3+m4)/6;
yo=yo+k;
zo=zo+m;
xo=xo+h;
}while((xo!=xn));
cout<<"\nx\t \ty \t\tz\n";
cout<<xo<<" \t|\t "<<yo<<" \t|\t "<<zo<<endl;
}

OUTPUT:
The coding above generated the following as output for different set of functions:
1. for yII=xz2-y2
enter xo,yo,zo,xn,h
0
1
0
0.2
0.1
x y z
0.2 | 0.980148 | -0.196969

2. enter xo,yo,zo,xn,h
0
0
1
0.3
0.1
x y z
0.3 | 0.303926 | 1.03763

3. for yII=2x-y2+z
enter xo,yo,zo,xn,h
0
0
1
0.3
0.1
x y z
0.3 | 0.358695 | 1.43698

DISCUSSION AND CONCLUSION :


Thus, from this lab session, we could simulate the method of RK-4 for solving a second order
homogenous ordinary differential equation based on our knowledge on computer programming.
Moreover, the graphical concepts regarding the operation mechanism of those methods was also
clarified with the help of the above written code.

You might also like