Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

DEPARTMENT OF PHYSICS, UNIVERSITY OF COLOMBO

ELECTRONICS & COMPUTING LABORATARY II

ECL 222-A

NUMERICAL METHODS-DAY 1

Group - 11- B

Name - W.Y.Madushani

Index - 13153

Partner - K.N.M.D.M.Kosgahakumbura

Date - 2017/04/27

1
ABSTRACT

The main objective of this practical was to get a clear knowledge about the use of MATLAB
software to apply numerical methods such as „Least Square Fit Method‟ for a particular set of data
and „Newton Raphson Method‟ in order to find roots of quadratic equations.

Basically three numerical methods were discussed in the report. Those are least square fit method,
curve fitting method and Newton Raphson method.

In the first experiment, a given set of x & y data were plotted using the mat lab and least square
method was used to fit the curve and find the best curve.

In the second experiment, the dataset given in the experiment 1 was plotted with the given error
values and used the least square method to fit the curve to the best curve with error values

In the third experiment, the best fit line was generated using the MATLAB curve fitting toolbox..

Roots of a given function was revealed by using the newton Raphson method was done by fourth
experiment.

This report consists of these functions of MATLAB as well as the use of basic mathematical
functions on MATLAB using matrices, such as finding root solutions, creating loops etc

2
TABLE OF CONTENTS

1 INTRODUCTION ........................................................................................................ 6
2 THEORY ...................................................................................................................... 7
2.1 The Least Square Method ..................................................................................... 7
2.2 The Newton-Raphson method .............................................................................. 9
3 METHODOLOGY ..................................................................................................... 11
3.1 Experiment 1 ....................................................................................................... 11
3.2 Experiment 2 ...................................................................................................... 12
3.3 Experiment 3 ....................................................................................................... 13
3.4 Experiment 4 ....................................................................................................... 13
4 DATA AND ANALYSIS ............................................................................................... 15
4.1 Experiment 1 & 2 ................................................................................................ 15
4.2 Experiment 3 ....................................................................................................... 17
4.3 Experiment 4 ....................................................................................................... 17
5 DISCUSSION............................................................................................................. 19
6 CONCLUSION .......................................................................................................... 21
7 REFERENCES ........................................................................................................... 22

3
TABLE OF FIGURES

Figure 2.1: Best fitting line to a given data points ............................................................................. 7


Figure 2.2: The Graph of Newton Raphson method........................................................................ 10
Figure 4.1:Graph of experiment 1 (Best fit line without error) ....................................................... 15
Figure 4.2:Graph of experiment 2 (Best fit line with error) ............................................................ 16
Figure 4.3: Results of curve fitting tool ........................................................................................... 17

4
LIST OF TABLE

Table 4.1:Final Values obtained without error bars & with errorbars ............................................. 16
Table 4.2:Values obtained when initial value of x is 20.................................................................. 18
Table 4.3: Final Values obtained when x is changed ...................................................................... 18

5
1 INTRODUCTION

MATLAB is a high-level language and interactive environment for numerical computation,


visualization, and programming. Using MATLAB, we can analyze data, develop algorithms, and
create models and applications. The language, tools, and built-in math functions enable us to
explore multiple approaches and MATLAB is a high-level language and interactive environment
for numerical computation, visualization, and programming. The language, tools, and built-in
math functions enable us to explore multiple approaches and reach a solution faster than with
spreadsheets or traditional programming languages, such as C/C++ or Java.

MATLAB consists of mainly 3 sections. These sections are;

 Editor – Consists of the commands given to the mathematical model or program


built.
 Workspace – Consists of the current values of all variables formed in a specific
editor space.
 Command Window – Consists of the outputs of the commands given using the editor.

A very high place is given to accurate graphical representation of data in this section. One such
technique used to do so is the Curve Fitting Technique. Curve Fitting is a method of building
relationships between a model and a real system. In most cases when a set of data is
experimentally found, it may deviate from the values expected due to errors. At these moments,
curve fitting can be used to determine the correct graphical outputs needed.

