[go: up one dir, main page]

0% found this document useful (0 votes)
0 views17 pages

Bryan Manual of Procedure

This C++ console application solves common numerical problems in math and engineering, including root-finding algorithms and systems of linear equations. It features interactive methods such as Bisection, False-Position, Newton-Raphson, and Gauss Elimination, allowing users to input values and view step-by-step results. The program serves as a practical educational tool for students and educators to learn numerical algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views17 pages

Bryan Manual of Procedure

This C++ console application solves common numerical problems in math and engineering, including root-finding algorithms and systems of linear equations. It features interactive methods such as Bisection, False-Position, Newton-Raphson, and Gauss Elimination, allowing users to input values and view step-by-step results. The program serves as a practical educational tool for students and educators to learn numerical algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Introduction to the Code

This C++ program is a console application designed to solve common numerical problems in
math and engineering. It includes methods for finding equation roots and solving systems of
linear equations. The program is interactive, guiding users to input the required values and
showing step-by-step results where needed.

Purpose: The program serves as a practical tool for students, educators, and anyone
interested in learning and using numerical algorithms. It allows users to interact with the
methods directly, showing how they work and how input values affect the results.

Key Features:
• Root-Finding Algorithms: Solves non-linear equations using methods such as Bisection,
False-Position, and Newton-Raphson.

• Linear System Solvers: Solves systems of linear equations using both Gauss Elimination
(without pivoting) and Gauss Elimination with Scaled Pivoting.

• Equation Builder: A flexible system that allows users to define custom algebraic,
trigonometric, hyperbolic, and exponential functions for root-finding problems.

• Clear Output: Results are presented in well-organized tables and matrix displays, making
the iteration process easy to follow and understand.

Included Libraries/Modules:
The program is divided into separate namespaces, each dedicated to a specific Machine
Problem (MP) or a set of utility functions.

• main () function: The entry point of the program, offering a menu to select which
Machine Problem to run.
• MP1 Namespace: Handles the Quadratic Equation Solver.
• MP2 Namespace: Implements the Bisection Method.
• MP3 Namespace: Implements the False-Position Method.
• MP4 Namespace: Implements the Newton-Raphson Method.
• MP5 Namespace: Implements the Gauss Elimination method without pivoting.
• MP6 Namespace: Implements the Gauss Elimination method with Scaled Pivoting.
• Numerical Methods Namespace: A utility namespace used by root-finding methods
(MP2, MP3, MP4). It includes:
o Equation Component struct: Defines the components of an equation.
o Function Type e num: Classifies functions (e.g., Algebraic, Sine, Cosine,
etc.).
o OperationType enum: Defines mathematical operations (Add, Subtract,
Multiply, Divide).
o solution (): Evaluates the defined equation at a specific x.
o derivative (): Computes the numerical derivative of the defined equation at a
given x.
o buildEquation(): Assists the user in constructing a custom equation.
o displayEquation(): Prints the user-defined equation in a readable format.
Machine Problem 1: Quadratic Equation
Solver

Description/Purpose:

Solves quadratic equations ax2+bx+c=0ax^2 + bx + c = 0ax2+bx+c=0, handling real distinct


roots, real repeated roots, and complex roots.

Options/Features:
• Prompts for coefficients a, b, and c.
• Checks if a = 0 and requests re-entry (to avoid linear equation).
• Displays root types and values clearly.
• Supports solving multiple equations in sequence.

Instructions:
1. Run the program to start the solver.
2. Enter value of a when prompted.
3. Enter values of b and ccc similarly.
4. View the calculated roots displayed.
5. When asked "Do you want to solve another quadratic equation? (yes/no):", type yes or
no and press Enter.
6. If "yes", repeat from step 2; if "no", program exits.

Example:
Solve x2+4x+3 = 0
Machine Problem 2: Bisection Method Root
Finder

Description/Purpose:
Finds the root of an equation f(x)=0 within a specified interval [a,b]using the Bisection
Method.
It requires the function values at the interval endpoints to have opposite signs, indicating a
root exists between them. The method repeatedly bisects the interval and narrows down to the
root with a specified error tolerance.

