Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ECL 222 Numerical Methods-Day 2: Department of Physics, University of Colombo Electronics & Computing Laboratary Ii

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

DEPARTMENT OF PHYSICS, UNIVERSITY OF COLOMBO

ELECTRONICS & COMPUTING LABORATARY II

ECL 222

NUMERICAL METHODS- DAY 2

Group - 11- B

Name - W.Y.Madushani

Index - 13153

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

Date - 2017/05/04

1
ABSTRACT

The main objective of this practical was to get a clear knowledge about the use of
MATLAB software to apply numerical methods. Another objective was to expand the
knowledge on use of MATLAB curve fitting tool box to fit a curve of a given data set.
Basically seven numerical methods were discussed in the report. Simpson rule,
Trapezoidal rule and Monte Carlo method were used to integrate functions which is given
in and Euler‟s method and Runge-Kutta method were used to solve first order and second
order ordinary linear differential equations.

In the first experiment Simpson‟s 1/3th rule was used to integrate a function.

As the next experiment, curve fitting toolbox of MATLAB was used to find the equation
of a given set of data points.

In the third experiment value of PI was approximated using the Monte Carlo method.

Fourth and fifth experiments were about solving a first order differential equation and a
second order differential equation respectively. Runge-Kutta 4th order method was
applied to solve the first order differential equation and Euler‟s method was applied to
solve the second order differential equation..

2
TABLE OF CONTENTS

1 INTRODUCTION ........................................................................................................ 6
2 THEORY ...................................................................................................................... 7
2.1 Integrating of a function ....................................................................................... 7
2.1.1 Riemann sum method ........................................................................................... 7
2.1.2 Trapezoidal Rule .................................................................................................... 8
2.1.3 Simpson’s method................................................................................................. 9
2.2 Differentiating of a function ................................................................................ 10
2.2.1 Euler’s method .................................................................................................... 10
2.2.2 Runge-Kutta method ........................................................................................... 11
3 METHODOLOGY ..................................................................................................... 13
4 RESULT AND ANALYSIS ....................................................................................... 20
5 DISCUSSION ............................................................................................................. 24
6 CONCLUSION........................................................................................................... 26
7 REFERENCES ........................................................................................................... 27

3
TABLE OF FIGURES

Figure 2.1:Integral of a function ........................................................................................................ 7


Figure 2.2: Graphical Representation of Riemann sum .................................................................... 7
Figure 2.3:Graphical representation of methods of Riemann sums ................................................... 8
Figure 2.4:Graphical Representation of Trapezoidal Rule ................................................................ 8
Figure 2.5: Graphical representation of Monte Carlo method ......................................................... 10
Figure 2.6: Graphical Representation of Euler‟s method ................................................................ 10
Figure 2.7: Graphical representation of Mid-point method ............................................................. 11
Figure 2.8 : Graphical Representation of Runge-Kutta method ...................................................... 12
Figure 3.1:Graph of pressure vs the volume for given data ............................................................. 21
Figure 3.2:MATLAB curve fitting tool ........................................................................................... 21
Figure 3.3: Graph of solved initial value problem using the Runge Kutta method and real
graph ................................................................................................................................................ 22
Figure 3.4: Graph when step size decreased to 0.02 ........................................................................ 23
Figure 3.5: Graph of solved initial value problem and f(t)=2e^(-t) cos(√3 t) graph........................ 23

4
LIST OF TABLE

Table 3.2:Data set of volume and pressure ........................................................................ 14


Table 3.3: Error of integral VS Number of iterations ........................................................ 20

5
1 NTRODUCTION
MATLAB is a high-level language and interactive environment for numerical
computation, visualization, and programming. Using MATLAB, we can analyse 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 spread sheets 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.

Integration and the differentiation are most common methods used in math operations.
The trapezoidal rule is one of the method to find the area under the curve, which is the
operation of the integration. In numerical analysis, Simpson's rule is a method
for numerical integration, the numerical approximation of definite integrals. Monte Carlo
method is a broad class of computational algorithms that rely on
repeated random sampling to obtain numerical results. They are often used
in physical and mathematical problems and are most useful when it is difficult or
impossible to use other mathematical methods. The Euler method is
a numerical procedure for solving ordinary (ODEs) with a given initial value. Rung-Kutta
method is a higher approximation to the midpoint rule.

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.

6
2 THEORY

2.1 Integrating of a function