Many functions are available in MATLAB which can make many calculations easy. One such
application is the MATLAB Curve Fitting Toolbox which is used extensively in this set of
exercises. Using the Curve Fitting Toolbox on MATLAB, many complex graphs can be easily
drawn as mentioned above in these occasions.

There also exist a basic method used in finding the roots of curved function which is discussed in
this section. This uses the first derivative of the function which is equated to the gradient at a
specific point. Using the subsequent x values obtained at the meeting of the tangential line and the
x axis; and repeating the above steps, the roots of the function can be observed. This method of
finding roots is called the Newton-Raphson method of finding roots.

This is an iterative method which can be used repeatedly to improve the accuracy of the root. The
process is repeated until a sufficiently accurate value is reached as the root & mostly when the
differences between two successive steps are closer to zero.

6
2 THEORY

2.1 The Least Square Method

Often more than one curve of a given type will appear to fit a set of data. To avoid individual
judgment in constructing lines, it is necessary to agree on a definition of a „best- fitting‟ line.

Figure 2.1: Best fitting line to a given data points

Definition of the Least- Squares curve

Of all curves approximating a given set of data points, the curve having the property
d12 + d22 + ……+ dn2 = min (Minimum deviation from the data points)

is the best- fitting curve.

The least squares regression line

The device on a set of values yi can be written as:

Thus

A necessary condition for this to be a minimum (or a maximum) would be;

These yields;
7
Then by simplifying we get;

Least square line in terms of sample variance & covariance.

The least square line

Where -sample correlation coefficient

Similarly, regression line of x on y can be written as;

The same ideas can be generalized to more than one variable.

Standard error of estimate

Let Yest denote the estimated value of y for a given value of x. Then, the measure of the scatter
about the regression curve is given by;

Which is called the standard error of estimate of y on x.

Since,

out of all possible curves, the least squares curve has the smallest standard
error.
Since,

We can also express the standard error of estimate using the variance and correlation coefficient.

Linear correlation coefficient

8
(By using the above equation)

i.e. the total variation = unexplained variation + explained variation, thus


= explained variation / Total variation

.(Hence r lies between 0 and 1)

r 0 the regression is not good.

The correlation coefficient is always a number between -1 and +1 and have a perfect linear
correlation.

Let assume that there is an error of σi for each measurement. Then let‟s define,

Then we can write,

Hence the unknown parameters are given by,

The above two equations represent the best-fit model parameters for a and b.

2.2 The Newton-Raphson method

This is a powerful technique available to solve equations numerically.

Suppose that it is necessary to finding the roots of the function f(x) = 0.


Then consider the following curve

9
Figure 2.2: The Graph of Newton Raphson method

Let x1 be the expected root of the above function

If it is assume that the first derivative of f(x) exists at x = x1. Let‟s name it as f ’(x) and it will be the
gradient at x = x1.
Let L be the line representing the gradient at x = x1. Now an expression for the gradient of the L
can be written as,
( )
( )

Hence,
( )
( )

The process is stop when tolerance (difference between two successive steps) is close to zero
which is approximately 10-10. finally, the root would be the value of Xn+1.

10
3 METHODOLOGY

3.1 Experiment 1
New MATLAB file was created choosing Home > new script. 2 matrices were made, one for
Period (minutes) and the other for Temperature (Celsius). Temperature vs Time graph was plotted.
Data points were marked using „x‟ with the help of attributes in the MATLAB plot command.
Time was taken as the x axis. Next, this graph was plotted on a graph by using the commands
given below.
clear all
close all
clc
X=[1:1:30]
Y=[30.6 31.2 32 32.8 33.8 34.6 35.4 36.4 37.4 38 38 40 40.8 40.6 42 43 43.6
44.6 45.2 46 46.4 47.2 47.8 48.6 49 49.4 50 50.8 52 52.8 ]
plot(X,Y,'x')
xlabel('Period')
ylabel('Temperature')

Hence the obtained graph was noted.

To obtain the gradient and intercept of the graph using square fit method the equations below were
typed in the editor and was executed.

To find the intercept and gradient;

∑ ∑ ∑ ∑ ∑ ∑ ∑
∑ (∑ ) ∑ (∑ )

Where is the gradient and is the interception.

The commands given in the editor field were;