Options/Features:
• Prompts for polynomial type, degree, and coefficients.
• Prompts for error tolerance ϵ\epsilonϵ.
• Prompts for initial interval [a,b]
• Validates that f(a)×f(b)<0; requests re-entry if invalid.
• Displays an iteration table showing current interval, midpoint, function values, and
error at each step.
• Stops when the error is less than ϵ\epsilon or root is found with high precision.
• Limits iterations to 1000 to avoid infinite loops.

Instructions:
1. Run the program to start the Bisection Method solver.
2. Enter the polynomial type, degree, and coefficients as prompted.
3. Enter the desired error tolerance (epsilon).
4. Enter the interval [a,b] where you expect the root to be.
5. If the interval is invalid, you will be asked to re-enter it.
6. View the iteration table and root result displayed.
7. The program ends after the root is found or maximum iterations reached.
Example:
In physics and engineering, cubic polynomials often arise when analyzing equilibrium
positions, beam deflections, or energy states. Consider the following polynomial equation:

f(x)= x3− x− 2 = 0

We want to determine the root of this equation using the Bisection Method. Based on prior
analysis or graphing, we suspect a root lies within the interval [1,2]. Use the Bisection
Method to approximate the root with an error tolerance of ε=0.0001

Solve the root of f(x)=x3−x−2= 0 in the interval [1,2] with error tolerance ϵ = 0.001

--- MP_2: Bisection Method ---


Enter polynomial type (e.g., 1 for polynomial): 1
Enter algebraic degree: 3
Enter coefficient for x^3: 1
Enter coefficient for x^2: 0
Enter coefficient for x^1: -1
Enter coefficient for x^0: -2
Enter desired error tolerance epsilon: 0.0001

Enter interval [a, b] where the root is expected: 1 2

Iteration Table:
---------------------------------------------------------------------------------------------------------------
i a b f(a) f(b) c f(c) Error
---------------------------------------------------------------------------------------------------------------
0 1.000000 2.000000 -2.000000 4.000000 1.500000 0.875000 1.000000
1 1.000000 1.500000 -2.000000 0.875000 1.250000 -1.296875 0.500000
2 1.250000 1.500000 -1.296875 0.875000 1.375000 -0.205078 0.250000
... (more iterations) ...
---------------------------------------------------------------------------------------------------------------
The root found using the bisection method is: 1.379729
Machine Problem 3: False-Position Method
Root Finder

Description/Purpose:
Finds the root of an equation f(x)=0f(x) = 0f(x)=0 within a specified interval [a,b][a, b][a,b]
using the False-Position (Regula Falsi) Method.
This method improves on the bisection method by using linear interpolation to approximate
the root and generally converges faster.

Options/Features:
• Prompts for polynomial type, degree, and coefficients.
• Prompts for error tolerance ϵ\epsilonϵ.
• Prompts for initial interval [a,b][a, b][a,b].
• Validates that f(a)×f(b)<0f(a) \times f(b) < 0f(a)×f(b)<0; requests re-entry if invalid.
• Displays an iteration table showing current interval, function values, estimated root,
function value at root, and error.
• Stops when the function value at the estimated root or the error falls below ϵ\epsilonϵ.
• Handles cases where denominator f(b)−f(a)f(b) - f(a)f(b)−f(a) is very small to avoid
division errors.
• Limits iterations to 1000 to prevent infinite loops.

Instructions:
1. Run the program to start the False-Position Method solver.
2. Enter the polynomial type, degree, and coefficients as prompted.
3. Enter the desired error tolerance (epsilon).
4. Enter the interval [a,b][a, b][a,b] where you expect the root to be.
5. If the interval is invalid (function values at ends not of opposite sign), you will be
asked to re-enter it.
6. View the iteration table showing progress and the root estimate at each step.
7. The program ends when the root is found within the specified tolerance or the
iteration limit is reached.
Example:
In mechanical systems, resonance can lead to dangerous oscillations. Engineers may need to
find time t when oscillations cancel out. Suppose the behavior is modeled by:

f(t)=t ⋅ cos(t) − 2t2 + 1 = 0


We suspect a root exists between t = 0.5 and t = 1.0.
Use the False-Position Method with an error tolerance ε = 0.001 to find the root.

--- MP_3: False-Position Method ---


