0% found this document useful (0 votes)
41 views45 pages

MATHS Record

The document provides information on curve fitting and numerical integration techniques. It includes: 1) Methods for fitting straight lines and parabolas to datasets using linear regression. It gives the equations and algorithms for determining the line of best fit for straight lines and parabolas. 2) Descriptions of numerical integration techniques including the trapezoidal rule and Simpson's 1/3rd rule. It provides the algorithms and shows how to compute integrals using these methods by approximating the area under a curve as a sum of geometric objects. 3) Examples of the bisection method and Newton-Raphson method for finding roots of equations. It gives the iterative formulas and algorithms for these root-finding techniques. Flow

Uploaded by

chandu93152049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views45 pages

MATHS Record

The document provides information on curve fitting and numerical integration techniques. It includes: 1) Methods for fitting straight lines and parabolas to datasets using linear regression. It gives the equations and algorithms for determining the line of best fit for straight lines and parabolas. 2) Descriptions of numerical integration techniques including the trapezoidal rule and Simpson's 1/3rd rule. It provides the algorithms and shows how to compute integrals using these methods by approximating the area under a curve as a sum of geometric objects. 3) Examples of the bisection method and Newton-Raphson method for finding roots of equations. It gives the iterative formulas and algorithms for these root-finding techniques. Flow

Uploaded by

chandu93152049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

MAHARAJA AGRASEN INSTITUTE OF

TECHNOLOGY

2
MAHARAJA AGRASEN INSTITUTE OF
TECHNOLOGY

3
59
60
Curve fitting

Fitting a Straight Line


Let y = ax + b be the straight line to be fitted to the given set of data
points (x1, y1) , (x2, y2),· · · , (xn, yn), then normal equations are:
Σ
y = a x + nb · · · (1)
Σ
Σ xy = a Σ Σ
x2 + b x · · · (2)

Algorithm to fit a straight line to given set of data points:

1. Start

2. Find number of pairs of data points (n) to be fitted

3. Input the x values and y values.


Σ Σ Σ Σ
4. Find x, y, x2 and xy

5. Solve the system of equations given by (1) and (2) using matrix
method, where

C = A− 1 B

6. Required line of best fit is y = ax + b

7. Stop

60
Flow Chart for fitting a straight line to given set of data points

61
Scilab Code for fitting a straight line to given set of data points (x,y)

n =input(’Enter the no. of pairs of values (x,y):’)


disp(’Enter the values of x:’)
for i=1:n
x(i)=input(’ ’)
end
disp(’Enter the corresponding values of
y:’) for i=1:n
y(i)=input(’ ’)
end
sumx=0; sumx2=0; sumy=0;
sumxy=0 for i=1:n
sumx=sumx+x(i);
sumx2=sumx2+x(i)*x(i
); sumy=sumy+y(i);
sumxy=sumxy+x(i)*y(i);
end
A=[sumx n; sumx2 sumx];
B=[sumy;sumxy];
C=inv(A)*B
printf(’The line of best fit is y =(%g)x+(%g)’,C(1,1),C(2,1))

62
Fitting a Parabola

Let y = ax2 + bx + c be the parabola to be fitted to the given set of data


points (x1, y1) , (x2, y2),· · · , (xn, yn).

Algorithm to fit a parabola to given set of data points

1. Start

2. Find number of pairs of data points (n) to be fitted

3. Input the x values and y values.


Σ Σ Σ Σ Σ Σ Σ
4. Find x, y, x2, x3, x4, xy and x2y

5.Solve the system of equations given by (1), (2) and (3) using matrix method,
where

C = A− 1 B
b
6.Required parabola to be fitted is y = ax2 + bx + c

7.Stop

63
Flow Chart for fitting a parabola to the given set of data points

64
Scilab Code for fitting a parabola line to given set of data points (x,y)

n =input(’Enter the no. of pairs of values (x,y):’)


disp(’Enter the values of x:’)
for i=1:n
x(i)=input(’ ’)
end
disp(’Enter the corresponding values of
y:’) for i=1:n
y(i)=input(’ ’)
end
sumx=0; sumx2=0; sumx3=0; sumx4=0; sumy=0; sumxy=0;
sumx2y=0; for i=1:n
sumx=sumx+x(i);
sumx2=sumx2+x(i)*x(i);
sumx3=sumx3+x(i)*x(i)*x(i);
sumx4=sumx4+x(i)*x(i)*x(i)*x(i)
; sumy=sumy+y(i);
sumxy=sumxy+x(i)*y(i);
sumx2y=sumx2y+x(i)*x(i)*y(i);
end
A=[sumx2 sumx n; sumx3 sumx2 sumx; sumx4 sumx3 sumx2];
B=[sumy;sumxy;sumx2y];
C=inv(A)*B
printf(’The fitted parabola is y=(%g)xˆ 2+(%g)x+(%g)’,C(1,1),C(2,1),C(3,1))

65
66
67
Bisection Method
Bisection method is used to find an approximate root in an interval by
repeat- edly bisecting into subintervals. It is a very simple and robust
method but it is also relatively slow. Because of this it is often used to
obtain a rough approximation to a solution which is then used as a starting
point for more rapidly converging methods. The method is also called the
interval halving method, binary search method or the dichotomy method.
This scheme is based on the intermediate value theorem for continuous
functions.
Let f (x) be a function which is continuous in the interval (a, b). Let f (a)
be positive and f (b) be negative. The initial approximation2 is x1 = a +b

Then to find the next approximation to the root, one of the three conditions arises:
1. f (x1) = 0, then we have a root at x1.
2. f (x1) < 0,then since f (x1)f (a) < 0,the root lies between x1 and a.
3. f (x1) > 0, then since f (x1)f (b) < 0,the root lies between x1 and b.
We can further divide this subinterval into two halves to get a new
subinterval which contains the root. We can continue the process till we get
desired accuracy.

Bisection Method Algorithm:

1. Start

2. Read y = f (x) whose root is to be computed.

3. Input a and b where a, b are end points of interval (a, b) in which the
root lies.

4. Compute f (a) and f (b).

5. If f (a) and f (b) have same signs, then display function must have
different signs at a and b, exit. Otherwise go to next step.

6. Input e and set iteration number counter to zero. Here e is the absolute
error i.e. the desired degree of accuracy.

7. root = (a + b)/2

74
8. If f (root) ∗ f (a) > 0, then a = root else b = root

9. Print root and iteration number

10. If |a − b| > 2e, print the root and exit otherwise continue in the loop

11. Stop

Flow Chart for Bisection Method

75
Scilab Code for Bisection method

deff(J y = f (x)J ,J y = x3 + x2 − 3 ∗ x −
3J ) a =input(”enter initial interval
value: ”); b =input(”enter final
interval value: ”);
//compute initial values of f (a) and f (b)
fa = f (a);
f b = f (b);
if sign(f a) == sign(f b)
// sanity check: f (a) and f (b) must have different
signs disp(’f must have different signs at the
endpoints a and b’) error
end
e=input(” answer correct upto : ”);
iter=0;
printf(’Iteration \t a \t\t b \t \t root \t \t f (root)\
n’) while abs(a − b) > 2 ∗ e
root = (a + b)/2
printf(’ %i\t\t %f \t %f \t %f \t %f \n’ ,iter,a,b,root,f
(root)) iff (root) ∗ f (a) > 0
a = root
else
b = root
end
iter=iter+
1 end
printf(’\n \n The solution of given equation is %f after %i Iterations’,root,iter−
1)

76
Newton-Raphson Method
Newton -Raphson method named after Isaac Newton and Joseph
Raphson, is a method for finding successively better approximations to the
roots of a real-valued function. The Newton-Raphson method in one

variable is implemented as follows:

Let x0 be an approximate root of the equation f (x) = 0. Next approximation x1


f (x0)
is given by x1 = x0 −
f (x
J

)0 f
and nth approximation x1 is given by xn+1 = xn −(f xn)
‘(xn)

Newton Raphson Method Algorithm:


1. Start

2. Enter the function f (x) and its first derivative f (x)

3. Take an initial guess root say x1 and error precision e.

4. Use Newtons iteration formula to get new better approximate of the


root, say x2. Repeat the process for x3, x4 .....till the actual root of the
function is obtained, fulfilling the tolerance of error.

77
78
Scilab code for Newton Raphson Method

deff(’y = f (x)’,’y = x3 + x2 − 3 ∗ x −
3’) deff(’y = df (x)’,’y = 3 ∗ x2 + 2 ∗ x
− 3’) x(1)=input(’Enter Initial
Guess:’);
e= input(” answer correct upto : ”);
for i = 1 : 100
x(i + 1) = x(i) − f (x(i))/df (x(i));
err(i) = abs((x(i + 1) −
x(i))/x(i)); if err(i) < e
break
; end
end
printf(’the solution is %f’,x(i))

79
80
81
The Trapezoidal Rule

Numerical Integration by Trapezoidal Rule Algorithm:

1. Start

2. Define and Declare function y = f (x) whose integral is to be computed.


∫b
3. Input a, b and n, where a, b are lower and upper limits of f
a (x)dx
integral
and n is number of trapezoids in which area is to be divided.
4. Initialize two counters sum1 and sum2 to zero.

5. Compute x(i) = a + i ∗ h and y(i) = f (x(i)) in a loop for all n + 1


points dividing n trapezoids.

82
6. sum1 = y(1) + y(n) and sum2 = y(2) + . . . + y(n − 1)

7. val = h2(sum1 + 2sum2)

8. Print value of integral.

9. Stop

Flow Chart for Trapezoidal Rule

83
84
Scilab code for Trapezoidal Rule

deff(’y = f (x)J ,J y = x/(x2 + 5)’);


a=input(”Enter Lower Limit: ”)
b=input(”Enter Upper Limit: ”)
n=input(”Enter number of sum intervals:
”) h = (b − a)/n
sum1 = 0
sum2 = 0
for i = 0 :
nx= a+ i
∗ hy= f
(x)
disp([xy])
if (i == 0)|(i ==
n) sum1 = sum1 +
y else
sum2 = sum2 + y
en
d
en
d
val = (h/2) ∗ (sum1 + 2 ∗ sum2)
disp(val,”Value of integral by Trapezoidal Rule is:”)

85
Simpson’s 1/3rd Rule

The Simpson’s 1/3rd rule is similar to the trapezoidal rule, though it


approxi- mates the area using a series of quadratic functions instead of
straight lines. It is used if the number of segments is even.

Numerical Integration by Simpson’s 1/3rd Rule Algorithm:

1. Start

2. Define and Declare function y = f (x) whose integral is to be computed.


∫b
3. Input a, b and n, where a, b are lower and upper limits of f
integral a (x)dx
and n is number of intervals in which area is to be divided. Note that n
must be even.

4. Put x1 = a and initialize sum = f (a)

5. Compute x(i) = x(i − 1) + h

86
6. sum = sum + 4[f (x2) + f (x4) + + f (xn))
7. sum = sum + 2[f (x3) + f (x5) + + f (xn−1))
8. sum = sum + f (b)
9. val = 3h(sum)
10. Print value of integral.
11. Stop

Flow Chart for Simpson’s 1/3rd Rule

87
Scilab Code for Simpsons 1/3rd Rule

deff(’y = f (x)J ,J y = x/(x2 + 5)’);


a=input(”Enter Lower Limit: ”)
b=input(”Enter Upper Limit: ”)
n=input(”Enter number of sum intervals:
”) h = (b − a)/n
x(1) = a;
sum = f (a);
for i = 2 : n
x(i) = x(i − 1) + h;
end
for j = 2 : 2 : n
sum = sum + 4 ∗ f (x(j));
end
for k = 3 : 2 : n
sum = sum + 2 ∗ f (x(k)); end
sum = sum + f (b);
val = sum ∗ h/3;
disp(val,”Value of integral by Simpsons 1/3rd Rule is:”)

88
Simpson’s 3/8th Rule
The Simpson’s 3/8th Rule rule is similar to the 1/3 rule. It is used when
it is required to take 3 segments at a time. Thus number of intervals must
be a multiple of 3.
∫b
f (x)dx 3h[f (x0) + 3f (x1) + 3f (x2) + 2f (x3) + 3f (x4) + 3f (x5) + 2f (x6)
= + ...+
a

89
8

f (xn)]

Numerical Integration by Simpson’s 3/8th Rule Algorithm:

1. Start
2. Define and Declare function y = f (x) whose integral is to be computed.
∫b
3. Input a, b and n, where a, b are lower and upper limits of f
integral a (x)dx
and n is number of intervals in which area is to be divided. Note that n
must be a multiple of 3.
4. Put x1 = a and initialize sum = f (a)
5. Compute x(i) = x(i − 1) + h
6. sum = sum + 3[f (x2) + f (x5) + ]

90
7. sum = sum + 3[f (x3) + f (x6) + ])

8. sum = sum + 2[f (x4) + f (x7) + ]

9. sum = sum + f (b)

10. val = 3h
8
(sum)

11. Print value of integral.

12. Stop

Flow Chart for Simpson’s 3/8th Rule

91
Scilab Code for Simpson’s 3/8th Rule

deff(’y = f (x)J ,J y = x/(x2 + 5)’);


