MP Lap File
MP Lap File
University of Delhi
OUTPUT :
PROGRAMME—03
AIM : To approximate nth root of a number up to a given number
of significant digits by using secent method.
THEORY :
The code implements the Secant Method, which is a numerical
technique used to find the root of a function. It's an iterative
method that starts with two initial guesses and refines them
successively to converge towards the root.
ALGORITHM :
1. Start with two initial guesses, X0 and X1.
2. Iterate until convergence, where convergence is typically
determined by a tolerance level.
3. At each iteration, calculate the next approximation Xnext using
the Secant Method formula.
Xnext = Xcurr + (f(Xcurr).((xcurr + xprev) / f(xcurr) – f(xprev)).
4. Update xprev and xcurr for the next iteration.
5. Repeat steps 3-4 until the absolute difference between xcurr
and xprev is less than the specified tolerance.
FORMULA USE : The formula used in the Secant Method for finding
the next approximation Xnext is:
OUTPUT :
PROGRAMME—04
AIM : Determine the depth up to which a spherical homogeneous object of given
radius and density will sink into a fluid of given density by using bisection method.
THEORY : The bisection method is a numerical technique used to find the root
of a real-valued function. It is based on the intermediate value theorem, which
states that if a continuous function has different signs at two points within an
interval, then it must have at least one root within that interval.
ALGORITHM :
• INPUT PARAMETERS : The algorithm takes the following inputs:
• ‘a’ and ‘b’ : Initial approximations of the root where a < b .
• ‘n’ : Number of iterations.
• ‘object_density ’: Density of the object.
• ‘fluid_density’ : Density of the fluid.
• BRACKETING THE ROOT: Before applying the bisection method, we need
to ensure that the initial approximations a and b bracket the root, i.e., the
function changes sign between a and b. This is checked using the product of
the function values at a and b. If the product is positive, it implies the root is
not bracketed.
• BISECTION ITERATION :
• Calculate the midpoint ‘x’ of the interval ‘[a ,b]’.
• Evaluate the function value ‘depth_of_immersion(x , object_density ,
fluid_density)’.
• Update ‘a’ or ‘b’ based on the sign of the function value at ‘x’.
• If the function value is negative , update ‘a’ to ‘x’ otherwise , update ‘b’
to ‘x’.
• Repeat this process for ‘n’ iterations.
FORMULA USE :
The formula used in the ‘depth_of_immersion’ function is :
FORMULA USE:
The secant formula used in the algorithm to compute the next
approximation ‘x_n’ is:
Xn = X1 - f(X1) x ( X1 – X0)/f(X1) –f(X0)
Where :
10. Xn is the next approximation.
11. X0 and X1 are the current approximations.
12. F(X0) and F(X1) are the function values at X0 and X1 , respectively.
PYTHON CODE :
OUTPUT :
PROGRAMME—06
AIM : Solve transeendental equation like α = tan(α) using secant
method.
THEORY: The code implements the Secant method which is an
iterative root-finding algorithm used to find the roots of a given
equation. It's an improvement over the simpler method of the
Newton-Raphson algorithm, as it doesn't require the calculation
of the derivative of the function.
ALGORITHM :
• INITIALIZATION:
• The algorithm start with two initial guesses , X0 and X1 ,
which should ideally bracket the root.
• ITERATION :
• It iteratively updates the value of X0 and X1 using the
formula derived from the secant line between the points
(X0 , F(X0)) and (X1 , f(X1)).
• The formula used for updating x1 is:
Xnext = X1 – f(X1) x (x1 –x0)/f(x1) – f(x0)
13. TERMINATION :
14. The iteration stop when either the absolute
difference between consecutive iterations becomes
smaller than a predefined tolerance or when a maximum
number of iterations is reached.
15. OUTPUT:
16. The algorithm return the approximate root if found
within the specified tolerance and maximum iterations.
FORMULA USE :
Xnext = X1 – f(X1) x (x1 –x0)/f(x1) – f(x0)
PYTHON CODE :
OUTPUT :
PROGRAMME—07
AIM : Solve transeendental equation like α = tan(α) by using newton
raphson method
THEORY : The Newton-Raphson method is an iterative numerical
technique used to find the root of a real-valued function. It is based on
the concept of using successive approximations to iteratively converge
towards the root of the function. The method is particularly effective for
functions that are differentiable and have a continuous first derivative.
ALGORITHM :
• INPUT PARAMETER : The algorithm takes the following inputs:
• Initial_guess : An initial approximation of the root .
• Tolerance : The desired accuracy of the root approximation.
• Max_iterations : Maximum number of iterations allowed.
• NEWTON-REPHSON ITERATION :
• Initialze ‘x’ with the initial guess .
• For each iteration:
• Calculate the function value ‘f(x)’ using the given equation.
• Calculate the derivative ‘f’(x)’ using the provided derivative
function.
• Update ‘x’ using the Newton-rephson formula.
• Check if the absolute difference between consecutive
approximations is less than the specified tolerance.if so ,
return the current approximation as the root.
• If the root is not found within within the maximum number of
iterations , return ‘None’.
OUTPUT : If the root is found within the specified tolerance and maximum
iterations, the algorithm outputs the approximated root of the equation.
Otherwise, it indicates that the method did not converge within the given
constraints.
FORMULA USED :
The Newton-Rephson formula used in the algorithm to compute the
next approximation Xn+1 is :
Xn+1 = Xn – (f(Xn)/f’(Xn))
Where :
• Xn+1 is the next approximation.
• Xn is the current approximation.
• F(Xn) is the function value at Xn.
• F’(Xn) is the derivative of the function at Xn .
PYTHON CODE :
OUTPUT :
UNIT—02
LEAST SQUARE FITTING
PROGRAMME—08
AIM: Make a function for least square fitting, use it for fitting
given data (x, y) and estimate the
parameters a, b as well as uncertainties in the parameters for the
following cases.
i. Linear (𝑦=ax+b)
The least-square method states that the curve that best fits a
given set of observations, is said to be a curve having a minimum
sum of the squared residuals (or deviations or errors) from the
given data points. Let us assume that the given points of data are
(x1, y1), (x2, y2), (x3, y3), …, (xn, yn) in which all x’s are independent
variables, while all y’s are dependent ones. Also, suppose that f(x)
is the fitting curve and d represents error or deviation from each
given point.
Now, we can write:
d1 = y1 − f(x1)
d2 = y2 − f(x2)
d3 = y3 − f(x3)
…..
dn = yn – f(xn)
The least-squares explain that the curve that best fits is
represented by the property that the sum of squares of all the
deviations from given values must be minimum, i.e:
INPUT:-
GRAPH:_
RESULT:
The Python program fits the given data (x, y) and estimate the
parameters a, b as well as uncertainties in the parameters for
linear (𝑦= 𝑎x+b).
PROGRAMME—09
AIM: Make a function for least square fitting, use it for fitting
given data (x, y) and estimate the
parameters a, b as well as uncertainties in the parameters for the
following cases.
ii. Power law (𝑦 = 𝑎xb)
The least-square method states that the curve that best fits a
given set of observations, is said to be a curve having a minimum
sum of the squared residuals (or deviations or errors) from the
given data points. Let us assume that the given points of data are
(x1, y1), (x2, y2), (x3, y3), …, (xn, yn) in which all x’s are independent
variables, while all y’s are dependent ones. Also, suppose that f(x)
is the fitting curve and d represents error or deviation from each
given point.
Now, we can write:
d1 = y1 − f(x1)
d2 = y2 − f(x2)
d3 = y3 − f(x3)
…..
dn = yn – f(xn)
The least-squares explain that the curve that best fits is
represented by the property that the sum of squares of all the
deviations from given values must be minimum, i.e:
where and
A=10^a
ALGORITHM:
Step 1: Start
Step 2: Import necessary modules:
numpy as np
array as arr
matplotlib.pyplot as plt
Step 3: Define the function 'fit(x, y)' for fitting the data:
Initialize empty lists X and Y of size n.
Convert each element of x and y to base-10 logarithm and
store in X and Y respectively.
Initialize variables xy, sumx, sumy, sqx to 0.
Calculate the sums and products required for the formula.
Calculate slope 'a1' and y-intercept 'a0' using the formula.
Calculate 'a' (y-intercept) and 'b' (slope).
Use numpy's polyfit to find the slope and y-intercept.
Get minimum and maximum values of x.
Create arrays xx and yy for plotting.
Generate 1000 values between the minimum and maximum
of x and calculate y values for plotting.
Plot the data points and the best fit curve using matplotlib.
The least-square method states that the curve that best fits a
given set of observations, is said to be a curve having a minimum
sum of the squared residuals (or deviations or errors) from the
given data points. Let us assume that the given points of data are
(x1, y1), (x2, y2), (x3, y3), …, (xn, yn) in which all x’s are independent
variables, while all y’s are dependent ones. Also, suppose that f(x)
is the fitting curve and d represents error or deviation from each
given point.
Now, we can write:
d1 = y1 − f(x1)
d2 = y2 − f(x2)
d3 = y3 − f(x3)
…..
dn = yn – f(xn)
The least-squares explain that the curve that best fits is
represented by the property that the sum of squares of all the
deviations from given values must be minimum, i.e:
(3)
(4)
where and .
(5)
Applying Least Squares Fitting gives
(6)
(7)
(8)
(9)
ALGORITHM:
Step 1: Start
Step 3: Define the function 'fit(x, y)' for fitting the data:
Initialize an empty list Y of size n.
Take the natural logarithm of each element of y and store it
in Y.
Initialize variables xy, sumx, sumy, sqx to 0.
Calculate the sums and products required for the formula.
Calculate slope 'a1' and y-intercept 'a0' using the formula.
Calculate 'a' (y-intercept) and 'b' (slope).
Use numpy's polyfit to find the slope and y-intercept.
Get minimum and maximum values of x.
Create arrays xx and yy for plotting.
Generate 1000 values between the minimum and maximum
of x and calculate y values for plotting.
Plot the data points and the best fit curve using matplotlib.
Step 4: Initialize empty arrays x and y.
Step 5: Input the number of elements 'n'.
Step 6: Print "Input the data values of x and y".
Step 7: Iterate 'n' times:
Read input values for x and y, convert to float, and append
to arrays x and y respectively.
Step 9: Stop
CODE:
INPUTS:-
GRAPH:-
RESULT:
The Python program fits the given data (x, y) and estimate the
parameters a, b as well as uncertainties in the parameters for
exponential law (𝑦 = 𝑎ebx).
UNIT 3
Numerical solution of ODE
PROGRAMME—11
AIM– Solve given first order differential equation numerically
using Euler, RK2, RK4 methods and apply to the following physics
problem:
i) Radioactive decay
ii)Newton’s law of cooling
THEORY—
Euler Method
In Euler’s method, you can approximate the curve of the solution
by the tangent in each interval (that is, by a sequence of short line
segments), at steps of h.In general, if you use small step sizes, the
accuracy of approximation increases.
The R-K4 method is the most frequently used R-K method for
solving differential equations.
The Runge-Kutta method estimates y for a given position x.
Formula–
INPUT CODE-
Output–
ii) Radioactive decay by RK2
INPUT CODE–
OUTPUT–
iii) Radioactive decay by RK4
INPUT CODE-
OUTPUT –
Newton’s law of cooling
i) Newton’s law of cooling by Euler method-
INPUT CODE-
OUTPUT–