Numerical Methods Project
Numerical Methods Project
Submission NOTE:
1- Write the problem and code script, its final results/solution/iteration table on the Word
file for each problem as per the provided format.
2- Prepare a program for each problem as a separate Matlab script file.
3- Zip all the program files, Excel, and this Word file, and upload them to Moodle as a
onezipped folder.
Problem-1:
Write the given data in an Excel file and save it as Read_Excel.xlsx. Write a code to read the
data from the Excel file, and store it in matrix variables. Store the header in one array variable
and data in another array variable. Rewrite the stored array variables in another Excel file with
the name Write_Excel.xlsx, and in the same format as in the read file.
Solution 1:
clear
clc
Note: The program will create an excel file and in case a file with the same
name exists it will overwrite the old file, and we can check by noticing the
time of save of the write file.
Snapshots:
Problem-2:
% Initial guesses
xl = 0; % Lower bound
xu = 2; % Upper bound
% Evaluate function at xm
fm = f(xm);
3. fixed-point method
% Define the function f(x)
f = @(x) 4*x^3 - 11*x^2 + 15*x - 5;
% Initial guess
x0 = 1.5;
% Initialize variables
x_prev = x0; % Previous value of x
iteration = 0; % Counter for iterations
4.Secant method
% Define the function f(x)
f = @(x) 4*x.^3 - 11*x.^2 + 15*x - 5;
Problem-3:
Write a Matlab program to solve the following set of equations
x + y - 2z = -3
6x + 2y + 2z =3
-3x + 5y + z = 1
Using:
(a) Gauss-elimination with partial pivoting (filename: GEWPP.m)
(b) Lower upper decomposition method (filename: LUDM.m)
(c) Gauss-Seidel Method (filename: GSM.m)
Code:
1. Gauss-elimination
A = [1, 1, -2; 6, 2, 2; -3, 5, 1];
b = [-3; 3; 1];
Ab = [A b];
n = length(b);
for i=1:n-1
for j=i+1:n
if abs(A(j,i))>abs(A(i,i))
T=A(j,:);
A(j,:)=A(i,:);
A(i,:)=T;
end
end
end
transpose(a)
2. Lower upper
% L-U Decomposition Example 10.2
clear;
clc;
% Define matrix A
A = [1, 1, -2; 6, 2, 2; -3, 5, 1];
% Define matrix b
b = [-3; 3; 1];
% Initialize U with A
U = A;
disp('Solution (x):');
disp(x);
3. Gauss seidel
3. Gauss seidel
Problem-4:
Write a program to find the inverse of the coefficient matrix using lower-upper decomposition
method for the following set of equations: (Matlab filename: LUDINV.m)
10x + 2y - z = 29
-3x - 6y + 2z = -61
x + y + 5z = -21
Note: Matlab has a direct function to find the inverse of a matrix, you should not use that rather
write a program as per the method taught in Chapter 10 using the concept of lower-upper
decomposition.
Code:
clear
clc
A=[10 2 -1; -3 -6 2; 1 1 5]
b=[29; -61; -21]
f21=A(2,1)/A(1,1);
A(2,:)=A(2,:)-A(1,:)*f21;
f31=A(3,1)/A(1,1);
A(3,:)=A(3,:)-A(1,:)*f31;
f32=A(3,2)/A(2,2);
A(3,:)=A(3,:)-A(2,:)*f32;
U=A;
L=eye(3);
L(2,1)=f21;
L(3,1)=f31;
L(3,2)=f32;
I=eye(3);
d1=L\I(:,1);
d2=L\I(:,2);
d3=L\I(:,3);
x(:,1)=U\d1;
x(:,2)=U\d2;
x(:,3)=U\d3;
x
snapshoots:
Results:
Problem-5:
Write a Matlab program to solve the following multiple regression problem to fit the given data
using multiple linear regression. (Matlab filename: MLRM.m)
X1 X2 Y
0.1 0.1 5
2.1 1 10
2.5 2 9
1.1 3.5 0.1
4 6.3 3
7 2.1 27
Code:
clc
clear
n=length(x1);
sum(y);
sum(x1);
sum(x2);
sum(x1.^2);
sum(x2.^2);
sum(x1.*x2);
sum(x1.*y);
sum(x2.*y);
snapshoots:
Results:
Problem-6:
Write a Matlab program to solve the following problem:
Determine the resistance at 33oC using the Lagrange interpolation method of third-order
polynomial. (Matlab filename: LIM3ORD.m)
R (ohm) T (oC)
1111 25.11
910 30.13
636 40.12
455 50.13
Code:
clc
clear
for i=1:n
a=1;
for j=1:n
if j~=i
a=a*(Tgiven-T(j))/(T(i)-T(j));
end
end
sum=sum+a*R(i);
end
fprintf('The resistance at temperature 35 is:%.2f\n',sum);
Snapshots of results:
Problem-7:
Write a Matlab program to evaluate the integral of the following function using Simpson’s 3/8
rule: (Matlab filename: SIMP1BY3.m)
Code:
clc
clear
f=@(x) 1-x-4*x.^3+2*x.^5;
a=-2; b=4;
n=3;
h=(b-a)/n;
x=[a,a+h,a+2*h,b];
y=f(x);
I=(3*h/8)*(y(1)+3*y(2)+3*y(3)+y(4));
fprintf('The integration by using Simpsons 3/8 rule is %.0f',I);
Snapshots of results: