MATHS Record
MATHS Record
TECHNOLOGY
2
MAHARAJA AGRASEN INSTITUTE OF
TECHNOLOGY
3
59
60
Curve fitting
1. Start
5. Solve the system of equations given by (1) and (2) using matrix
method, where
C = A− 1 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)
62
Fitting a Parabola
1. Start
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)
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.
1. Start
3. Input a and b where a, b are end points of interval (a, b) in which the
root lies.
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
10. If |a − b| > 2e, print the root and exit otherwise continue in the loop
11. Stop
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
)0 f
and nth approximation x1 is given by xn+1 = xn −(f xn)
‘(xn)
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
1. Start
82
6. sum1 = y(1) + y(n) and sum2 = y(2) + . . . + y(n − 1)
9. Stop
83
84
Scilab code for Trapezoidal Rule
85
Simpson’s 1/3rd Rule
1. Start
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
87
Scilab Code for Simpsons 1/3rd Rule
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)]
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) + ])
10. val = 3h
8
(sum)
12. Stop
91
Scilab Code for Simpson’s 3/8th Rule
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.
1. Start
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.
7. Stop
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
1. Start
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
99
7. Stop
100
Scilab Code for Runge Kutta Method
x1 = 0;
y1 = 1;
101
102