n=30;
a=(((sum(Y))*(sum(X.^2)))-((sum(X))*(sum(X.*Y))))/(n*(sum(X.^2))-(sum(X)^2))
b=(n*(sum(X.*Y))-(sum(X))*(sum(Y)))/(n*(sum(X.^2))-(sum(X)^2))

Best fit line was plotted using plot command. Hold on key word was used to plot both graph in
same figure. Hence the line was plotted and the gradient, intercept as well as the standard error and
linear coefficient of correlation were noted.
hold on;
Z=a+b*X;
gradient=b
intersection=a
plot(X,Z)
c=((sum(Y.^2))-(a*(sum(Y)))-(b*(sum(X.*Y))))/n;
standard_error=sqrt(c)
m=mean(Y);
d=(sum((Y-m).^2))/n;
r=1-(c/d);
coefficient_of_correlation=sqrt(r)

11
3.2 Experiment 2
In this experiment, the same graph above was constructed including errors and error bars.

To make use of the data, a third array named e was created and the errors were added to the editor.
e=[0.5 0.5 0.5 0.2 0.2 0.2 0.2 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.2 0.2 0.2 0.2 0.2
0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5];
figure;
xlabel('Period')
ylabel('Temperature')
errorbar(Y,e,'.')

Since errors now exist in the data, the previous equations cannot be used. Hence equations of the
Standard Error of Estimates and hence obtained gradient and intercept values were used to draw
the graphs. These equations are given below.

∑ ∑ ∑ ∑ ∑

Hence the gradient and intercept is given by;

( )

Hence the results of both figures were compared and results were given.
A=(sum(1./(e.^2)));
B=(sum(X./(e.^2)));
C=(sum(Y./(e.^2)));
D=(sum((X.^2)./(e.^2)));
E=(sum((X.*Y)./(e.^2)));
F=(A*D)-(B^2);
p=((D*C)-(B*E))/F
q=((A*E)-(B*C))/F
R=p+q*X;
hold on;
plot(X,R)
gradient_2=q
intersection_2=p

12
3.3 Experiment 3
MATLAB curve fitting tool was opened and the necessary data points were added. As “X data”
and “Y data” variable X and Y was selected respectively. Degree was set to 1 since we are
expecting a straight line.

A linear polynomial fit was done using the toolbox to compare with the values obtained in
experiment 1.

3.4 Experiment 4
MATLAB built in function (key word “roots”) was used to find the roots of the function

F (X) =X2-5.
clear all
close all
clc
F=('x'^2)-5;
g=[1 0 -5];
r=roots(g)

The Newton Raphson method was used to obtain the results and they were compared with the real
answer. The initial value was taken as 20.
clear all
close all
clc
x=20;
for i=1:20
x1=x-(((x^2)-5)/(2*x));
13
x=x1;
if((i==1) || (i==2) ||(i==5) ||(i==10)|| (i==20))
disp(i);
disp(x);
end
end

As the next program, MATLLAB script was written for find a root of an equation for given
tolerance by the user. Above code was modified to get the tolerance from the user.
clear all
close all
clc
x=20;
tolerance=input('enter the tolerance')
while(true)
x1=x-(((x^2)-5)/(2*x));
if((x1-x)==10^-10)
disp(x)
break
end
x=x1;
end

As the final part of the experiment, roots of the f(x) = X2 – 5 was computed for different initial
values.
disp(x)
x=-20;
for i=1:20
x1=x-(((x^2)-5)/(2*x));
x=x1;
end
disp(x)
x=0;
for i=1:20
x1=x-(((x^2)-5)/(2*x));
x=x1;
end
disp(x)

14
4 DATA AND ANALYSIS

4.1 Experiment 1 & 2

Figure 4.1:Graph of experiment 1 (Best fit line without error)

Above graph was illustrated using data set in table 3.1 and also the method used was least square
method.

15
Figure 4.2:Graph of experiment 2 (Best fit line with error)

Above graph was illustrated using data set in table 3.2 and also the method used was least square
method.

Two least square lines are slightly different from each other. Gradient of the line corresponding to
without errors is greater than that of the line with error bars. There is no considerable difference
between them.

