MathLabReport (02210070)
MathLabReport (02210070)
1. Aim/Objective
2. Factorial Definition
The factorial of a non-negative integer n, denoted by n!, is the product of all positive
integers less than or equal to n. Mathematically, it is expressed as:
n! = 1 x 2 x 3 x ... x (n-1) x n
3. Program
The provided MATLAB code defines a function to calculate the factorial of a positive
integer. Here's a breakdown of its functionality:
Input: The code takes a single input (n) representing the number for which the factorial
needs to be calculated.
Input Validation: The code includes an if statement to ensure n is a valid positive integer. It
checks for the following conditions:
o ~isscalar(n): Verifies n is a single value.
o n < 0: Checks if n is negative (factorials are not defined for negative numbers).
o n ~= fix(n): Confirms n is an integer (no decimals allowed).
o If any of these conditions are not met, the code throws an error message
indicating invalid input.
Initialization: A variable factorial_result is initialized to 1, which serves as the starting point
for the factorial calculation.
Loop for Factorial Calculation: A for loop iterates from k = 1 to k = n. In each iteration,
the current value of factorial_result is multiplied by the current loop variable k. This
essentially multiplies all positive integers from 1 to n.
Output: The code uses disp to display a message containing the factorial of the input
number (n!) and its calculated value (factorial_result).
4. Results and Discussion
The code successfully calculates the factorial of a positive integer entered by the user.
The input validation ensures the code only accepts valid inputs and prevents
unexpected behavior.
5. Conclusion
This lab report demonstrated the calculation of factorials using a loop-based approach in
MATLAB. The code highlights the importance of input validation to ensure data
integrity and proper program execution. By changing the input value, you can
calculate the factorial of any positive integer.
Lab Report 2: Finding a Root using Regula-Falsi
Method
1. Aim/Objective
This lab report investigates the application of the Regula-Falsi method to find a root
of a non-linear equation. The objective is to solve the equation f(x) = x^3 - 2x - 5 = 0
using the Regula-Falsi method and analyze its iterative behavior.
2. Procedure/Algorithm
The Regula-Falsi method, also known as the False Position method, is an iterative
approach for finding roots of real-valued functions. It works by following these steps:
5. Update the interval by discarding the half where the function value has the same sign as f(x2).
6. Set a stopping criterion based on a tolerance level (e). Terminate the loop if the
absolute difference between consecutive root estimates falls below e.
3. Program
The provided code implements the Regula-Falsi method in MATLAB. It defines the
function f(x), sets initial guesses, and iterates using the Regula-Falsi formula. The
code also includes a stopping criterion based on a tolerance level and updates the
search interval based on the function signs. The output displays the iteration number,
current interval bounds, function values at the bounds, and the new root estimate with
its function value.
1. Results and Discussion
The provided output shows the iterative process of the Regula-Falsi method for
solving f(x) = x^3 - 2x - 5 = 0.
The initial guesses are x0 = 2 and x1 = 3. The function values at these points have opposite
signs, satisfying the method's requirement.
In each iteration, a new root estimate (x2) is calculated, and the search interval is refined
based on the function signs.
As the iterations progress, the values in the output table (interval bounds, root estimates, and
function values) become increasingly closer, indicating convergence towards a root.
The function value at the root estimate (f(x2)) approaches zero, as expected for a solution
to the equation.
5. Conclusion
The Regula-Falsi method is a useful iterative approach for finding roots of non-linear
equations. The provided code demonstrates its implementation and highlights the
convergence process by showing how the interval and root estimate get refined with
each iteration. By analyzing the changes in the root estimate or implementing a
stopping criterion based on a tolerance level, one can determine when convergence is
achieved and obtain a more precise root approximation.
This lab report investigates the application of the Newton-Rapson method to solve the
non-linear equation:
xe^x - 2 = 0
The objective is to find the root of this equation using an iterative approach and analyze
the convergence behavior.
2. Procedure/Algorithm
The Newton-Rapson method is an iterative algorithm for finding the roots of non- linear
equations. It works by utilizing the following steps:
3. Program
The provided code snippet implements the Newton-Rapson method in MATLAB. The
code defines the function f(x) = xe^x - 2 and its derivative df(x) = (x+1)e^x. It then iterates
using an initial guess, a maximum number of iterations, and a tolerance level. The
code displays the iteration number, current guess, function value, derivative, step size,
and updated guess for each iteration.
4. Input and Output
Input:
o The program takes the following inputs:
Output:
Iteration number
Current guess (x_0)
Function value (f(x_0))
Derivative (df(x_0))
Step size
Updated guess (x_1)
o If convergence occurs within the set iterations, a message indicating successful root
finding is displayed along with the final root value.
o If convergence doesn't occur, an error message may be displayed depending on
the implementation.
5. Conclusion and Interpretation
This lab report investigates the application of the Gauss-Seidel iteration method to
solve a system of linear equations. The objective is to demonstrate the iterative
process and analyze the convergence behavior of the method for a given system.
2. Procedure/Algorithm
1. Define the coefficient matrix A and the right-hand side constant vector b of the system.
2. Set the maximum number of iterations (max_iter) and a tolerance level (tol) for
the stopping criterion.
3. Choose an initial guess for the solution vector (x_0).
4. Iterate through the following steps for each iteration (k):
Update the solution for variable x(i) using the Gauss-Seidel formula, considering previously
calculated values from the current iteration for variables appearing before x(i) and values from
the previous iteration for variables appearing after x(i).
Check the convergence criterion: If the norm of the difference between the current solution vector
(x) and the previous iteration's solution vector (x_old) is less than or equal to the tolerance (tol),
terminate the loop.
5. The solution vector x contains the approximate solution to the system of linear equations.
3. Program
The provided MATLAB code implements the Gauss-Seidel method. It defines the
coefficient matrix A, right-hand side vector b, maximum iterations (max_iter),
tolerance (tol), and performs the iterative updates and convergence checks as
described in the algorithm. The code also includes a diagonal dominance check to
ensure the method is suitable for the given system.
4. Results and Discussion
The provided output shows the iterative process of the Gauss-Seidel method for a
specific system of equations. We observe the following:
Iterations: Lines labeled "Iter. k" indicate the current iteration number.
Solution Updates: After each iteration, the code prints the updated estimates for all three
variables (x_1, x_2, and x_3). We can see the values gradually converge towards a
solution.
5. Conclusion
The Gauss-Seidel method is a valuable tool for solving systems of linear equations.
This lab report demonstrated its implementation and highlighted the iterative nature
of the solution process. By analyzing the changes in solution estimates and
implementing a stopping criterion, one can determine when convergence is achieved
and obtain the approximate solution to the system.
Lab Report 5: Gauss-Jacobi Iterative Method
1. Aim/Objective
This lab report investigates the application of the Gauss-Jacobi iterative method to
solve a system of linear equations. The objective is to find the solution vector x for a
given system represented by a coefficient matrix A and a constant vector b.
2. Procedure/Algorithm
1. Define the coefficient matrix A and the constant vector b representing the system of equations.
2. Choose an initial guess for the solution vector x. A common approach is to divide each
element of b by the corresponding diagonal element of A.
3. Iterate through the following steps until convergence is achieved:
1. For each element x_i in the solution vector:
3. where: * A(i,i) is the diagonal element of row i in matrix A. * b(i) is the element at position i in
vector b. * A(i,:) is the entire row i of matrix A. * x_old is the solution vector from the previous
iteration.
4. Check for convergence using a stopping criterion, such as the difference between
consecutive solution vectors falling below a predefined tolerance level.
3. Program
Input:
o The program takes the coefficient matrix A and constant vector b as input.
o It might also include an initial guess for the solution vector and a tolerance level for
convergence.
Output:
o The program iteratively updates the solution vector and displays its values for each
iteration.
o If convergence occurs within the set iterations, the loop terminates without any
additional message.
The provided output shows the first six iterations of the Gauss-Jacobi method. We can
observe that the values in the solution vector are getting closer with each iteration,
indicating convergence. However, the complete output is needed to determine if the
method converges within the set tolerance level.
6. Conclusion
The Gauss-Jacobi method is a useful iterative approach for solving systems of linear
equations. The provided code demonstrates its implementation and the iterative
process. It's important to set appropriate convergence criteria and monitor the
solution vector to ensure successful convergence.
Lab Report 7: Numerical Integration using
Trapezoidal Rule
1. Aim/Objective
This lab report investigates the application of the trapezoidal rule for numerical
integration. The objective is to approximate the definite integral of a given function
f(x) over a specified interval using the trapezoidal rule and analyze the impact of the
number of sub-intervals on the approximation accuracy.
2. Procedure/Algorithm
The trapezoidal rule is a numerical integration technique that approximates the area
under the curve of a function by dividing the integration interval into smaller sub-
intervals and summing the areas of trapezoids formed by the function and the x-axis.
The steps involved are:
1. Define the function f(x) to be integrated.Set the lower limit (a) and upper limit (b)
of the integration interval.
2. Choose the number of sub-intervals (n) for the approximation.
3. Calculate the step size (h) by dividing the interval length (b-a) by the number of
sub-intervals (n).
4. Generate a vector of ordinates (x) containing the x-values at which the function
will be evaluated. This typically involves starting at the lower limit ( a) and
incrementing by the step size (h) until reaching the upper limit (b).
5. Evaluate the function f(x) at each ordinate to obtain a vector of function values (y).
6. Implement the trapezoidal rule formula:
where:
* y(1) and y(end) are the first and last elements of the function values
vector (y), representing the function values at the integration limits.
* y(2:end-1) are the inner elements of the function values vector, representing
the function values at the interior points of the sub-intervals.
3. Program
The provided code implements the trapezoidal rule in MATLAB. It defines a function
trapezoidal_rule(f, a, b, n) that takes the function, integration interval limits, and number
of sub-intervals as input and calculates the approximate integral value, ordinates, and
function values. The code then demonstrates calling the function with a specific
function (f(x) = exp(x) / (1 + x)), interval ([0, 6]), and varying the number of sub-intervals
(10, 20, and 50) to observe the effect on the approximation.
The provided output shows the approximate integral values (T) calculated using the
trapezoidal rule for different numbers of sub-intervals (n). We observe that the
integral values:
Get closer to each other as n increases (10, 20, and 50). This suggests a convergence towards
a more accurate approximation.
5. Conclusion
The trapezoidal rule is a useful method for numerical integration. The provided code
demonstrates its implementation and how the number of sub-intervals impacts the
approximation accuracy. As the number of sub-intervals increases, the trapezoidal
rule tends to provide a more accurate estimate of the definite integral.
Lab Report 8: Solving an ODE using Runge-Kutta
4th Order Method
1. Aim/Objective
This lab report investigates the application of the Runge-Kutta 4th order method (RK4)
to solve an Ordinary Differential Equation (ODE). The objective is to numerically
approximate the solution of the ODE dy/dx = 2x - x^2 + 4xy using RK4 and analyze the
impact of the step size on the solution accuracy.
2. Procedure/Algorithm
3. Program
The provided code implements the RK4 method in MATLAB. It defines the ODE
function F_xy, sets the initial condition, step size, and calculates the solution at
discrete points using the RK4 loop. The output shows the solution values for a
specific step size.
4. Results and Discussion
The provided output displays the solution values (y) of the ODE at three points (x = 0,
0.1, 0.2) calculated using RK4 with a step size of h = 0.1.
5. Conclusion
The RK4 method is a powerful tool for numerically approximating solutions to ODEs.
This lab report demonstrated its implementation and highlighted the importance of the
step size in influencing the solution accuracy. Further exploration can involve
visualizing the solution, analyzing the impact of step size quantitatively, and
comparing RK4 with other numerical methods.