Integration of a function can be explained in simple way using graphical method as the
area of a region underneath a curve (area between curve of given function an x-axis).

Figure 2.1:Integral of a function

Above mentioned area will be bounded by x=a and x=b. Hence integral can be given as
follows,

∫ =S

2.1.1 Riemann sum method


Riemann sum method is one method which can be used to find integrations of functions.
In this method integration of a function was calculated by using the area between function
of the curve and the x-axis by dividing the area underneath the curve into small
rectangular squares and getting the total area by the sum of the individual rectangular
areas. There are three types of methods used to get Riemann sum of given functions.

Figure 2.2: Graphical Representation of Riemann sum

7
Figure 2.3:Graphical representation of methods of Riemann sums

 The left side of rectangle:-


In this method use right end points, right shoulder of rectangle touches the curve.
 The right side triangle:-
In this method using left end points, left shoulder of rectangle touches the curve.
 The middle of the triangle:-
Using midpoints of each subinterval, so the segment containing the midpoint of
the subinterval is used as the height for that rectangle.

2.1.2 Trapezoidal Rule

Figure 2.4:Graphical Representation of Trapezoidal Rule

The trapezoidal rule is the most simplest form of finding the area under a curve with less
errors. In this method the area will divide into large number of trapezoids and finding the
total area by adding the areas of those individual trapezoids. To reduce the error of total
area value, we had to increase the number of trapezoids.

Let f(x) defined on a to b and let xi be an individual point between x= a to b. Then definite
integral defined as follows,

∫ ( ) ( )

8
Where =

There will be slight difference between the exact value and the calculated value because
most of times there is difference between the total sum of area values of individual
trapezoids and the real area beneath the curve. Therefore want to select method with less
of error can occur.

2.1.3 Simpson‟s method


Can be reduce the error of the integrals by using another method which called Simpson‟s
method. Error in trapezoidal rule is twice than the midpoint rule, and in Simpson‟s
method uses a weighted mean between them to get more accurate integral. There are two
kind of Simpson‟s methods such as 1/3rd rule and 3/8th rule.

Simpson‟s 1/3rd rule can be written as,

∫ [ ∫ ∫ ]

To further reduce the error in this method can be done by using Simpson‟s 3/8th rule. It
can written as,

Where step size h = xi+1 – xi and all xi s are equally spaced.

2.1.5 Monte Carlo method


Monte Carlo method is very useful method which is used to find integrals. This method is
mainly based on random sampling. The theory behind this method is counts the ratio
between the number of randomly generated points in inside of the boundary and the total
number of randomly generated points. If the selected boundary has a known area we can
find the unknown area by that calculated ratio.

9
Figure 2.5: Graphical representation of Monte Carlo method

2.2 Differentiating of a function


A differential equation is mathematical equation which describes the rate of changing.
They are widely used in many fields such as economics, social sciences, biology, business
etc. Often, systems described by differential equations are complex that a purely
analytical solution to the equation is not available. Therefore, numerical methods which
are used to solve linear differential equations become important.

2.2.1 Euler‟s method


Euler‟s method is used to find solution for given differential equations but it can be used
only for initial value problems.

Figure 2.6: Graphical Representation of Euler‟s method

Let the curve which is in the bottom be unknown. Now we had to find the equation of that
line. First select the point which lies on the curve which denoted by A0. Then from the
differential equation, the slope to the curve at A0 can be computed, and so, the tangent
line.

10
Take a small step along that tangent line up to a point A1. The slope does not change too
much because the step size is very small, so A1 will be close to the curve. If we assuming
that A1 is still on the curve. The same procedure can be used to A1. After of several steps,
a polygonal curve A0A1A2A3A4 has computed. In general, this curve does not diverge too
far from the original unknown curve because of usage of small step sizes. Error which is
occur in this method can be reduce using small step sizes.

n-1

2.2.3 Mid-point method

Figure 2.7: Graphical representation of Mid-point method

This method is most improved version of the Euler‟s


method and this method has very low error rate compared
to Euler‟s method. In this method find the function value
midway into the interval and then use the start point
function value and the midpoint function value to estimate
the slope.

2.2.2 Runge-Kutta method


This method is a higher order approximation to the midpoint rule. Therefore Runge-Kutta
method is more accurate method which can be used to solve ordinary differential
equations. In this method used weighted means of gradients between the interval to get
better value for the gradient.

