MATLAB PRACTICAL
1) Write a program to assign the following expressions to a variable
A and then to print out
the value of A.
a. (3+4)/(5+6)
b. 2π2
c. √2
d.(0.0000123 + 5.67×10-3)× 0.4567×10-4
Soln.
a = (3+4)/(5+6)
fprintf("%d",a)
a = 2*(3.14 * 3.14)
fprintf("%d", a)
a = sqrt(2)
fprintf("%d", a)
a = (0.0000123 + 5.67 * 10^(-3)) * 0.4567 * 10^(-4)
fprintf("%d", a)
2) Celsius temperatures can be converted to Fahrenheit by
multiplying by 9, dividing by 5,
and adding 32. Assign a variable called C the value 37, and
implement this formula to
assign a variable F the Fahrenheit equivalent of 37 Celsius.
Soln.
C = 37
F = (C * 9/5) + 32
fprintf("%d %d", C,F)
3) Set up a vector called N with five elements having the values: 1,
2, 3, 4, 5. Using N,
create assignment statements for a vector X which will result in X
having these values:
a. 2, 4, 6, 8, 10
b. 1/2, 1, 3/2, 2, 5/2
c. 1, 1/2, 1/3, 1/4, 1/5
d. 1, 1/4, 1/9, 1/16, 1/25
Soln.
N = [1,2,3,4,5]
X = 2*N
disp(X)
X = N/2
disp(X)
X = N.^-1
disp(X)
X = N.^-2
disp(X)
4) A supermarket conveyor belt holds an array of groceries. The
price of each product (in
pounds) is [ 0.6, 1.2 ,0.5, 1.3 ] ; while the numbers of each product
are [ 3, 2 ,1 ,5 ]. Use
MATLAB to calculate the total bill.
N = [0.6,1.2,0.5,1.3]
X = [3,2,1,5]
bill = sum(N .* X)
fprintf("%d", bill)
5) The sortrows(x) function will sort a vector or matrix X into
increasing row order. Use this
function to sort a list of names into alphabetical order
Soln.
names = {'Alice', 'Charlie', 'Bob', 'Eve', 'David'}
sorted = sort(names)
disp(sorted)
6) The ―identity‖matrix is a square matrix that has ones on the
diagonal and zeros
elsewhere. You can generate one with the eye() function in
MATLAB. Use MATLAB to
find a matrix B, such that when multiplied by matrix A=[ 1 2; -1 0 ]
the identity matrix
I=[ 1 0; 0 1 ] is generated. That is A*B=I.
Soln.
A = [1, 2; -1, 0]
B = inv(A)
disp(B)
7) Create an array of N numbers. Now find a single MATLAB
statement that picks out from
that array the 1,4,9,16,...,√Nth entries, i.e. those numbers which
have indices that are
square numbers.
Soln.
n = 25
array = 1:n
result = array(rem(1:n, sqrt(n)) == 0)
8) Draw a graph that joins the points (0,1), (4,3), (2,0) and (5,-2).
Soln.
% Define the coordinates of the points
x = [0, 4, 2, 5];
y = [1, 3, 0, -2];
% Plot the points and join them with lines
plot(x, y, '-o'); % '-o' specifies a line with markers at data points
% Set axis limits (optional, adjust as needed)
axis([-1, 6, -3, 4]);
% Add labels to the points
text(x, y, num2str([1, 2, 3, 4]'), 'VerticalAlignment', 'bottom',
'HorizontalAlignment', 'right');
% Add labels to the axes
xlabel('X-Axis');
ylabel('Y-Axis');
% Add a title to the graph
title('Graph Joining Points (0,1), (4,3), (2,0), and (5,-2)');
% Display the grid (optional)
grid on;
% Show the graph
hold off; % Turn off hold if used elsewhere in your script
9) The seeds on a sunflower are distributed according to the
formula below. Plot a small circle at each of the
first 1000 co-ordinates :
rn = √n
θn = (137.51/180)πn
Soln.
% Number of circles to plot
numCircles = 1000;
% Initialize arrays to store x and y coordinates of the circles
xCoordinates = zeros(1, numCircles);
yCoordinates = zeros(1, numCircles);
% Calculate and store the coordinates
for n = 1:numCircles
r = sqrt(n);
theta = (137.51/180) * pi * n;
xCoordinates(n) = r * cos(theta);
yCoordinates(n) = r * sin(theta);
end
% Create a scatter plot of the circles
scatter(xCoordinates, yCoordinates, 10, 'filled'); % Adjust marker size as needed
% Set axis limits (optional, adjust as needed)
axis equal; % Ensure equal aspect ratio for circles
% Add labels to the axes (optional)
xlabel('X-Axis');
ylabel('Y-Axis');
% Add a title to the plot (optional)
title('Circles at the First 1000 Coordinates');
% Display the plot
10) Calculate 10 approximate points from the function y=2x by using
the formulae:
i. xn= n
ii. yn= 2n + rand - 0.5
Fit a line of best fit to these points using the function polyfit() with
degree=1, and
generate co-ordinates from the line of best fit using polyval(). Use
the on-line help to find
out how to use these functions. Plot the raw data and the line of
best fit.
Soln.
% Step 1: Generate 10 approximate points with random noise
n = 1:10; % n values from 1 to 10
x = n; % x values (xn = n)
y = 2 * n + rand(1, 10) - 0.5; % y values (yn = 2n + random noise)
% Step 2: Fit a line of best fit to the points using polyfit
degree = 1; % Degree of the polynomial (a straight line)
coefficients = polyfit(x, y, degree);
% Step 3: Generate coordinates from the line of best fit using polyval
x_fit = 1:10; % Generate x values for the line of best fit
y_fit = polyval(coefficients, x_fit); % Calculate y values using polyval
% Step 4: Plot the raw data and the line of best fit
figure; % Create a new figure
plot(x, y, 'o', x_fit, y_fit, '-'); % Plot raw data points ('o') and the line of
best fit ('-')
legend('Raw Data', 'Line of Best Fit'); % Add legend
xlabel('x'); % X-axis label
ylabel('y'); % Y-axis label
title('Raw Data and Line of Best Fit'); % Title of the plot
grid on; % Display grid