Common Program
Common Program
(i) Develop a Python (3) program to obtain a root of the equation 𝒙𝟑 − 𝟐𝒙 − 𝟓 = 𝟎 ,between
2 and 3 by regula-falsi method.
Algorithm:
Program:
Output:
Output:
Statement: - 1 (B):
1. Import Libraries: Import necessary functions from sympy for symbolic computations.
2. Define Variables: Define the symbolic variable x to represent the independent variable in the function.
3. Input Function: Prompt the user to enter a function g as a string.
4. Convert Function to Callable: Convert the string function g into a callable function f using lambdify.
5. Compute Derivative: Calculate the derivative dg of g with respect to x.
6. Convert Derivative to Callable: Convert the derivative dg into a callable function df using lambdify.
B.L.D.E.A's V.P. Dr.P.G.Halakatti College of Engineering and Technology Page | 2
7. Initial Approximation: Prompt the user to enter an initial approximation x0 for the root.
8. Number of Iterations: Prompt the user to enter the number of iterations n.
9. Newton-Raphson Iterations:
• For each iteration:
• Compute the next approximation x1 using: 𝑥1 = 𝑥0 − 𝑓(𝑥0)/𝑓′(𝑥0)
• Update x0 to x1.
• Print the iteration number, the root approximation x1, and the function value f(x1).
Program:
Output:
(ii) Develop a python (3) program to find a root of the equation 𝟑𝒙 = 𝒄𝒐𝒔(𝒙) + 𝟏
near 1, by Newton Raphson method
Program:
Output:
Algorithm:
1. User inputs the number of data points (n) and then enters the values for x and y (data points).
2. The code constructs the forward difference table using the entered data points.
3. The algorithm computes the Newton's divided difference coefficients to construct the interpolating
polynomial.
4. It simplifies the polynomial using SymPy.
5. The code then asks the user to enter a value a at which they want to approximate the function's value.
6. Finally, it uses the interpolating polynomial to calculate and print the approximate value of the function at
point a
Program:
Output:
Statement: - 2 (B): Develop a python (2) program to Use Newtons backward interpolation to obtain the
interpolating polynomial and hence calculate 𝒚(𝟖) for the following data:
Algorithm:
Algorithm:
Program:
def my_func ( x ) :
return 1 / ( 1 + x ** 2 )
def trapezoidal ( x0 , xn , n ) :
h = ( xn - x0 ) / n
integration = my_func ( x0 ) + my_func ( xn )
for i in range (1 , n ) :
k = x0 + i * h
integration = integration + 2 * my_func ( k )
integration = integration * h / 2
return integration
Output:
Algorithm:
Program:
def my_func ( x ) :
return 1 / ( 1 + x ** 2 )
def simpson13 ( x0 , xn , n ) :
h = ( xn - x0 ) / n
integration = ( my_func ( x0 ) + my_func ( xn ) )
k = x0
for i in range (1 , n ) :
if i % 2 == 0 :
integration = integration + 2 * my_func ( k )
else :
integration = integration + 4 * my_func ( k )
k += h
integration = integration * h * ( 1 / 3 )
return integration
Output:
Algorithm:
1. The function my_func(x) is defined, representing the integrand. In this case, the function is 3*x**2.
2. The simpson38 function is defined to perform the numerical integration using Simpson's 3/8 rule. It takes the
parameters x0 (lower limit), xn (upper limit), and n (number of sub-intervals) as input.
3. The variable h is calculated, representing the step size for each sub-interval.
4. The integration sum is initialized with the values of the function at the lower and upper limits (x0 and xn).
5. The function values at intermediate points within each sub-interval are then summed up according to the
Simpson's 3/8 rule. If the index i is divisible by 3, the function value is multiplied by 2; otherwise, it is
multiplied by 3.
6. The final integration result is calculated by multiplying the summed values by (3h/8).
7. The user is prompted to enter the lower limit, upper limit, and the number of sub-intervals.
8. The simpson38 function is called with the provided input, and the integration result is printed.
Program:
def my_func ( x ) :
return 3*x**2
def simpson38 ( x0 , xn , n ) :
h = ( xn - x0 ) / n
integration = ( my_func ( x0 ) + my_func ( xn ) )
for i in range (1 , n ) :
k=x0+i*h
if i % 3 == 0 :
integration = integration + 2 * my_func ( k )
else :
integration = integration + 3 * my_func ( k )
integration = integration * h * ( 3 / 8 )
return integration
Output:
Algorithm:
1. The taylor function is defined to perform the Taylor series method for numerical integration of the system of
ODEs.
2. The deriv function is defined, representing the system of first-order ODEs that need to be solved. In this case,
it is a system of four ODEs.
3. The taylor function takes the following parameters:
• deriv: A function that returns the derivatives of the system of ODEs.
• x: The initial value of the independent variable.
• y: An array containing the initial values of the dependent variables.
• xStop: The stopping value of the independent variable.
• h: The step size for the numerical integration.
4. The function uses a loop to iteratively compute the approximate values of the dependent variables (y) at
different points in the interval (x to xStop) using the Taylor series expansion.
5. The computed values of the independent variable (x) and the corresponding values of the dependent
variables (y) are stored in arrays X and Y, respectively.
6. The initial values and the first four approximated values of x and y are printed.
Program:
Output:
Statement: - 4 (B): Develop a Python (2) program to Solve 𝒚′ = −𝒌𝒚 with 𝒚(𝟎) = 𝟏𝟎𝟎 using modified
Euler’s method at 𝒙 = 𝟏𝟎𝟎, by taking 𝒉 = 𝟐𝟓 .
Algorithm:
1. The modified_euler function is defined to perform the Modified Euler method for numerical integration of
the ODE.
2. The f function is defined, representing the ODE itself. In this case, it is a simple first-order ODE with the
function f(x, y) = -0.01*y.
3. The modified_euler function takes the following parameters:
• f: The function representing the ODE.
• x0: The initial value of the independent variable.
• y0: The initial value of the dependent variable.
• h: The step size for the numerical integration.
• n: The number of steps to be performed.
4. The function uses a loop to iteratively compute the approximate values of x and y using the Modified Euler
method.
5. The computed values of x and y at the n-th step are stored in arrays x and y, respectively.
6. The final values of x and y are printed.
Program:
import numpy as np
def modified_euler (f , x0 , y0 , h , n ) :
x = np . zeros ( n + 1 )
y = np . zeros ( n + 1 )
x [ 0 ] = x0
y [ 0 ] = y0
for i in range ( n ) :
x[i+1] = x[i] + h
k1 = h * f ( x [ i ] , y [ i ] )
k2 = h * f ( x [ i + 1 ] , y [ i ] + k1 )
y [ i + 1 ] = y [ i ] + 0.5 * ( k1 + k2 )
Output:
Algorithm:
Output:
Algorithm:
1. The initial values of x and y are given as x0 and y0, respectively. The values of y at three additional points are
also provided as y1, y2, and y3.
2. The step size h is defined.
3. The function f(x, y) representing the ODE is defined. In this case, the ODE is dy/dx = x^2 + (y / 2).
4. The value of y at intermediate points (y10, y11, y12, y13) is calculated using the given initial values.
5. The predicted value of y at x4 is approximated using the fourth-order Runge-Kutta method and stored in y4p.
6. The corrected value of y at x4 is then calculated using a correction step. The correction is performed
iteratively three times. In each iteration, the corrected value y4c is calculated based on the previously
corrected value or the predicted value y4p. The value of y14 is updated using the corrected value after each
iteration.
7. After each iteration, the corrected value of y at x4 is printed.
Program:
x0 = 1
y0 = 2
y1 = 2.2156
y2 = 2.4649
y3 = 2.7514
h=0.1
x1 = x0 + h
x2 = x1 + h
x3 = x2 + h
x4 = x3 + h
def f (x , y ) :
return x ** 2 + ( y / 2 )
y10 = f ( x0 , y0 )
y11 = f ( x1 , y1 )
y12 = f ( x2 , y2 )
y13 = f ( x3 , y3 )
y4p = y0 + ( 4 * h / 3 ) * ( 2 * y11 - y12 + 2 * y13 )
print ( ' predicted value of y4 is % 3.3f '% y4p )
y14 = f ( x4 , y4p )
for i in range (1 , 4 ) :
y4c = y2 + ( h / 3 ) * ( y14 + 4 * y13 + y12 )
print ( ' corrected value of y4 after iteration %d is % 3.5f '%(i ,
y4c ) )
y14 = f ( x4 , y4c )
Output: