0% found this document useful (0 votes)
3 views9 pages

Explanations -MATLAB

This document provides detailed explanations of MATLAB codes for solving systems of linear equations using various methods, including the method of solving linear equations, Gauss-Jacobi, and Gauss-Seidel methods. Each section outlines the purpose of the code, the structure of matrices and vectors used, and the iterative processes involved in calculating unknown variables. The document emphasizes the initialization of variables, convergence criteria, and the output of results in a readable format.

Uploaded by

xyrickaspiras847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Explanations -MATLAB

This document provides detailed explanations of MATLAB codes for solving systems of linear equations using various methods, including the method of solving linear equations, Gauss-Jacobi, and Gauss-Seidel methods. Each section outlines the purpose of the code, the structure of matrices and vectors used, and the iterative processes involved in calculating unknown variables. The document emphasizes the initialization of variables, convergence criteria, and the output of results in a readable format.

Uploaded by

xyrickaspiras847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Explanations

1.
This MATLAB code is designed to solve a truss problem using the method of solving
a system of linear equations. Below is a step-by-step explanation of each part of the
code:
1. clear and clc:
These commands clear the workspace and the command window,
respectively. clear removes all variables, and clc clears the command window
for a fresh output display.
2. A = [ ... ]:
This defines a matrix A, which represents the system of equations for the
truss problem. Each row in the matrix corresponds to an equation, and each
column represents the unknown forces acting in various parts of the truss.
The elements in the matrix are coefficients for these forces. The structure of
the matrix likely corresponds to the forces and displacements in the truss
members based on the geometry and load conditions.
3. B = [ ... ]:
This vector B contains the known values (forces and displacements) on the
truss system. Each entry represents the force applied at a node in the truss.
For example, B(2) = 2000 represents a force of 2000 N applied at node 2.
These values are typically the loads applied to the system or boundary
conditions.
4. X = linsolve(A,B);
The linsolve function is used to solve the system of linear equations
A×X=BA \times X = BA×X=B for the unknown forces and reactions (stored in
vector X). The result is a vector X, where each entry corresponds to a force or
reaction in the truss system.
5. Extracting Values:
o F1 = X(1,1);

o F2 = X(2,1);

o F3 = X(3,1);

o F4 = X(4,1);

o F5 = X(5,1);

o F6 = X(6,1);

o F7 = X(7,1);

o F8 = X(8,1);
o F9 = X(9,1);

o R1 = X(10,1);

o R2 = X(11,1);