Enter function type: 1
Enter the degree of the equation: 2
Enter coefficient for x^2: -2
Enter coefficient for x^1: 0
Enter coefficient for x^0: 1
Enter interval [a, b] where the root is expected: 0.5 1.0
Enter tolerance (epsilon): 0.001

False-Position Method Iteration Table:


---------------------------------------------------------------------------------------------------------------------------
i a b f(a) f(b) c f(c) Error(|c-a|)
---------------------------------------------------------------------------------------------------------------------------
0 0.500000 1.000000 0.315490 -0.459700 0.724137 -0.017300 0.224137
1 0.500000 0.724137 0.315490 -0.017300 0.714355 0.000712 0.214355
2 0.714355 0.724137 0.000712 -0.017300 0.714975 0.000000 0.000620
---------------------------------------------------------------------------------------------------------------------------
The root found using the False-Position method is: 0.71497500
Machine Problem 4: Newton-Raphson Method

Description/Purpose:
Implements the Newton-Raphson method to find roots of real-valued functions. This
iterative technique uses tangents to approximate the root of a function.

Options/Features:
• Prompts for the type of function (e.g., polynomial).
• Accepts the algebraic degree and corresponding coefficients.
• Requests an initial guess for the root.
• Calculates the derivative numerically using a small increment hhh.
• Displays each iteration with current approximation, function value, derivative, next
approximation, and error.
• Continues iterations until the desired error tolerance is achieved or the maximum
number of iterations is reached.

Instructions:
1. Run the program to start the solver.
2. Enter the type of function when prompted (e.g., 1 for polynomial).
3. Enter the algebraic degree of the function.
4. Input the coefficients for each term, starting from the highest degree to the constant
term.
5. Enter the desired error tolerance ϵ\epsilonϵ.
6. Provide an initial guess x0x_0x0 for the root.
7. View the iteration table displaying the progress of the method.
8. After convergence or reaching the maximum iterations, the program will display the
approximated root.
Example Problem:

Problem Statement:

Determine the root of the equation f(x)=x3 − x −2 = 0using the Newton-Raphson method. Use
an initial guess X0=1.5 and an error tolerance ϵ=0.0001

--- MP_4: Newton-Raphson Method ---

Newton-Raphson Method Iteration Table:

--------------------------------------------------------------------------------------------------------

i x_i f(x_i) f'(x_i) x_i+1 Error(|x_i+1 - x_i|)

--------------------------------------------------------------------------------------------------------

0 1.500000 0.875000 5.370000 1.336953 0.163047

1 1.336953 0.063288 4.365700 1.322443 0.014510

2 1.322443 0.000486 4.252270 1.322329 0.000114

3 1.322329 0.000000 4.252000 1.322329 0.000000

---------------------------------------------------------------------------------------------------------

The root found using the Newton-Raphson method is: 1.32232900


Machine Problem 5: Gauss Elimination
Solver
Description/Purpose:
Solves systems of linear equations using Gauss Elimination method. It transforms the system
into an upper triangular form and uses back substitution to find the unique solution.

Options/Features:
• Prompts the user to enter the number of unknowns.
• Accepts the augmented matrix input from the user (matrix A and vector b).
• Automatically performs partial pivoting to handle zero or small pivot values.
• Displays the matrix after each elimination step.
• Performs back substitution to compute the solution vector.
• Displays the complete solution after processing.

Instructions:
1. Run the program to launch the Gauss Elimination solver.
2. Enter the number of unknowns nnn when prompted.
3. Input the coefficients of each equation including the constants (i.e., the augmented
matrix [A|b]).
4. The program will show the initial matrix, any row swaps (if pivoting occurs), and
matrices after each elimination step.
5. After all elimination steps, the program will perform back substitution and show the
solution for each variable.
Problem Statement:

Solve the following system of linear equations using Gauss Elimination:

2x + 3y – z = 5

4x + 4y – 3z = 3

-2x + 3y – 1z = 1

Expected Input:

Enter the number of unknowns (n): 3

Enter the coefficients of the augmented matrix [A | b]:

Equation 1:

A [0][0]: 2

A [0][1]: 3

A [0][2]: -1

B [0]: 5

Equation 2:

A [1][0]: 4

A [1][1]: 4

A [1][2]: -3

B [1]: 3

Equation 3:

A [2][0]: -2

A [2][1]: 3

A [2][2]: -1

