MATLAB Manual
For I Semester BE Students
ACADEMIC YEAR 2024-25
Prepared by:
Department of Mathematics
Vidyavardhaka College of Engineering
Mysuru
VVCE, Mysuru Page 1
Contents
Experiment No. Title of the Experiment Page No.
Open ended experiment 01 Solving system of Linear equations (For all)
Euclid Algorithm (CSE Stream)
Open ended experiment 02
Newton-Raphson Method (ME, CIVIL, EC Stream)
VVCE, Mysuru Page 2
Open ended experiment 01
Solving system of Linear equations
Illustration 1
Consider the following system of linear equations:
2 x+ 2 y +2 z=2 ;
x + y +3 z=5 ;
x +4 y+ z =10 ;
Create the coefficient matrix containing all the coefficients for x , y , and z . Name this
matrix A.
A= [2 2 2; 1 1 3; 1 4 1]
Assemble the constants on the right side of the equations into a column vector namedb .
b= [2 5 10]'
Find the determinant of A.
det(A)
Solve the system of linear equation
x=inv(A)*b or x=A\b (using the backslash operator)
Task 1
Consider the following system of linear equations:
x− y =2
x + y=4
2 x+ y=8
Make a 3-by-2 matrix containing all the coefficients. Name this matrix A.
Assemble the constants on the right side of the equations into a column vector namedb .
Solve for x using the command A\b. Assign the output to a variable point.
Task 2
Consider the following system of linear equations:
2 x+ y−z=5 , 3 x + 4 y + z=−2
Create the coefficient matrix A for the system.
Create the constant vector b.
Find the determinant of A.
Find a solution to the system of equations using the backslash operator.
VVCE, Mysuru Page 3
Task 3
(Solve System of Linear Equations Using linsolve)
Consider the following system of linear equations:
2 x+ y+ z =2
−x + y−z=3
x +2 y+ 3 z=−10
Declare the system of equations.
syms x y z
eqn1 = 2*x + y + z == 2;
eqn2 = -x + y - z == 3;
eqn3 = x + 2*y + 3*z == -10;
Use equationsToMatrix to convert the equations into the form AX=B. The second input
to equationsToMatrix specifies the independent variables in the equations.
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [x, y, z])
Use linsolve to solve AX=B for the vector of unknowns X .
X = linsolve(A,B)
Find a solution to the system of equations using the backslash operator.
Task 4
Solve System of Linear Equations Using solve
Consider the following system of linear equations:
2 x+ y+ z =2
−x + y−z=3
x +2 y+ 3 z=−10
Declare the system of equations.
syms x y z
eqn1 = 2*x + y + z == 2;
eqn2 = -x + y - z == 3;
eqn3 = x + 2*y + 3*z == -10;
Solve the system of equations using solve.
sol = solve([eqn1, eqn2, eqn3], [x, y, z]);
xSol = sol.x
ySol = sol.y
zSol = sol.z
VVCE, Mysuru Page 4
Task 5
Reduced row echelon form (Gauss-Jordan elimination process)
Consider the following system of linear equations:
3 x+ 2 y + z=9, y=10 , x +2 y=2
Create the coefficient matrix A for the system.
>> A = [3 2 1; 0 1 0; 1 2 0];
Create the constant vector b.
>> b = [1:3]';
Write the augmented matrix using A and b.
>> Aug = [A b]
Aug =
3 2 1 1
0 1 0 2
1 2 0 3
Perform Row reduction using rref
>> B=rref(Aug)
B=
1 0 0 -1
0 1 0 2
0 0 1 0
The solution is simply the last column of the previous matrix.
>> Solution=B(:,end)
Task 6
Reduced row echelon form
( )
3 2 1
A. Find the inverse of a matrix 0 1 0 using rref
1 2 0
>> R=rref([A eye(size(A))])
R=
1 0 0 0 -2 1
0 1 0 0 1 0
0 0 1 1 4 -3
>> inv_A = R(:,4:end)
B. Verify the above by finding the inverse of A using the inv function
>> inv(A)
ans =
0 -2 1
0 1 0
1 4 -3
VVCE, Mysuru Page 5
Exercise
1) Solve the following system of Linear Equations:
7 x + y +8 z=9
4 x+5 y +8 z=10
10 x+ 4 y +2 z=2
2) Solve the following system of Linear Equations:
3 a+2 b−c=1
4 c +2 a+1=2 b
a+ c=0.5
*********
VVCE, Mysuru Page 6
Open ended experiment 02 (CSE Stream)
Euclid’s Algorithm:
If a , b ∈ Z , b>0 , then there exist unique q ,r ∈ Z such that a=qb+ r ,0 ≤ r <b . Here q is called
quotient of the integer division of a by b , and r is called remainder.
MATLAB CODE:
a1=input('enter the first number\n');
b1=input('enter the second number\n');
a=a1;
b=b1;
i=1;
r(i)=rem(a,b);
if r(i)==0
fprintf('GCD of %d and %d is : %d\n',a1,b1,b)
else
while(r(i)~=0)
a=b;
b=r(i);
i=i+1;
r(i)=rem(a,b);
end
fprintf('GCD of %d and %d is : %d\n',a1,b1,r(i-1))
end
OUTPUT
GCD_Euclidalgorithm
enter the first number
4578
enter the second number
50789
GCD of 4578 and 50789 is : 1
Linear congruence
Let d=(a ,m), and consider the equation ax ≡b (mod m).
(a) If d 6 | b, there are no solutions.
(b) If d | b, there are exactly d distinct solutions mod m.
MATLAB CODE
%Linear congruence
VVCE, Mysuru Page 7
%Consider ax=b(mod m)
a=input('enter integer a ');% 7
b=input('enter integer b ');%9
m=input('enter integer m ');%15
d=gcd(a,m);
if rem(b,d)~=0
disp('the congruence has no integer solution');
else
for i=1:m-1
x=(m/a)*i+(b/a);
if floor(x)==ceil(x)% check whether x is an integer
disp(x);
break
end
end
end
OUTPUT
linearcongruences
enter integer a
35
enter integer b
47
enter integer m
7
the congruence has no integer solution
*******
VVCE, Mysuru Page 8
Open ended experiment 02 (ME, CIVIL, EC Stream)
Newton-Raphson Method of finding roots
MATLAB Code
function []= NRM_Btech(x0,f,df)
x(1) = x0;
Maxit = 100;
Tol = 10^(-6);
for i = 1:Maxit
if abs(df(x(i)))<10^(-10)
fprintf('The derivative is zero at x = %f\n',x(i));
break
end
x(i+1) = x(i)-f(x(i))/df(x(i));
if abs(x(i+1)-x(i))<Tol
fprintf('The approximate root of the given equation
after %d iteration is %f\n',i,x(i+1));
break
end
fprintf('The value of x at %d iteration is x(%d)=%f\
n',i,i,x(i));
plot(x(i),f(x(i)),'*g')
hold on
end
t = -5:0.001:5;
y = f(t);
plot(t,y,'r',x(i+1),0,'*')
end
Problem 01
1. Find a real root of the equation f ( x )=x e x −2
2. Find a real root of the equation f ( x )=10 x + x−4
3. Find a real root of the equation f ( x )=xe x −cos x
********
VVCE, Mysuru Page 9