MATLAB Assignment
MATLAB Assignment
the value of A.
a. (3+4)/(5+6)
b. 2π2
c. √2
Soln.
a = (3+4)/(5+6)
fprintf("%d",a)
a = 2*(3.14 * 3.14)
fprintf("%d", a)
a = sqrt(2)
fprintf("%d", a)
and adding 32. Assign a variable called C the value 37, and
implement this formula to
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)
N = [0.6,1.2,0.5,1.3]
X = [3,2,1,5]
bill = sum(N .* X)
fprintf("%d", bill)
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)
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];
% 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