B [2]: 1
Expected Output:

Initial Augmented Matrix:

2 3 -1 | 5

4 4 -3 | 3

-2 3 -1 | 1

Swapped row 1 with row 2

Matrix after swap:

4 4 -3 | 3

2 3 -1 | 5

-2 3 -1 | 1

Matrix after elimination step 1:

4 4 -3 | 3

0 1 0| 3.5

0 5 -2.5 | 2.5

Matrix after elimination step 2:

4 4 -3 | 3

0 5 -2.5 | 2.5

0 0 1| 1

Solution:

x [0] = 1

x [1] = 2

x [2] = 1
Machine Problem 6: Gauss Elimination and
Scaled Pivoting

Description/Purpose:
This program solves systems of linear equations using the Gauss Elimination method
enhanced with Scaled Partial Pivoting. Scaled Pivoting improves numerical stability by
selecting pivot elements based on their relative size within each row, reducing errors due to
large variations in coefficient magnitudes.

Options/Features:
• Prompts for the number of unknowns (n).
• Accepts input for the augmented matrix [A|b], where A is the coefficient matrix and b
is the constants vector.
• Computes scale factors for each row to determine the most suitable pivot elements.
• Performs row swaps based on scaled pivoting criteria to enhance numerical stability.
• Displays the augmented matrix after each elimination step, reflecting any row swaps
and eliminations performed.
• Solves the system using back substitution after transforming the matrix to upper
triangular form.
• Outputs the solution vector with high precision.
• Detects and reports cases where the matrix is singular or nearly singular, indicating no
unique solution exists.

Instructions:
1. Run the Program:
Start the program to initiate the Gauss Elimination with Scaled Pivoting solver.
2. Enter the Number of Unknowns:
When prompted, input the number of unknowns (n) in your system of equations.
3. Input the Augmented Matrix:
For each equation (from 1 to n):
o Enter the coefficients for each variable in the equation.
o Enter the constant term (right-hand side value) for the equation.
4. View Initial Augmented Matrix:
The program will display the initial augmented matrix [A|b] based on your inputs.
5. Elimination Process:
The program performs the following for each pivot position:
o Calculates scale factors for each row.
o
Determines the pivot row by finding the maximum ratio of the absolute value
of the pivot candidate to its row's scale factor.
o Swaps rows if necessary to position the best pivot element.
o Eliminates entries below the pivot to form an upper triangular matrix.
o Displays the augmented matrix after each elimination step.
6. Back Substitution:
Once the matrix is in upper triangular form, the program performs back substitution to
solve for the unknown variables.
7. View Solution:
The program outputs the values of the unknowns with high precision.
8. Program Termination:
The program ends after displaying the solution.

Problem Question:
A mechanical engineer is designing a system of three interconnected pipes that work under
varying pressure conditions. The following system of equations describes the relationships
between the pressures in the three pipes:

5x1 – 2x2 + 3x3 = 7

-3x1 + 4x2 – 2x3 = -3

2x1 – x2 + 4x3 = 6

--- MP_6: Gauss Elimination with Scaled Pivoting ---

Enter the number of unknowns (n): 3

Enter the coefficients of the augmented matrix [A|b]:

Equation 1:

A[0][0]: 2

A[0][1]: 1

A[0][2]: -1

b[0]: 8

Equation 2:

A[1][0]: -3

A[1][1]: -1

A[1][2]: 2
b[1]: -11

Equation 3:

A[2][0]: -2

A[2][1]: 1

A[2][2]: 2

b[2]: -3

Initial Augmented Matrix:

2 1 -1 | 8

-3 -1 2| -11

-2 1 2| -3

Swapped logical row 1 with logical row 2 (actual rows 2 and 1)

Matrix after elimination step 1 (logical view):

-3 -1 2| -11

2 1 -1 | 8

-2 1 2| -3

Matrix after elimination step 2 (logical view):

-3 -1 2| -11

0 0.3333 0.3333 | 0.6667

0 1.3333 0.6667 | 4.3333

Matrix after elimination step 3 (logical view):


-3 -1 2| -11

0 0.3333 0.3333 | 0.6667

0 0 -1 | 1

Solution:

x[0] = 2.00000000

x[1] = 3.00000000

x[2] = -1.00000000

You might also like