Numsol Merge Files
Numsol Merge Files
Numerical solutions are approximate methods for solving mathematical problems that
cannot be easily solved using exact, analytical methods. These solutions involve iterative
procedures or algorithms to compute an answer to a desired level of accuracy.
• Analytical Solutions: These are exact solutions derived using algebraic manipulations and
closed-form expressions. For example, solving a quadratic equation using the quadratic
formula.
Numerical solutions are crucial for tackling complex real-world problems that are difficult or
impossible to solve analytically. Such problems often involve intricate equations or large systems
that do not have straightforward solutions.
1. Engineering:
2. Finance:
Error Analysis
Objective:
2. Learn how to compute errors and distinguish between absolute and true errors.
3. Learn methods to minimize errors and ensure the accuracy of numerical solutions.
Types of Errors
1. Truncation Error:
2. Round-off Error:
Sources of Errors
1. Absolute Error:
The absolute difference between the true value and the approximate value.
The absolute error divided by the true value, often expressed as a percentage.
|𝑥𝑡𝑟𝑢𝑒 − 𝑥𝑎𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑒 |
𝑅𝑒𝑙𝑎𝑡𝑖𝑣𝑒 𝐸𝑟𝑟𝑜𝑟 (𝑒𝑟 ) = 𝑥100%
|𝑥𝑡𝑟𝑢𝑒 |
3. True Error:
The difference between the exact mathematical value and the approximate value.
4. Approximate Error:
|𝑥𝑐𝑢𝑟𝑟𝑒𝑛𝑡 − 𝑥𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 |
𝑅𝑒𝑙𝑎𝑡𝑖𝑣𝑒 𝐴𝑝𝑝𝑟𝑜𝑥𝑖𝑚𝑎𝑡𝑒 𝐸𝑟𝑟𝑜𝑟 (𝑒𝑟 ) = 𝑥100%
|𝑥𝑐𝑢𝑟𝑟𝑒𝑛𝑡 |
Example Calculations
Given:
Given:
Suppose we are using an iterative method to find the root of an equation, and we have the
following successive approximations:
Objectives:
Materials Needed:
Lab Outline:
Defining functions.
A guided exercise involving the creation of a script that uses functions and plotting.
Detailed Instructions:
Starting Scilab:
Basic Commands:
--> a = 5 + 3;
--> b = 10 - 2;
--> c = 4 * 7;
--> d = 20 / 4;
--> disp(a);
--> disp(b);
--> disp(c);
--> disp(d);
--> x = 10;
--> y = 5
--> // Vector
--> disp(x);
--> disp(v);
--> x = 10;
--> y = 20;
--> sum = x + y;
--> difference = x - y;
--> product = x * y;
--> quotient = x / y;
--> disp(sum);
--> disp(difference);
--> disp(product);
--> disp(quotient);
// basic_operations
x = 10;
y = 20;
sum = x + y;
difference = x - y;
product = x * y;
quotient = x / y;
disp(sum);
disp(difference);
disp(product);
disp(quotient);
Defining Functions:
--> endfunction
--> disp(result);
// use_function
function y = myfunction(x)
y = x^2 + 2*x + 1;
endfunction
result1 = myfunction(2);
result2 = myfunction(5);
disp(result1);
disp(result2);
// Conditional statement
x = 5;
if x > 0 then
disp('x is positive');
else
disp('x is non-positive');
end
// Loop
for i = 1:5
disp(i);
end
--> disp(cos_theta);
--> disp(tan_theta);
--> disp(e);
--> disp(exp_val);
--> disp(log_val);
--> disp(log10_val);
Basic Plotting:
--> x = 0:0.1:10;
--> y = sin(x);
Customizing Plots:
--> xlabel('x-axis');
--> ylabel('y-axis');
--> legend('sin(x)');
Plotting Multiple Functions:
--> y1 = sin(x);
--> y2 = cos(x);
Guided Exercise:
1. Defines a function that takes a vector as input and returns the vector with each
element squared.
Solution:
// Define a function
function y = square_vector(x)
y = x .^ 2;
endfunction
v = 0:0.1:5;
squared_v = square_vector(v);
disp(squared_v);
sin_v = sin(v);
exp_v = exp(v);
plot(v, v, 'r', v, squared_v, 'b', v, sin_v, 'g', v, exp_v, 'm', v, log_v, 'c');
title('Various Functions');
xlabel('x-axis');
ylabel('y-axis');
In this tutorial, you will learn how to use SCILAB to perform basic matrix operations, such as
entering matrices, finding their sizes, adding, subtracting, and multiplying them. You will also learn
how to use Scinote, a web-based notebook for SCILAB, to display your results.
To enter a matrix in SCILAB, you can use square brackets [ ] and separate the elements by spaces or
commas. To start a new row, you can use a semicolon ; or a new line. For example, to enter the
matrix A = [ 1 2 3 ; 4 5 6 ], you can type:
-->A = [1 2 3; 4 5 6]
or
-->A = [1, 2, 3
4, 5, 6]
To find the size of a matrix, you can use the size function, which returns the number of rows and
columns as a vector. For example, to find the size of the matrix A, you can type:
-->size(A) // The first number indicates the row and the second number indicates the column.
2. 3.
To add or subtract two matrices, they must have the same size. You can use the + or - operators to
perform element-wise addition or subtraction. For example, to add the matrices C and D, you can
type:
-->C = [ 7 ; 8 ; 9 ]
-->D = [ 10 11 ]
-->C + D
Operator +: Wrong dimensions for operation [3x1] + [1x2], same dimensions expected.
-->A = [1 2 3; 4 5 6]
-->B = [-1 1 5; 2 -3 4]
-->A+B
0. 3. 8.
6. 2. 10.
-->A-B
ans =
2. 1. -2.
2. 8. 2.
To multiply two matrices, the number of columns of the first matrix must equal the number of rows
of the second matrix. You can use the * operator to perform matrix multiplication. For example, to
multiply the matrices A and B, you can type:
-->A = [1 2 3; 4 5 6]
-->B = [-1 1 5; 2 -3 4]
-->A*B
-->A = [1 2 3; 4 5 6]
-->B = [ -1; 0 ; 1 ]
-->A * B
2.
2.
For this part, create a new scinotes file and type the following. Run and save the file. See the results
in the console.
A = [2 -1 0; 3 1 2; 4 5 -1];
B = [1 2 3; 4 5 6; 7 8 9];
disp(A)
disp(A - B)
// from here, save and run your scinotes file for the results, then exit file for the results
shown on command console.
You can use the input function to accept the elements of a matrix from the user. The input function
takes a string as an argument and displays it as a prompt. You can also specify the type of the input
by using the 'string', 'double' or 'integer' keywords.
For example, to ask the user for a 3x3 matrix, you can use the following code:
// Loop through each element and prompt the user to input values
for i = 1:m
for j = 1:n
end
end
You can repeat the same process for another matrix, say B, and then display the result of any
operation you want.
// Add the following program at the end of the existing program, and when done, run and
save the file.
// create a matrix B with the same dimensions as A
// Loop through each element and prompt the user to input values
for i = 1:m
for j = 1:n
end
end
disp(A - B)
disp(A * B)
This laboratory activity ends here. Perform the following lab exercises using the lecture on
SCILAB above. You may use SCILAB Notes or use the command console for the execution of this
exercises.
If you choose to place it in SCINOTES, you may combine the exercises from numbers 1-5, ‘a’
to ‘j’ into a single file and save it as lab_exercises2. Go to command console and copy and save the
output of this lab_exercises2 in a document file, and label it as lab_exercises2_output.
If you choose to do the activity in the command console, go to command console and copy
and save all the commands and output of this lab exercises in a document file, and label it as
lab_exercises2_output.
Lab Exercises
In this tutorial, you will learn how to use SCILAB to perform basic matrix operations, such as
entering matrices, finding their sizes, adding, subtracting, and multiplying them. You will also learn
how to use Scinote, a web-based notebook for SCILAB, to display your results.
To enter a matrix in SCILAB, you can use square brackets [ ] and separate the elements by spaces or
commas. To start a new row, you can use a semicolon ; or a new line. For example, to enter the
matrix A = [ 1 2 3 ; 4 5 6 ], you can type:
-->A = [1 2 3; 4 5 6]
or
-->A = [1, 2, 3
4, 5, 6]
To find the size of a matrix, you can use the size function, which returns the number of rows and
columns as a vector. For example, to find the size of the matrix A, you can type:
-->size(A) // The first number indicates the row and the second number indicates the column.
2. 3.
To add or subtract two matrices, they must have the same size. You can use the + or - operators to
perform element-wise addition or subtraction. For example, to add the matrices C and D, you can
type:
-->C = [ 7 ; 8 ; 9 ]
-->D = [ 10 11 ]
-->C + D
Operator +: Wrong dimensions for operation [3x1] + [1x2], same dimensions expected.
-->A = [1 2 3; 4 5 6]
-->B = [-1 1 5; 2 -3 4]
-->A+B
0. 3. 8.
6. 2. 10.
-->A-B
ans =
2. 1. -2.
2. 8. 2.
To multiply two matrices, the number of columns of the first matrix must equal the number of rows
of the second matrix. You can use the * operator to perform matrix multiplication. For example, to
multiply the matrices A and B, you can type:
-->A = [1 2 3; 4 5 6]
-->B = [-1 1 5; 2 -3 4]
-->A*B
-->A = [1 2 3; 4 5 6]
-->B = [ -1; 0 ; 1 ]
-->A * B
2.
2.
For this part, create a new scinotes file and type the following. Run and save the file. See the results
in the console.
A = [2 -1 0; 3 1 2; 4 5 -1];
B = [1 2 3; 4 5 6; 7 8 9];
disp(A)
disp(A - B)
// from here, save and run your scinotes file for the results, then exit file for the results
shown on command console.
You can use the input function to accept the elements of a matrix from the user. The input function
takes a string as an argument and displays it as a prompt. You can also specify the type of the input
by using the 'string', 'double' or 'integer' keywords.
For example, to ask the user for a 3x3 matrix, you can use the following code:
// Loop through each element and prompt the user to input values
for i = 1:m
for j = 1:n
end
end
You can repeat the same process for another matrix, say B, and then display the result of any
operation you want.
// Add the following program at the end of the existing program, and when done, run and
save the file.
// create a matrix B with the same dimensions as A
// Loop through each element and prompt the user to input values
for i = 1:m
for j = 1:n
end
end
disp(A - B)
disp(A * B)
This laboratory activity ends here. Perform the following lab exercises using the lecture on
SCILAB above. You may use SCILAB Notes or use the command console for the execution of this
exercises.
If you choose to place it in SCINOTES, you may combine the exercises from numbers 1-5, ‘a’
to ‘j’ into a single file and save it as lab_exercises2. Go to command console and copy and save the
output of this lab_exercises2 in a document file, and label it as lab_exercises2_output.
If you choose to do the activity in the command console, go to command console and copy
and save all the commands and output of this lab exercises in a document file, and label it as
lab_exercises2_output.
Lab Exercises
Matrix operations are fundamental concepts in linear algebra with wide applications in engineering,
physics, computer science, economics, and more. In this lecture, we will explore key matrix
operations and concepts such as minors, cofactors, adjoints, transpose, determinants, rank, and
inverse of a matrix.
Minors of a Matrix
Definition:
The minor of an element in a matrix is the determinant of the submatrix formed by deleting the row
and column of that element.
Example:
The minor of element a11(which is 2) is obtained by deleting the first row and first column:
The minor of element a12(which is 3) is obtained by deleting the first row and second column
The minor of element a21(which is 4) is obtained by deleting the second row and first column
Cofactors of a Matrix
Definition:
Example:
Transpose of a Matrix
Definition:
Example:
For matrix A:
Adjoint of a Matrix
Definition:
The adjoint (or adjugate) of a matrix is the transpose of the cofactor matrix.
Steps:
Example:
Definition:
The determinant of a matrix is a scalar value that provides important properties about the matrix,
such as whether it is invertible.
Steps:
Example:
For matrix A:
Determinants of a Matrix using Gaussian Elimination Method
Definition:
This method reduces the matrix to upper triangular form using row operations, and the determinant
is the product of the diagonal elements.
Steps:
Example:
For matrix A:
2. Determinant
Rank of a Matrix
Definition:
The rank of a matrix is the maximum number of linearly independent rows or columns.
Steps:
1. Use row reduction (Gaussian elimination) to bring the matrix to row echelon form.
Example:
For matrix C:
Definition:
The inverse of a matrix A is a matrix A-1 such that A*A-1 = I, where I is the identity matrix.
Steps:
Example:
Objective:
This lab exercise will guide you through performing matrix operations in Scilab, focusing on
calculating minors, cofactors, adjoint, transpose, determinants, rank, and inverse of matrices. By
the end of the exercise, you should be able to implement these operations using Scilab.
Required Software:
1. Open Scilab:
In the Scilab console, go to `File > New > Script` to open the script editor.
Task:
Task:
Task:
Task:
Task:
Task:
Task:
Task:
Task:
Given a 3x3 matrix `B`, perform all the operations learned above.
Part 12: Submission
3. Submit: Upload your `.sce` file and results to the course submission portal.
Conclusion:
This lab exercise provided hands-on experience with matrix operations in Scilab. You explored how
to calculate minors, cofactors, adjoint, transpose, determinants, rank, and the inverse of matrices.
Practicing these operations will deepen your understanding of matrix algebra and its applications.
Solutions of Simultaneous Linear Equations
1. Elimination Method:
This is a process of eliminating one variable from a system of equations to solve for the other.
Here's the step-by-step process:
Example:
Step-by-Step Solution:
Step 1: Align both equations. Make the coefficients of one variable (e.g., \( x \)) the same in
both equations by multiplying equation (1) by 2:
Step 3: Substitute into one of the original equations, say equation (1):
2. Cramer's Rule:
Cramer's Rule applies to systems of linear equations where the number of equations matches the
number of unknowns. It uses determinants to find the solution.
Example:
Step-by-Step Solution:
Step 3: Find by replacing the first column of the coefficient matrix with the
constant column:
Step 4: Find by replacing the second column of the coefficient matrix with the
constant column:
3. Gaussian Elimination:
This method transforms the system into an upper triangular matrix, making it easier to solve the
system through back substitution.
Example:
Step-by-Step Solution:
Step 2: Use row operations to create an upper triangular matrix. We can eliminate
from the second equation by performing .
System of Equations:
1. Elimination Method
The goal of the elimination method is to eliminate one variable at a time, reducing the system of
three equations to two, and eventually to one.
Step-by-Step Solution:
1. Start with the system:
Simplifying:
7. Substitute into the expressions for x and y:
2. Cramer's Rule
Step-by-Step Solution:
3. Find by replacing the second column of the coefficient matrix with the constants on
the right-hand side:
4. Find by replacing the third column of the coefficient matrix with the constants on the
right-hand side:
3. Gaussian Elimination
We apply Gaussian elimination to convert the system into upper triangular form and then use back
substitution.
Step-by-Step Solution:
Row 2:
Row 3:
Row 3:
4. Back substitution:
Summary:
Gaussian Elimination uses row operations to reduce the system to an upper triangular
matrix for back substitution.
Example:
Step-by-Step Solution:
Step 1: Write the augmented matrix:
Step 2: Use row operations to reduce the matrix to echelon form (RREF). We can
eliminate from the second equation by performing:
Now, the system is in reduced row echelon form (RREF), and the solution is:
Example:
System of Equations:
Step-by-Step Solution:
2. Use row operations to reduce the matrix to echelon form (RREF). Eliminate the -
terms from the second and third rows:
Row 2:
Row 3:
Row 3:
5. Use row operations to reduce the matrix to echelon form (RREF). Eliminate the -
terms from the first and second rows:
6. Reduce the diagonal of R2 to 1.
Now that the solution is in reduced row echelon form, the solution is:
Laboratory Exercise: Simultaneous Linear Equations Using Scilab
Objective:
This lab exercise will guide you through performing solutions of simultaneous linear equations in
Scilab, focusing on elimination method, Cramer’s Rule, Gaussian Elimination and Gauss-Jordan
Elimination Method. By the end of the exercise, you should be able to implement these operations
using Scilab.
Required Software:
1. Open Scilab:
In the Scilab console, go to `File > New > Script` to open the script editor.
In the elimination method, we manually eliminate variables by combining equations to reduce the
number of unknowns.
System of Equations:
NOTE: Save file as ‘eliminationmethod’
Cramer's Rule solves the system by computing determinants. We manually compute the
determinants of the matrices.
System of Equations:
NOTE: Save file as ‘cramersrule’
Gaussian elimination involves converting the matrix into upper triangular form and then back
substitution.
System of Equations:
NOTE: Save file as ‘gaussianelimination’
Gauss-Jordan elimination involves converting the matrix into reduced row-echelon form (RREF).
System of Equations:
NOTE: Save file as ‘gaussjordan’
Problem 1.
Problem 2