EXCERSICE
EXCERSICE
1. Plotting a graph: Plot a simple graph of a mathematical function such as sine, cosine
or exponential function.
4. Vector operations: Perform vector operations such as dot product, cross product and
normalization.
5. Solving equations: Solve simple equations using MATLAB's built-in equation solver.
1.
% Define the range of x values to plot
x = -pi:0.01:pi;
This code first defines the range of x values to plot using the : operator. In this case, we are
plotting the sine function over the range [-pi, pi] with a step size of 0.01.
Then, the corresponding y values for the sine function are calculated using the sin function in
MATLAB.
Finally, the plot function is used to plot the sine function with the x values on the horizontal
axis and the y values on the vertical axis. The title, xlabel, and ylabel functions are used to
add a title and axis labels to the plot.
You can similarly plot other mathematical functions such as cosine or exponential functions
by modifying the y variable to be equal to cos(x) or exp(x), respectively.
2.
% Define two variables a and b
a = 5;
b = 2;
% Perform addition
addition = a + b;
% Perform subtraction
subtraction = a - b;
% Perform multiplication
multiplication = a * b;
% Perform division
division = a / b;
This code defines two variables a and b and performs basic arithmetic operations such as
addition, subtraction, multiplication, and division on these variables. The results are stored in
separate variables addition, subtraction, multiplication, and division, respectively.
The disp function is then used to display the results in the command window.
You can modify the values of a and b to perform the operations on different numbers.
3.
% Define two matrices A and B
A = [1 2; 3 4];
B = [5 6; 7 8];
4.
% Define two vectors u and v
u = [1 2 3];
v = [4 5 6];
% Perform normalization
u_norm = u / norm(u);
This code defines two vectors u and v and performs vector operations such as dot product,
cross product, and normalization on these vectors.
The dot function is used to calculate the dot product of u and v. The cross function is used to
calculate the cross product of u and v. The norm function is used to calculate the norm of u
and then the normalization of u is performed by dividing it by its norm.
The results are stored in separate variables dot_product, cross_product, and u_norm,
respectively.
The disp function is then used to display the results in the command window.
5.
% Define the equation to solve
syms x
eqn = x^2 - 5*x + 6 == 0;
6.
% Generate a random scalar between 0 and 1
rand_num = rand();
A = eye(3)
Exercise 2: Create a matrix with dimensions 2-by-3, where each element is equal to 5.
B = ones(2,3)*5
A = [1 2 3; 4 5 6; 7 8 9]; B = [9 8 7; 6 5 4; 3 2 1];
C=A+B
A = [1 2; 3 4]; B = [4 3; 2 1];
C=A-B
Exercise 5: Multiply two matrices A and B with dimensions 2-by-3 and 3-by-2, respectively.
C = A*B
% Define matrix A
A = [1 2 3; 4 5 6; 7 8 9];
% Invert matrix A
B = inv(A)
Exercise 7: Find the determinant of a matrix A with dimensions 2-by-2.
% Define matrix A
A = [1 2; 3 4];
detA = det(A)
Exercise 1: Plot a simple sine wave with a frequency of 1 Hz and a duration of 1 second.
t = linspace(0, 1, 1000);
y = sin(2*pi*1*t);
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sine Wave');
Exercise 2: Plot two sine waves with frequencies of 1 Hz and 2 Hz, respectively, and a
duration of 1 second.
t = linspace(0, 1, 1000);
y1 = sin(2*pi*1*t);
y2 = sin(2*pi*2*t);
xlabel('Time (s)');
ylabel('Amplitude');
t = linspace(0, 1, 1000);
y = ones(size(t));
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Step Function');
Exercise 4: Plot a square wave with a frequency of 1 Hz and a duty cycle of 50%, and a
duration of 1 second.
t = linspace(0, 1, 1000);
y = square(2*pi*1*t, 50);
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Square Wave');
These exercises cover basic signal plotting in MATLAB, such as plotting sine waves, step
functions, and square waves. The MATLAB functions used include linspace to create a time
vector, sin to define a sine wave, ones to create a step function, and square to create a
square wave.