11
In this method there is four iterations to find the final gradient. The first considers one
quarter of the interval in estimating the derivative, then moves to the midpoint, three
quarters and the full interval.

Figure 2.8 : Graphical Representation of Runge-Kutta method

12
3 METHODOLOGY

Experiment 1

The first experiment was to find the integral of the following polynomial using the
Simpson‟s rule by taking n=2

The program was coded in MATLAB and the result was compared with the exact value.

Then the following function was integrated using the Simpson‟s 1/3rd rule.N was
changed as factor of 2 for steps as 2,4,8,16,32 and 64. Error between the real value and
the MATLAB approximation was calculated for each value of N.

13
Experiment 2

The following data set was considered

Table 3.1:Data set of volume and pressure

Volume(m3) 5 10 15 20 25 30
Pressure(Pa) 0.3714 0.2264 0.1694 0.1380 0.1176 0.1033

The Matlab code was written to plot the PV curve for above data set.

14
Then a curve was fitted for the above data set using MATLAB curve fitting tool box.
Custom equation function available in curve fitting tool box was used to find a possible
relationship between pressure and volume.

Then work done by the volume change of 10𝑚3 to 15𝑚3 was found using integrating the
above discovered function with the help of Simpson‟s rule.

Experiment 3

The Matlab program was coded to find π by using Monte Carlo method. Two sets of
N=1000 random numbers from a uniform distribution between 0 and 1 were generated
using the MATLAB function „rand‟. A counter named „M‟ was initialized. Each pair

15
taken from the two sets was taken as coordinates of a point on a plane. Each of these
points lie inside the circle or outside the circle was checked. If that point lies inside the
circle, counter M was increased by 1.Above step was repeated for all 1000 points using a
for loop. Finally, ratio of M and N was calculated. Then the result of it was multiplied by
four to obtain a value for pi.

Experiment 4

=-xy; y(0)=1

A Matlab program was coded to solve the given initial value problem using Runge Kutta
4th order method by taking step size(h) as 0.2. Graph was plotted for the solution
obtained by the program. Then the exact solution was plotted on the same graph. Hold on
key word was used to tell MATLAB that plot both graphs in the same figure.

16
Then step size was changed to 0.02 and same graph was plotted. Both graphs were
compared by zooming into a same level.

17
Experiment 5

+ 2 +4y=0 y(0)=2, =0

The Matlab program was coded to solve above equation by using Euler method in range
0 .The function y(t)=2e-tcos(√ t) was plotted in the range 0 .This function
is the particular solution for the above differential equation was checked.

18
19
4 RESULT AND ANALYSIS

Experiment 1

The value of I using Simpson‟s rule = 61.6630

The exact value of I = 61.6630

The difference between the exact value and the answer obtained using Simpson‟s rule
calculation =0

∫ dx=I

Exact value=2.436564

Table 4.1: Error of integral VS Number of iterations

N 2 4 8 16 32 64
H 0.5 0.25 0.125 0.0625 0.0313 0.0156
Error 0.0012 7.4027 4.6524 2.9110 1.8124 1.0560
x10-5 x10-5 x10-5 x10-5 x10-5

Result 2.4377 2.4366 2.4366 2.4366 2.4366 2.4366

Experiment 2
There is a relationship between Pressure and the volume. Then we assume the
relationship be,

p = a vb
and respective a and b values are obtained by using curve fitting toolbox of MATLAB.

a = 1.173

b = -0.7143

Therefore the equation is, p = 1.173 v-0.7143


20
Figure 4.1:Graph of pressure vs the volume for given data

Figure 4.2:MATLAB curve fitting tool

 Total work done of the volume between 10m3 to 15m3 = 0.9735


21
Experiment 3
Ration of m and m (m/n) = 0.7830

Calculated pi ( ) value using Monte Carlo method = 3.3120

Experiment 4

Figure 4.3: Graph of solved initial value problem using the Runge Kutta method and real graph

22
Figure 4.4: Graph when step size decreased to 0.02

Experiment 5
Solution for the differential equation is = -0.4001

Figure 4.5: Graph of solved initial value problem and f(t)=2e^(-t) cos(√3 t) graph

23
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,
differentiating & integrating function. In this practical there were 5 experiments. The
methods were used to solve integration and differential equations. Trapezoidal rule,
Simpson‟s rule and Monte Carlo method were used for integration. Euler‟s method and
midpoint method were used to solve 1st order differential equations. Runge-Kutta method
was used to solve 2nd order differential equations