These lines extract individual forces and reactions from the vector X. Each F and R
represents a specific force or reaction at a truss node or member. For instance, F1
represents the force in the first member, R1 represents a reaction force at the first
node, and so on.
6. fprintf commands:
These lines output the results of the calculations to the command window in a
readable format. Each fprintf statement prints the value of a specific force or
reaction, formatted to four decimal places. For example, fprintf('The value of
F1 is %.4f N \n', F1) prints the value of F1 (the force in the first truss member)
with four decimal places.
The result of running the code is a list of forces and reactions for each member and
node in the truss system. These forces represent the internal forces in the truss
members and the reactions at the supports due to applied loads. The solution is
calculated using the linear system approach, where matrix A defines the
relationships between forces and displacements, and vector B contains the known
load values.
2.
This MATLAB code determines the cost per pound of five ingredients in a mixture.
Here’s an explanation of the code, broken down step by step:
1. clear and clc:
These commands are used to clear the workspace and the command window,
respectively. clear removes all variables from the workspace, and clc clears
the command window to make the output display clean.
2. A = [0.2, 0.2, 0.2, 0.2, 0.2; 0.35, 0.15, 0.35, 0, 0.15; 0.1, 0.3, 0.1, 0.1,
0.4; 0, 0.3, 0.1, 0.4, 0.2; 0.15, 0.3, 0.2, 0.35, 0]:
This creates a 5x5 matrix A representing the coefficients of the amount of
ingredients in each mix. Each row corresponds to a different mixture, and
each column corresponds to the proportion of a specific ingredient in that
mixture. For example, the first row indicates that in the first mix, each of the
five ingredients (Peanuts, Raisins, Almonds, Chocolate Chips, Dried Plums)
contributes 0.2 pounds.
3. b = [1.44; 1.16; 1.38; 1.78; 1.61]:
This vector b contains the total cost for each of the five mixes. Each value
represents the total cost for that specific mix. For instance, the first value
(1.44) indicates the total cost for the first mixture.
4. x = A \ b;
This line solves the system of linear equations represented by the matrix
equation A×x=bA \times x = bA×x=b, where x is the vector of the cost per
pound of each ingredient. The backslash operator \ is used to perform matrix
division, effectively finding the values of x that satisfy the equation. The
result is stored in the vector x.
5. fprintf('Cost per pound of each ingredient:\n');
This prints a message to the command window, introducing the output of the
cost per pound for each ingredient.
6. fprintf('Peanuts: $%.2f per pound\n', x(1));
This displays the cost per pound for Peanuts, using the first element of vector
x (x(1)). The %.2f formats the value to two decimal places, showing the result
as a monetary value.
7. fprintf('Raisins: $%.2f per pound\n', x(2));
This displays the cost per pound for Raisins, using the second element of
vector x (x(2)), formatted as a monetary value.
8. fprintf('Almonds: $%.2f per pound\n', x(3));
This displays the cost per pound for Almonds, using the third element of
vector x (x(3)), formatted as a monetary value.
9. fprintf('Chocolate Chips: $%.2f per pound\n', x(4));
This displays the cost per pound for Chocolate Chips, using the fourth
element of vector x (x(4)), formatted as a monetary value.
10.fprintf('Dried Plums: $%.2f per pound\n', x(5));
This displays the cost per pound for Dried Plums, using the fifth element of
vector x (x(5)), formatted as a monetary value.
The result is the cost per pound for each ingredient, which is printed in a clear and
readable format.
3.
This MATLAB code solves a system of linear equations using the Gauss-Jacobi
method, which is an iterative technique for solving systems of linear equations.
Below is a step-by-step explanation of the code:
1. clear and clc:
o clear removes all variables from the workspace.

o clc clears the command window, providing a clean display for output.

2. A = [9, -2, 3, 2; 2, 8, -2, 3; -3, 2, 11, -4; -2, 3, 2, 10];


o This defines the coefficient matrix A, which represents the
coefficients of the system of equations. Each row corresponds to an
equation, and each column represents a variable in the system.
3. b = [54.5; -14; 12.5; -21];
o This defines the constant vector b, which contains the constants on
the right-hand side of the system of equations. The elements of this
vector are the values that result from the linear combinations of
variables in the system.
4. x = zeros(4, 1);
o This initializes the vector x with zeros. This vector will hold the
variables x1,x2,x3,x4x_1, x_2, x_3, x_4x1,x2,x3,x4, which are the
unknowns in the system.
5. tol = 1e-6;
o This sets the tolerance for convergence. The method will stop
iterating when the difference between the new and old values of x is
smaller than this tolerance.
6. max_iter = 100;
o This defines the maximum number of iterations. If the method does
not converge within 100 iterations, the algorithm will stop.
7. n = length(b);
o This assigns the number of variables (or the size of the system) to n,
which is equal to the length of the vector b.
8. x_new = zeros(n, 1);
o This initializes a new vector x_new that will store the updated values of
the variables after each iteration.
9. fprintf('Iteration\tx1\tx2\tx3\tx4\n');
o This prints a header to the command window for displaying the
iteration number and the values of x1,x2,x3,x4x_1, x_2, x_3, x_4x1,x2
,x3,x4 in each iteration.
10.for iter = 1:max_iter
o This begins the outer loop, which runs for a maximum of max_iter
iterations.
11.for i = 1:n
o This starts an inner loop, iterating through each variable (from x1x_1x1
to xnx_nxn) to update its value.
12.sum = 0;
o A variable sum is initialized to zero. This will store the sum of the
products of the coefficients and the current values of other variables.
13.for j = 1:n
o This nested loop sums the contributions from the other variables,
excluding the diagonal element (the coefficient of the current variable).
14.if i ~= j
o This ensures that the diagonal element (the coefficient corresponding
to the variable x_i) is excluded from the sum.
15.sum = sum + A(i, j) * x(j);
o This adds the product of the coefficient and the value of the other
variable to sum.
16.x_new(i) = (b(i) - sum) / A(i, i);
o This updates the value of x_new(i) using the Jacobi iteration
formula. The updated value is calculated by subtracting the sum of
the contributions of all other variables from the constant term and
dividing by the diagonal coefficient.
17.fprintf('%d\t%.6f\t%.6f\t%.6f\t%.6f\n', iter, x_new);
o This prints the current iteration number and the values of x1, x2,x3,x4
to the command window. The values are displayed to six decimal
places.
18.if norm(x_new - x, inf) < tol
o This checks if the difference between the new and previous values of x
(measured using the infinity norm) is smaller than the tolerance tol. If
it is, the method has converged.
19.fprintf('Converged after %d iterations.\n', iter);
o If the method converges, this line prints the number of iterations it
took for convergence.
20.break;
o If the method converges, this break statement exits the loop early.

21.x = x_new;
o If the method has not yet converged, this updates the vector x with the
new values from x_new to be used in the next iteration.
22.if iter == max_iter
o This checks if the maximum number of iterations has been reached
without convergence.
23.fprintf('Did not converge within the maximum number of iterations.\
n');
o If the method did not converge within the maximum allowed iterations,
this message is printed.
24.Final Output (fprintf loop):
o This loop prints the final values of the variables x1, x2,x3,x4 after the
iterations are complete, whether or not the method has converged.
The Gauss-Jacobi method is an iterative approach to solving linear systems, and this
MATLAB code implements it by iteratively updating the variables until convergence
criteria are met.
4.
This MATLAB code solves a system of linear equations using the Gauss-Seidel
method, an iterative technique for solving systems of equations. Below is a step-
by-step explanation of the code:
1. clear and clc:
o clear removes all variables from the workspace.

o clc clears the command window to provide a clean output display.

2. A = [9, -2, 3, 2; 2, 8, -2, 3; -3, 2, 11, -4; -2, 3, 2, 10];


o This defines the coefficient matrix A for the system of linear
equations. Each row represents an equation, and each column
represents a variable.
3. b = [54.5; -14; 12.5; -21];
o This defines the constant vector b, which contains the constants on
the right-hand side of the system of equations.
4. x = zeros(4, 1);
o This initializes the solution vector x with zeros. It represents the
variables x1,x2,x3,x4x_1, x_2, x_3, x_4x1,x2,x3,x4.
5. tol = 1e-6;
o This sets the tolerance for convergence. The iteration will stop if the
change between successive values of x is smaller than tol.
6. max_iter = 100;
o This defines the maximum number of iterations the algorithm will
run before stopping if it doesn't converge.
7. n = length(b);
o This assigns the number of variables (the size of the system) to n,
which is the length of vector b.
8. fprintf('Iteration\tx1\tx2\tx3\tx4\n');
o This prints the header for the iteration table, showing the iteration
number and the values of x1,x2,x3,x4x_1, x_2, x_3, x_4x1,x2,x3,x4.
9. for iter = 1:max_iter
o This begins the loop for the Gauss-Seidel iteration. It runs up to
max_iter iterations.
10.x_old = x;
o This stores the current values of x before the iteration starts. It will be
used later to check for convergence.
11.for i = 1:n
o This inner loop iterates over each variable x1,x2,…,xnx_1, x_2, \dots,
x_nx1,x2,…,xn to update their values.
12.sum = 0;
o Initializes a variable sum to accumulate the sum of the products of the
off-diagonal elements in row i and the current values of the variables.
13.for j = 1:n
o This nested loop calculates the sum of the terms excluding the
diagonal element.
14.if i ~= j
o This condition ensures that the diagonal element (the coefficient for
the variable x_i) is excluded from the sum.
15.sum = sum + A(i, j) * x(j);
o This adds the product of the coefficient A(i,j) and the current value x(j)
to sum.

16. fprintf('%d\t%.6f\t%.6f\t%.6f\t%.6f\n', iter, x);


o This prints the current iteration number and the updated values of x1,x2,x3,x4x_1,
x_2, x_3, x_4x1,x2,x3,x4.
17. if norm(x - x_old, inf) < tol
o This checks if the difference between the new and old values of x is smaller than
the specified tolerance (tol). If so, the method has converged.
18. fprintf('Converged after %d iterations.\n', iter);
o If the method converges, this message is displayed, showing the number of
iterations taken.
19. break;
o If convergence is achieved, the loop is exited early.
20. if iter == max_iter
o If the loop reaches the maximum number of iterations without convergence, this
condition is true.
21. fprintf('Did not converge within the maximum number of iterations.\n');
o This message is printed if the method does not converge within the maximum
allowed iterations.
22. Final Output (fprintf loop):
o After the iterations, the final values of x1,x2,x3,x4x_1, x_2, x_3, x_4x1,x2,x3,x4
are printed.

The Gauss-Seidel method iteratively refines the guesses for the variables x1,x2,x3,x4 and
continues until the solution converges within the tolerance or the maximum iterations are
reached.

5.
Here's a MATLAB program implementing the False Position Method to determine the
root of ( f(x) = -2 + 6x - 4x^2 + 0.5x^3 ) within a specified tolerance ( E_s =
0.02% ) for the interval ([0,1]): Explanation of the Code:
1. Function Definition: - The function ( f(x) ) is defined as an anonymous function.
2. Initial Interval: - The root search is confined to the interval ([0,1]).
3. Stopping Criterion: - The loop continues until the approximate relative error
( E_a ) is less than or equal to ( E_s = 0.02% ).
4. False Position Formula: - ( x_r = b - frac{f(b) cdot (b - a)}{f(b) - f(a)} ).
5. Interval Update: - Based on the sign of ( f(x_r) ), either ( a ) or ( b ) is updated.
6. Error Calculation: - Approximate relative error is calculated as ( E_a = left|
frac{x_r - x_{r,text{old}}}{x_r} right| times 100 ).
7. Output: - Iteration results are printed, and the final root estimate is displayed with
the corresponding error and iteration count.
6.
Below is a MATLAB program that uses the Newton-Raphson method to find the root
of the function ( f(x) = x^3 + 18x^2 + 51x - 74 ) with an initial guess of ( x_0 = 1 )
and a specified error tolerance of ( E_s = 0.02% ). The program will display the
iteration results: Explanation: - f is the function ( f(x) = x^3 + 18x^2 + 51x - 74 ). -
f_prime is the derivative of the function, ( f'(x) = 3x^2 + 36x + 51 ). - The initial
guess ( x_0 ) is set to 1. - The loop continues until the relative error ( E_s ) is less
than 0.02%. - The program prints the results of each iteration, including the current
guess ( x ), ( f(x) ), and the error percentage. - The final output displays the root
found, the number of iterations, and the final error.

You might also like