0% found this document useful (0 votes)
30 views7 pages

EXCERSICE

The document outlines a series of computational exercises using MATLAB, including plotting mathematical functions, performing arithmetic and matrix operations, solving equations, and generating random numbers. Each exercise is accompanied by sample code demonstrating the implementation of the tasks. Additionally, it includes exercises for creating specific matrices and plotting various signal types like sine and square waves.

Uploaded by

sindudeepika347
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

EXCERSICE

The document outlines a series of computational exercises using MATLAB, including plotting mathematical functions, performing arithmetic and matrix operations, solving equations, and generating random numbers. Each exercise is accompanied by sample code demonstrating the implementation of the tasks. Additionally, it includes exercises for creating specific matrices and plotting various signal types like sine and square waves.

Uploaded by

sindudeepika347
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Computational Lab- Exercise

1. Plotting a graph: Plot a simple graph of a mathematical function such as sine, cosine
or exponential function.

2. Basic arithmetic operations: Perform basic arithmetic operations such as addition,


subtraction, multiplication and division.

3. Matrix operations: Perform matrix operations such as addition, subtraction,


multiplication, and inversion.

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.

6. Generating random numbers: Generate random numbers using MATLAB's built-in


functions.

1.
% Define the range of x values to plot
x = -pi:0.01:pi;

% Calculate the corresponding y values for the sine function


y = sin(x);

% Plot the sine function


plot(x, y);

% Add title and axis labels


title('Sine Function');
xlabel('x');
ylabel('y');

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;

% Display the results


disp(['Addition: ', num2str(addition)]);
disp(['Subtraction: ', num2str(subtraction)]);
disp(['Multiplication: ', num2str(multiplication)]);
disp(['Division: ', num2str(division)]);

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];

% Perform matrix addition


addition = A + B;

% Perform matrix subtraction


subtraction = A - B;

% Perform matrix multiplication


multiplication = A * B;

% Perform matrix inversion


inverse = inv(A);

% Display the results


disp('Matrix Addition:');
disp(addition);
disp('Matrix Subtraction:');
disp(subtraction);
disp('Matrix Multiplication:');
disp(multiplication);
disp('Matrix Inverse:');
disp(inverse);

4.
% Define two vectors u and v
u = [1 2 3];
v = [4 5 6];

% Perform dot product


dot_product = dot(u, v);

% Perform cross product


cross_product = cross(u, v);

% Perform normalization
u_norm = u / norm(u);

% Display the results


disp(['Dot Product: ', num2str(dot_product)]);
disp('Cross Product:');
disp(cross_product);
disp('Normalized Vector:');
disp(u_norm);

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;

% Solve the equation


sol = solve(eqn, x);

% Display the solution


disp(['The solutions are: ', num2str(sol)]);
This code defines an equation x^2 - 5*x + 6 = 0 using the syms function, which creates a
symbolic variable x.
The solve function is then used to solve the equation for x. The first argument of the solve
function is the equation to solve, and the second argument is the variable to solve for.
The solution is stored in the sol variable.
Finally, the disp function is used to display the solution in the command window.

6.
% Generate a random scalar between 0 and 1
rand_num = rand();

% Generate a random vector with 5 elements between 0 and 1


rand_vec = rand(1, 5);

% Generate a random matrix with 3 rows and 4 columns between 0 and 1


rand_mat = rand(3, 4);

% Generate a random scalar between -5 and 5


rand_num_range = -5 + 10*rand();

% Generate a random integer between 1 and 10


rand_int = randi([1, 10]);

% Display the results


disp(['Random Scalar between 0 and 1: ', num2str(rand_num)]);
disp('Random Vector between 0 and 1:');
disp(rand_vec);
disp('Random Matrix between 0 and 1:');
disp(rand_mat);
disp(['Random Scalar between -5 and 5: ', num2str(rand_num_range)]);
disp(['Random Integer between 1 and 10: ', num2str(rand_int)]);

This code generates random numbers using MATLAB's built-in functions.


The rand function is used to generate random numbers between 0 and 1. The rand_vec
variable is a 1-by-5 vector with five random numbers. The rand_mat variable is a 3-by-4
matrix with 12 random numbers.
The randi function is used to generate a random integer between 1 and 10. The
rand_num_range variable is a random scalar between -5 and 5.
Finally, the disp function is used to display the results in the command window.
Exercise 1: Create a matrix with dimensions 3-by-3, where the elements on the diagonal are
1, and the elements elsewhere are 0.

% Create a 3-by-3 identity matrix

A = eye(3)

Exercise 2: Create a matrix with dimensions 2-by-3, where each element is equal to 5.

% Create a 2-by-3 matrix with all elements equal to 5

B = ones(2,3)*5

Exercise 3: Add two matrices A and B with dimensions 3-by-3.

% Define matrices A and B

A = [1 2 3; 4 5 6; 7 8 9]; B = [9 8 7; 6 5 4; 3 2 1];

% Add matrices A and B

C=A+B

Exercise 4: Subtract two matrices A and B with dimensions 2-by-2.

% Define matrices A and B

A = [1 2; 3 4]; B = [4 3; 2 1];

% Subtract matrices A and B

C=A-B

Exercise 5: Multiply two matrices A and B with dimensions 2-by-3 and 3-by-2, respectively.

% Define matrices A and B

A = [1 2 3; 4 5 6]; B = [7 8; 9 10; 11 12];

% Multiply matrices A and B

C = A*B

Exercise 6: Invert a matrix A with dimensions 3-by-3.

% 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];

% Find the determinant of matrix A

detA = det(A)

Exercise 1: Plot a simple sine wave with a frequency of 1 Hz and a duration of 1 second.

% Define time vector

t = linspace(0, 1, 1000);

% Define sine wave

y = sin(2*pi*1*t);

% Plot sine wave

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.

% Define time vector

t = linspace(0, 1, 1000);

% Define sine waves

y1 = sin(2*pi*1*t);

y2 = sin(2*pi*2*t);

% Plot sine waves

plot(t, y1, t, y2);

xlabel('Time (s)');

ylabel('Amplitude');

title('Two Sine Waves');

legend('1 Hz', '2 Hz');


Exercise 3: Plot a step function with a duration of 1 second.

% Define time vector

t = linspace(0, 1, 1000);

% Define step function

y = ones(size(t));

y(t < 0.5) = 0;

% Plot step function

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.

% Define time vector

t = linspace(0, 1, 1000);

% Define square wave

y = square(2*pi*1*t, 50);

% Plot square wave

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.

You might also like