In the 1st experiment to find area under the curve Simpson‟s rule in Matlab was used. The
value of given integral using Simpson‟s rule and the exact value of the given integral is
same. There was no difference between exact value and Simpson‟s value. This shows
Simpson method will give accurate values for integrals. As the second part of experiment
1, same function was integrated with different n values. According to the values in this
table clearly observe that when increasing the n value by 1,2,4,8 the respective error value
decrease. After of n=8, the value of error is starts to increase by small values. The error of
the MATLAB approximation is usually around 10-5. When n is increasing, the integral
generated from MATLAB using Simpson‟s method become more accurate.

In the experiment 2 two sets of data were given for pressure and volume. According to
laws of gasses there is a relationship between volume and pressure. In this experiment the
relationship was found between volume and pressure using Matlab curve fitting toolbox,
the obtained equation was P=1.173V-0.7143. But the according to laws of gas the
relationship should be P=kV-1. These laws derived for ideal gasses. But the given data set
cannot be taken under ideal conditions. Therefore there can be some deviation of result
from law. Then the work done of the volume between (10 to 15)m3 was evaluated using
Simpson‟s rule in MATLAB.

In 3rd experiment Monte Carlo Method was used to find the value of pi. In this a square
enclosing a circle was taken and the MATLAB function “rand” was used to generate a set
of random numbers which returned a pxq matrix containing pseudo random values drawn
from a standard uniform distribution in the open interval 0 to 1. Then pi was calculated
using simple geomantic. Each time run the program MATLAB gives a different value for
the pi because each time MATLAB generates a different set of random numbers. To
increase the accuracy of the approximated value of pi, the number of random values can
be increased.

In the 4th experiment Runge-Kutta method was used to solve the given initial value
problem. According to experiment when step size decreases the curve which gives from
Matlab is close to real curve. When the step size is increased, the graph becomes more
curvy because there are more data points in the graph. When the two graphs were
compared it was clear that both the graphs were almost similar. There was a slight
deviation but this deviation was minimized when the value of h was reduced from 0.2 to
0.02.
24
In 5th experiment the Euler‟s method was used to solve a given differential equation and
its solution and the function itself were plotted in MALTAB Both graphs were same in
shape but did not overlap. Accuracy of the resultant solution could have been improved if
Runge Kutta Method had been used for this problem as well.

By completing those two set of practical successfully, through knowledge about how
those numerical methods can be applied for mathematical operation was obtained.

25
6 CONCLUSION
Simpson's 1/3rd rule can be used to integrate a function and Simpson's method is more
accurate than trapezoidal rule.

Monte Carlo method can also be used to evaluate constants associated with area values
such as π. MATLAB rand function was used to generate random values between 0 and 1.
Higher the number of random values generated, higher would be the accuracy of the final
result.

The smoothness of a graph of a solution of a function solved using Runge kutta‟s method,
can be improved by decreasing the step size. That means the graph simulated using runge
kutta‟s method in MATLAB becomes the same as the graph of the exact solution when
step size is decreased.

MATLAB curve fitting tool box can be used effectively to find the relationship between
two variables.

When using numerical methods, to increase the accuracy of the result, the step size (h)
should decrease.

26
7 REFERENCES
Calculuslab.deltacollege.edu. (2017). Numerical Methods--Runge-Kutta Method. [online]
Available at: http://calculuslab.deltacollege.edu/ODE/7-C-3/7-C-3-h.html [Accessed 04
May 2017].

Tutorial.math.lamar.edu. (2017). Differential Equations - Euler's Method. [online]


Available at: http://tutorial.math.lamar.edu/Classes/DE/EulersMethod.aspx [Accessed 04
May 2017].

Mathworld.wolfram.com. (2017). Simpson's Rule -- from Wolfram MathWorld. [online]


Available at: http://mathworld.wolfram.com/SimpsonsRule.html [Accessed 04 May
2017].

Mathworks.com. (2017). Monte Carlo Simulation - MATLAB & Simulink. [online]


Available at: https://www.mathworks.com/discovery/monte-carlo-simulation.html
[Accessed 04 May 2017].

Bourne, M. (2017). 5. Trapezoidal Rule. [online] Intmath.com. Available at:


http://www.intmath.com/integration/5-trapezoidal-rule.php [Accessed 04 May 2017].

27
APPENDIX – A

28

You might also like