Table 4.1:Final Values obtained without error bars & with errorbars

Attribute Values Without Values With


errors errors
Gradient b= 0.7619 b1=0.7797
Intersection a= 30.1903 a1=30.1370
Standard error s= 0.4479 St=0.5233
linear coefficient of correlation r= 0.9977 r1=1.0215
According to the table 4.1 if there is an error in the measurement of temperature, above attributes
are slightly difference from the normal attribute values.

16
4.2 Experiment 3

 Figure 4.3 shows the graph illustrated using data set in table 3.1 (same as experiment1) but
the method used was curve fitting method. Since line equation is p1x+p2, gradient is
0.7619 and intersection is 30.19. The values observed by using curve fitting tool box are
equal to values which are observed by least square method in experiment 1. This method is
easier tool to draw best lines.

Figure 4.3: Results of curve fitting tool

4.3 Experiment 4

5) F (x)= x2 -5
roots: 2.2361, -2.2361
b)

17
Table 4.2:Values obtained when initial value of x is 20

Number of iterations Real value Obtained value Error


1 2.2361 10.1250 7.8889
2 2.2361 5.3094 3.0733
5 2.2361 2.2395 0.0034
10 2.2361 2.2361 0
20 2.2361 2.2361 0

According to the Newton_Raphson method, twenty x2 values were obtained with 20 iterations
setting initial x value 20. That x2 value has gradually reached the real root of the function
(2.2361). At the both 10th and 20th times, obtained value is equal to the real value. Process of
finding root had to be stopped when difference between x2 values were close to zero. So obtained
value has finally tallied with real roots of the function.

Table 4.3: Final Values obtained when x is changed


(C)

Initial X value Result Explanantion


X = 20 2.2361 When initial X is 20, gradient of f(x) at the
corresponding point is positive. Process has
started with bigger positive value and
gradually decreased to the real positive root
(2.2361) of F (x)within 20 iterations.

X = -20 -2.2361 When initial X is -20, gradient of f(x) at the


corresponding point is negative Process has
started with smaller negative value and
gradually increased to the real negative root (-
2.2361) of F (x) within 20 iterations.

X=0 NaN When initial X= 0, gradient of F(x) is zero at


that point. Hence tangent at that point will
never cross the x-axis, there‟s no more
iteration. Numerically what happens here is,
function is dividing by zero, which can‟t be
done. Hence here the Newton Raphson method
violates.

18
5 DISCUSSION
The main purpose about this practical was to study about the usage of the computer software
program MATHLAB in order to do more accurate analysis on curves, finding roots.In this
practical there were 4 experiments.

The 1st experiment was based on the Curve Fitting Equations for functions with no errors. Using
the given data and equation, a straight line graph was obtained and plotted on the same axes were
the pointed experimentally found.

