Lab Report Title Page
Lab Report Title Page
Title :
Experiment :
Lab Number : 1
Submitted by:
Name Reg No
Muhammad Talha Zulfiqar 210401005
Marks:
Result &
Format Total
Discussion
Objectives:
The objective of this Task is to introduce the students to the concept of mathematical
programming using the software called MATLAB. We shall study how to define
variables, matrices etc, see how we can plot results and write simple MATLAB codes.
Software Required:
MATLAB
Introduction:
MATLAB, standing for "matrix laboratory," is powerful software known for its efficiency in
numerical analysis, matrix computation, and control system design. It provides an intuitive
platform where tasks are executed using familiar mathematical notation. MATLAB's
capabilities encompass math, algorithm development, modeling, data analysis, visualization,
and application development, making it indispensable for scientific and engineering tasks.
PROCEDURE:
Part a:
Generate a magic square matrix of size 6x6 using the magic function.
Display the fourth row of the matrix mat.
Part b:
Create a vector x ranging from 0 to 1.1 with an increment of 0.1 using the colon operator.
Create a vector y ranging from 10 to 21.
Multiply corresponding elements of vectors x and y and store the result in a vector named
multiply.
Divide corresponding elements of vector y by vector x and store the result in a vector named
divide.
Display the result of x multiplied by y.
Display the result of y divided by x.
Part c:
Generate a random matrix r of size 4x5 with integer values ranging from -8 to 9 using the randi
function.
Display the random matrix r.
CODE:
%% part a
mat=magic(6);
disp('The fourth row of the matrix mat');
display(mat(4,:))
%% part b
x=0:0.1:1.1;
y=10:21;
multiply=x.*y;
divide=y./x;
disp( '‘x’ multiply by ‘y’' )
display(multiply)
disp('‘y’ divides by ‘x’')
display(divide)
%% part c
r = randi([-8, 9], 4, 5);
disp('Random matrix ''r'' of size 4 by 5:');
display(r);
RESULT:
PART A:
PART B:
PART C:
Task 2:
Use MATLAB commands to get exactly as the figure shown below
x=pi/2:pi/10:2*pi;
y=sin(x);
z=cos(x);
PROCEDURE:
1. Define a vector x ranging from pi/2 to 2π with increments of pi/10 using the colon operator.
2. Calculate the sine values of the elements in vector x and store them in a vector y.
3. Calculate the cosine values of the elements in vector x and store them in a vector z.
4. Create a subplot with 2 rows and 1 column and activate the first subplot.
5. Plot y against x using blue plus markers connected by dotted lines (b+:):
6. Set the title as "Sine wave".
7. Label the x-axis as "angle".
8. Label the y-axis as "sine(x)".
9. Set the legend as "sine".
10. Set the axis limits to [1.5, 6.3] for the x-axis and [-1, 1] for the y-axis.
11. Create a subplot with 2 rows and 1 column and activate the second subplot.
12. Plot z against x using red plus markers connected by dashed lines (r+--):
13. Set the title as "Cosine wave".
14. Label the x-axis as "angle".
15. Label the y-axis as "cosine(x)".
16. Set the legend as "cosine".
17. Set the axis limits to [1.5, 6.3] for the x-axis and [-1, 1] for the y-axis.
CODE:
x=pi/2:pi/10:2*pi;
y=sin(x);
z=cos(x);
subplot(2,1,1)
plot(x,y,'b+:')
title('Sine wave')
xlabel('angle')
ylabel('sine(x)')
legend('sine')
axis ([1.5 6.3 -1 1])
subplot(2,1,2)
plot(x,z,'r+--')
title('Cosine wave')
xlabel('angle')
ylabel('cosine(x)')
legend('cosine')
axis ([1.5 6.3 -1 1])
RESULT:
Task 3:
Define Following Matrices in MATLAB
PROCEDURE:
Part a:
Define matrix A with specified values.
Define vector B with specified values.
Calculate the size of matrix A using the size function.
Display the size of matrix A.
Part b:
Define matrix A with specified values.
Define vector B with specified values.
Calculate the product of matrix A and vector B.
Display the result of the multiplication.
Part c:
Define matrix A with specified values.
Define vector B with specified values.
Extract elements of matrix A that are in the 2nd to 4th rows and 1st & 3rd columns.
Display the extracted elements.
Part d:
Define matrix A with specified values.
Define vector B with specified values.
Attempt to calculate the solution vector x using the formula x=A-1B, where A-1 denotes the
inverse of matrix A.
Since the inverse of matrix A is not possible (as indicated by the comment in the code), the
inverse operation cannot be performed.
CODE:
A = [1.2 3.04 7.3 3.2;-1 -12 1.2 5 ;2.7 1 -2 4; 6.6 4.51 0 1 ;-3
2.2 1 6 ];
B = [1.3;-1 ;2.5;1.6];
%% part a
A = [1.2 3.04 7.3 3.2;-1 -12 1.2 5 ;2.7 1 -2 4; 6.6 4.51 0 1 ;-3
2.2 1 6 ];
B = [1.3;-1 ;2.5;1.6];
size_A = size(A);
disp(size_A);
%% part b
A = [1.2 3.04 7.3 3.2;-1 -12 1.2 5 ;2.7 1 -2 4; 6.6 4.51 0 1 ;-3
2.2 1 6 ];
B = [1.3;-1 ;2.5;1.6];
AB = A * B;
disp('Multiplication of A and B')
disp(AB);
%% part c
A = [1.2 3.04 7.3 3.2;-1 -12 1.2 5 ;2.7 1 -2 4; 6.6 4.51 0 1 ;-3
2.2 1 6 ];
B = [1.3;-1 ;2.5;1.6];
disp('Elements of A that are in 2nd to 4th row and 1st & 3rd coloumn')
disp(A(2:4, [1, 3]))
%% part d
A = [1.2 3.04 7.3 3.2;-1 -12 1.2 5 ;2.7 1 -2 4; 6.6 4.51 0 1 ;-3
2.2 1 6 ];
B = [1.3;-1 ;2.5;1.6];
RESULT:
PART A:
PART B:
PART C:
PART D:
Solution not possible
Task 4:
Draw the graph of following function:
𝑧 = 𝑒−2𝑡 cos (3𝑡 + 1) − 𝜋 ≤ 𝑡 ≤ 5𝜋
a.
Force 𝑓 (pound) 0 100 200 300 400 500 600 700 800
PROCEDURE:
Part a:
Generate a vector t containing 1000 equally spaced points between −π and 5π using the linspace
Calculate the function z=e2t⋅cos(3t+1) for each value of t and store the results in the vector z.
function.
Create a new figure for plotting.
Plot z against t using green plus markers connected by solid lines (g+-).
Label the x-axis as "t" and the y-axis as "z".
Set the title of the plot as "Graph of z = e^{-2t} \cdot cos(3t + 1)".
Set the legend as "z".
Set the axis limits to [−3.2,15.9][−3.2,15.9] for the x-axis and [−300,160][−300,160] for the y-
axis.
Part b:
Define the force applied as a vector force containing values [0, 100, 200, 300, 400, 500, 600, 700,
800] in pounds.
Define the corresponding deflection values as a vector deflection containing values [0, 0.09, 0.18,
0.28, 0.37, 0.46, 0.55, 0.65, 0.74] in inches.
Create a new figure for plotting.
Plot deflection against force using blue circles connected by solid lines (bo-) with a line width of
2.
Label the x-axis as "Force (pound)" and the y-axis as "Deflection (inches)".
Set the title of the plot as "Deflection of a Cantilever Beam".
Set the legend as "deflection".
CODE:
%% part a
t = linspace(-pi, 5*pi, 1000);
z = exp(-2*t) .* cos(3*t + 1);
figure
plot(t, z,'g+-');
xlabel('t');
ylabel('z');
title('Graph of z = e^{-2t} \cdot cos(3t + 1)');
legend('z')
axis ([-3.2 15.9 -300 160])
%% part b
force = [0, 100, 200, 300, 400, 500, 600, 700, 800]; % Force in pounds
deflection = [0, 0.09, 0.18, 0.28, 0.37, 0.46, 0.55, 0.65, 0.74]; %
Deflection in inches
figure
plot(force, deflection, 'bo-', 'LineWidth', 2);
xlabel('Force (pound)');
ylabel('Deflection (inches)');
title('Deflection of a Cantilever Beam');
legend('deflection')
RESULT:
PART A:
PART B:
Draw the complete model of the dynamic system, and comments on the
results if.
a. Feedback is removed.
b. Gain increased.
c. Gain decreased.
PROCEDURE:
Apply the desired input signal to the system.
The Step1 block sums the input signal with the output from Controller1.
The summed signal is then passed through Gain1, which amplifies the signal by a factor of 10.
The amplified signal is then fed to Controller1.
Controller1 processes the signal and generates a control signal.
The control signal from Controller1 is applied to Plant1.
Plant1 represents the physical system that is being controlled. It takes the control signal as input
and produces an output signal.
The output signal from Plant1 is fed back to the Sum1 block.
The feedback signal is subtracted from the input signal to create an error signal.
The error signal is then used by Controller1 to adjust its control output in the following step.
Steps 2-10 are repeated continuously as long as the system is operating.
SIMULATION:
RESULT:
a. Feedback is removed.
When the feedback is removed the system becomes unstable as depicted by the linear model of the
graph
b. Gain increased.
When the gain is increase it results in fluctuations in the model resulting in larger steady
state time and less transient time
c. Gain decreased.
When the gain is decreased it results in a lower steady state time and larger transient time but has
lower fluctuations.
CONCLUSION:
In this lab we revised our concept of Matlab functions and basics, vector manipulation and signals and
data plotting. We also depicted how a closed looped system should work and what differs it from an open
looped system.