Professional Documents
Culture Documents
rk4 2ndcurfitpol
rk4 2ndcurfitpol
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.
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:
7 18 52 290
18 52 162 831
52 162 532 2593
5 19 79 209
19 79 349 815
79 349 1603 3433
-15.9998
31.9485
-4.02561
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
-36.5659
35.9714
-5.23991
0.356026
𝑑2𝑦 𝑑𝑦
= 𝑓(𝑥, 𝑦, )
𝑑𝑥 2 𝑑𝑥
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