Batch 5
Batch 5
INTRODUCTION
has the best fit to a series of data points, possibly subject to constraints. Curve fitting
can involve either interpolation, where an exact fit to the data is required or
data.
statistical inference such as how much uncertainty is present in a curve that is fit to
data observed with random errors. Fitted curves can be used as an aid for data
visualization to infer values of a function where no data are available and to summarize
the relationships among two or more variables. Extrapolation refers to the use of a
fitted curve beyond the range of the observed data and is subject to a degree of
(Sun) in 1991. The target of Java is to write a program once and then run this program
1
1|Page
on multiple operating systems. The first publicly available version of Java (Java 1.0) was
released in 1995. Sun Microsystems was acquired by the Oracle Corporation in 2010.
Oracle has now the statesmanship for Java. In 2006 Sun started to make Java available
under the GNU General Public License (GPL). Oracle continues this project
called Opened. Over time new enhanced versions of Java have been released. The
compiler, core libraries and a runtime (Java virtual machine). The Java runtime allows
software developers to write program code in other languages than the Java
programming language which still runs on the Java virtual machine. The Java platform is
Platform Independent: Java programs use the Java virtual machine as abstraction
and do not access the operating system directly. This makes Java programs highly
portable. A Java program (which is standard-compliant and follows certain rules) can
the used variables must be pre-defined and conversion to other objects is relatively
Interpreted and Compiled Language: Java source code is transferred into the byte
code format which does not depend on the target platform. These byte code
instructions will be interpreted by the Java Virtual machine (JVM). The JVM contains a
Automatic Memory Management: Java manages the memory allocation and de-
allocation for creating new objects. The program does not have direct access to the
Working in a cloud
3
3|Page
Performing big data analysis
Making games
4
4|Page
2. METHOD OF LEAST SQUARES
fitting that has been popular for a long time. Least squares minimize the square of the
error between the original data and the values predicted by the equation.
Suppose the curve y=f(x) is fitted to this data. Let the observed value at x= x iis y i
S =¿+¿+…………….+¿
= e 12+e 22+……+e m2
The method of least squares consisting in minimizing S that is the sum of squares
of errors.
5
5|Page
3. FITTING OF A STRAIGHT LINE
Let y=a 0+a 1x be the straight line to be fitted to the given data, where a 0 and a1
∂s ∂s
If S to be minimum, we have ∂ a =0 and ∂ a =0.
0 1
∂s
For ∂ a =0,
0
n n
⇒ ma 0+a 1 ∑ x i = ∑ yi …………….(2)
i=1 i=1
∂s
For ∂ a =0,
1
⇒-2 x 1[ y 1-(a 0+a 1 x 1)]-2 x 2[ y 2-(a 0+a 1 x 2)]-…………..-2 x m[ y m-(a 0+a 1 x m)]=0
6
6|Page
⇒ x 1 y 1 + x 2 y 2+………+ x m y m= (a 0 x 1+a 0 x 2+……….+a 0 x m)+(a 1 x 12 +a 1 x 22+…….+a 1 x m2)
m m m
⇒ a 0 ∑ x i+a 1 ∑ x i2 = ∑ x i y i ……………(3)
i=1 i=1 i=1
∴ The values of x i and y i are known and the equations (2) & (3) can be solved for
3.2 Example
Find the straight line that best fit the following data.
Length(x) 1 2 3 4 5
Width(y) 14 27 40 55 68
Solution:
xi yi x i2 xi yi
1 14 1 14
2 27 4 54
3 40 9 120
4 55 16 220
5 68 25 340
∑ x i=1 ∑ yi=20 ∑ x i2=55 ∑ x i y i=748
5 4
7
7|Page
From the above table, we have
ma 0+a 1 ∑ x i= ∑ yi
a 0 ∑ x i+a 1 ∑ x i2=∑ x i y i
// x and y
import java.io.*;
public class A
8
8|Page
// function to calculate m and c that best fit points
int n = x.length;
sum_xy = 0, sum_x2 = 0;
sum_x += x[i];
sum_y += y[i];
c = (sum_y - m * sum_x) / n;
}
9
9|Page
// Driver main function
int x[] = { 1, 2, 3, 4, 5 };
bestApproximate(x, y);
3.4 OUTPUT
10
10 | P a g e
Graph:
Y-Values(width)
80
70
f(x) = 13.6 x + 0
60 R² = 1 Y-Values(width)
50 Linear (Y-
Values(width))
40
30
20
10
0
0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5
Figure 1
11
11 | P a g e
4 FITTING OF nth DEGREE POLYNOMIAL
4.1 MATHEMATICAL APPROACH
Let the given set of data points be ( x i, y i),i=1,2,3,……..m.
Let the polynomial of nth degree to be fitted to the given data points be
S=¿ +¿
2
[ y −(a + a x +a
2 0 1 2 2 x 22 + …+ an x 2n ) +…+¿
]
2
[ y m−( a0 +a1 x m +a2 x m2+ …+an x mn ) ] ………………..(2)
12
12 | P a g e
¿
……….(3)
The system of n+1 equations in equation (3) are in n+1 unknowns a 0, a 1, a 2,…, a n by
solving these equations we get the values of these unknowns and get the required
4.2 Example
Fit a polynomial of second degree to the data points given in the following table.
m m m m
a 0 ∑ x i+a 1 ∑ x i +a 2 ∑ x i3
2
∑ xi yi =
i=1 i=1 i=1 i=1
m m m m
3 4 5 9 7 0 4
Substitute the above values in equation (1), we get the required 2 nd degree
polynomial is y=1+2x+3 x 2.
14
14 | P a g e
import java.util.Arrays;
import java.util.function.IntToDoubleFunction;
import java.util.stream.IntStream;
int n = x.length;
double xm = Arrays.stream(x).average().orElse(Double.NaN);
double ym = Arrays.stream(y).average().orElse(Double.NaN);
orElse(Double.NaN);
average().orElse(Double.NaN);
15
15 | P a g e
for (int i = 0; i < x.length && i < y.length; ++i)
double a = ym - b * xm - c * x2m;
16
16 | P a g e
IntToDoubleFunction abc = (int xx) -> a + b * xx + c * xx * xx;
System.out.println(" x y y1");
polyRegression(x, y);
4.4 OUTPUT
17
17 | P a g e
Graph:
Y-Values
18
16 f(x) = 3 x² + 2 x + 1
R² = 1
14
12
Y-Values
10 Polynomial (Y-Values)
8
0
0 0.5 1 1.5 2 2.5
Figure 2
y=ae bx ………(1)
18
18 | P a g e
where a, b are arbitrary constants.
Ln y=ln(ae bx)
=ln a+bx ln e
y=A+bx ………..(2)
Equation (2) is of the form of a straight line and the normal equations are
m m
m m m
2
A∑ x i+b∑ x i =∑ x i y i ……..(4)
i=1 i=1 i=1
By solving the above normal equations (3) & (4), we get the values of A & b and
5.2 Example
19
19 | P a g e
Determine the constants a & b by the method of least squares such that y=a e bx fits
Length(X 2 4 6 8 10
)
Width(y) 4.077 11.084 30.128 81.897 222.62
SOLUTION:
y= A+bx ……………(2)
Equation (2) is of the form of a straight line and the normal equations to
equation(2) are
m m
mA+b∑ x i =∑ yi …………(3)
i=1 i=1
m m m
A∑ x i+b∑ x i2 =∑ x i y i ………(4)
i=1 i=1 i=1
20
20 | P a g e
The table below follows fro m the data.
xi yi Y i=ln y i x i2 xi Y i
2 4.077 1.4054 4 2.8108
4 11.084 2.4055 16 9.6220
6 30.128 3.4055 36 20.4330
8 81.897 4.4055 64 35.2440
10 222.62 5.4055 100 54.0550
m m m m
∑ x i=30, ∑ Y i=17.0274,
i=1
∑ x i2=220,∑ x i Y i=122.1648
5A+30b=17.0274 ……..(5)
30A+220b=122.1648 ……..(6)
A=0.4054,b=0.5
a=e A
21
21 | P a g e
=e (0.4054)
=1.499902341
import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoints;
{
final WeightedObservedPoints obs = new WeightedObservedPoints();
obs.add(2,4.077);
obs.add(4,11.084);
obs.add(6,30.128);
obs.add(8,81.897);
obs.add(10,222.62);
22
22 | P a g e
final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(1);
System.out.println(Arrays.toString(coeff));
} }
5.4 OUTPUT
Graph:
23
23 | P a g e
Y-Values
250
100 Y-Values
Exponential (Y-
50 Values)
0
1 2 3 4 5 6 7 8 9 10 11
Figure 3
CONCLUSION
24
24 | P a g e
In real life problems, it is necessary to fit curves for hundreds of given data points.
It is not possible to do this type of problems manually. So, this project with the source
codes will help for dealing with more number of data points
polynomial of nth degree by using the least square method are revised and written the
source code in java language for these methods executed and shown output.
25
25 | P a g e
REFERENCES
S.S.Sastry, Introductory Method of Numerical Analysis, Fourth Edition, PHI
1984.
26
26 | P a g e