a=input(”Enter Lower Limit: ”)
b=input(”Enter Upper Limit: ”)
n=input(”Enter number of sum intervals:
”) h = (b − a)/n
x(1) = a;
sum = f (a);
for i = 2 : n
x(i) = x(i − 1) +
h; end
for j = 2 : 3 : n
sum = sum + 3 ∗ f
(x(j)); end
for k = 3 : 3 : n
sum = sum + 3 ∗ f
(x(k)); end
for l = 4 : 3 : n
sum = sum + 2 ∗ f
(x(l)); end
sum = sum + f (b);
val = sum ∗ 3 ∗ h/8;
disp(val,”Value of integral by Simpson’s 3/8th Rule is:”)

92
93
92
Euler’s Method
Euler’s Method provides us with an approximation for the solution of a
dif- ferential equation of the dxform dy = f (x, y) , y(x0) = y0. The idea behind
Euler’s Method is to use the concept of local linearity to join multiple small
line segments so that they make up an approximation of the actual curve,
as shown below.

The upper curve shows the actual graph of a function. The A˜ 0 A5


curve
give approximations using Euler’s method. Generally, the approximation
gets less accurate the further we go away from the initial value. Better
accuracy is achieved when the points in the approximation are chosen in
small steps . Each next approximation is estimated from previous by
using the formula yn+1 = yn + hf (xn, yn)
Algorithm to solve an initial value problem using Euler’s method:

1. Start

2. Define and Declare function ydot representing


dx
dy
= f (x, y)

93
3. Input x0, y0 , h and xn for the given initial value condition y(x0) = y0. h is

94
step size and xn is final value of x.

4. For x = x0 to xn solve the D.E. in steps of h using inbuilt function ode.

5. Print values of x0 to xn and y0 to yn

6. Plot graph in (x, y)

7. Stop

Flow Chart for Euler’s Method

95
Scilab Code for Solving Initial Value Problem using Euler’s Method
// Solution of Initial value problem
dx
dy
= 2 − 2y − e−4x, y(0) = 1, h = .1, xn =
1
// If f is a Scilab function, its calling sequence must be ydot = f (x, y)
// ydot is used for first order derivativedxdy
// ode is scilab inbuilt function to evaluate the D.E. in the given
format
Function ydot = euler(x, y)
ydot= 2 − 2 ∗ y − %e∧(−4
∗ x) endfunction
x0=input(”Enter initial value x0:
”) y0=input(”Enter initial value
y0: ”) h=input(”Enter step size
h: ”) xn=input(”Enter final value
xn: ”) x = x0 : h : xn;
y=ode(y0, x0, x,
euler) disp(x,” x
value:”)
disp(y,” y value:”)
plot (x, y)

96
97
97
Runge-Kutta method of 4th Order

Eulers method is simple but not an appropriate method for integrating


an ODE as the derivative at the starting point of each interval is
extrapolated to find the next function value. The method has first-order
accuracy.
In Fourth-order Runge-Kutta method, at each step the derivative is
evaluated four times; once at the initial point; twice at trial midpoints; and
once at a trial endpoint. The final function value is calculated from these
derivatives as given below.

Algorithm for solving initial value problem using RUNGE KUTTA


METHOD :

1. Start

2. Define and Declare function ydot representing


dx
dy
= f (x, y)
98
3. Initialize values of x and y and input h, the step size.

4. Calculate 2 sets (i= 1,2) of values for k1, k2, k3 and k4 and subsequently
value of k = 61 [k1 + 2k2 + 2k3 + k4].

5. Finally yi+1 = yi + k

6. Print values of xi and yi.

99
7. Stop

Flow Chart for Runge Kutta Method

100
Scilab Code for Runge Kutta Method

function ydot = f (x,


y) ydot = x + y∧2
endfunction

x1 = 0;
y1 = 1;

h=input(”Enter step size h: ”)


x(1) = x1;
y(1) = y1;
for i = 1 : 2
k1 = h ∗ f (x(i), y(i));
k2 = h ∗ f (x(i) + 0.5 ∗ h, y(i) + 0.5 ∗ k1);
k3 = h ∗ f ((x(i) + 0.5 ∗ h), (y(i) + 0.5 ∗ k2));
k4 = h ∗ f ((x(i) + h), (y(i) + k3));
k = (1/6) ∗ (k1 + 2 ∗ k2 + 2 ∗ k3
+ k4); y(i + 1) = y(i) + k;
printf(’\n The value of y at x=%f is %f ’,i ∗ h, y(i + 1))
x(i + 1) = x(1) + i ∗
h; end

101
102

You might also like