In the experiment 2 however had the same graph to be drawn but taking errors into consideration.
Hence when the graph was given to be drawn, the given equations must be changed. The thus
obtained graph also followed the same data points as the previous case but did not have the same
gradient and intercept values. This graph had slightly increased its gradient and slightly decreased
its interception. The best fitting curve for a given function can be obtained from the graph, which
is drawn without error bars.
Correlation coefficient was calculated to get an idea about how good the equation for predictive
purposes. A perfect correlation of ± 1 occurs only when the data points all lie exactly on a straight
line. If correlation coefficient = +1, the slope of this line is positive and if correlation coefficient =
-1, the slope of this line is negative.
In experiment 3, the same graph was drawn again without considering the errors using the
MATLAB Curve Fitting Tool and hence obtained graph was identical to the graph obtained in
experiment no.1. The main reason for this is because the Tool box also uses the same equations
used when building the graph for experiment no.1.
Another fact observed is the fact that these equations in Curve Fitting takes into account all
available points when drawing its graph. Hence due to outliers the graphs obtained may be
incorrect. But by using the Curve Fitting tool, these outliers can be removed. In the Curve Fitting
Tool, there exists a button which says „Exclude Outliers‟. Using this button, outliers are removed
and a much more accurate graph can be taken. This toolbox also can be used when the graph is not
linear. At these occasions, instead of selecting Linear Polynomials, user can select other relations
such as Quadratic Polynomials, 4th Degree Polynomials, Powers and Interplants etc.
As the difference between using least square method and Mat lab toolbox was compared while
carrying out the experiment, it was observed that more accurate results can be obtained from the
least square method rather than using the 'curve fit tool' in Matlab. Therefore if one need to obtain
more accurate results, least square method is more suitable to use. But least square fit method is
not suitable for a data set with a lot of outliers as the best fitting line obtained through this method
deviates from real scenario giving a wrong result.
The last experiment is based on the roots of polynomials. Here the needed polynomial is entered in
a matrix form with the highest ordered coefficient first. Hence by using the „roots‟ function, the
roots can be obtained. Newton Raphson method gives approximate values to roots of a
polynomial. Here a program based on Newton Raphson method was written in Matlab to find the
roots of a quadratic equation to the given accuracy and also was to accurate. In the second part of
this experiment, the roots were to be found using the Newton Raphson method and using the loops
as described previously this was successfully completed. Here the main observation was that when
the number of iterations increased, the accuracy of the answer also increased. Another observation
was that when when a positive initial value is given a positive output is obtained and when a
negative initial value is given a negative output is received, and when the initial X value is zero the
output will be as NAN(not a number) which indicated the output is not a number and this could be
due to the fact that when x=0 the limit of the function is zero hence it is not possible to find a
gradient at this point and the Newton Rapshon method fails at this points thus giving NAN.A
19
tolerance can be given based on the accuracy expected and to avoid printing the same figure on
command window. A proper combination of “while” loop and “if” statement is a one way to
ensure the iteration and limit the iteration with tolerance command. A tolerance can be given based
on the accuracy expected and to avoid printing the same figure on command window.
By completing those two set of practical successfully, through knowledge about how those
numerical methods can be applied for mathematical operation was obtained.

20
6 CONCLUSION
Curve Fitting Functions can be used to draw highly accurate graphs.When there exists error in the
data points, the previously used functions will also have errors and therefore a separate set of
functions must be used.Error bars can be drawn in a graph using the „errorbar‟ function on
MATLAB.

MATLAB Curve Fitting Tool can also be used to draw curves when a set of data points are
given.Then using this toolbox, it is necessary to give the type of function needed to be obtained,
such as 2nd order polynomial, 3rd order polynomial, ratio etc.

Comparing the results obtained using least square method and the curve fitting method it can be
concluded that they are nearly same. Curve fitting method is the easiest way to plot graphs and
draw best fitting lines.

To find the roots of a polynomial, the polynomial must be entered as an array with the highest
order coefficient first. It is after this that the root function can be used.Newton_Raphson method
gives the roots of a function after several iterations which depend on the chosen initial value. That
value gradually reaches the real root of the function. It is better to choose the initial value closer to
its root.With the increase of iterations, the accuracy of the function increases.

21
7 REFERENCES
Least Squares Fitting -- from Wolfram MathWorld. [online] Available at:
http://mathworld.wolfram.com/LeastSquaresFitting.html [Accessed 27 Apr. 2017].

Nptel.ac.in. (2017). Newton-Raphson Method:. [online] Available at:


http://nptel.ac.in/courses/122104019/numerical-analysis/Rathish-kumar/ratish- 1/f3node6.html
[Accessed 27 Apr. 2017].

Anon, (2017). [online] Available at: https://onlinecourses.science.psu.edu/stat200/node/80


[Accessed 27 Apr. 2017]. Mathworld.wolfram.com. (2017).

Sosmath.com. (2017). The Newton-Raphson Method. [online] Available at:


http://www.sosmath.com/calculus/diff/der07/der07.html [Accessed 27 Apr. 2017].

Spiff.rit.edu. (2017). Finding Roots by "Open" Methods. [online] Available at:


http://spiff.rit.edu/classes/phys317/lectures/open_root/open_root.html [Accessed 27 Apr. 2017].

Davdata.nl. (2017). the Least Squares method. [online] Available at:


http://www.davdata.nl/math/leastsquares.html [Accessed 27 Apr. 2017].

theory section: Lab sheet of ECL222 – Numerical Methods

22
APPENDIX – A

23

You might also like