















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A brief introduction to MATLAB programming, focusing on core operations and graphing. It features exercises covering calculations, matrix manipulation, and 2D plotting. Designed to help students apply MATLAB to mathematical and engineering problems, it offers examples and step-by-step solutions. Topics include MATLAB syntax, matrix operations, special matrices, and 2D graphing, providing a hands-on learning approach. Exercises with solutions reinforce understanding and application. The content builds a foundation in MATLAB, suitable for beginners and those enhancing scientific computing skills. It emphasizes practical application, making it valuable for students and professionals. Exercises cover calculations, matrix operations, and graphing, providing a MATLAB capabilities overview.
Typology: Exercises
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















DIAG - DIAGONAL MATRIX
TOEPLITZ - MATRIX WITH CONSTANT DIAGONALS
TRIU - UPPER TRIANGULAR MATRIX
TRIL - LOWER TRIANGULAR MATRIX
RAND - MATRIX WITH RANDOM ELEMENTS IN [0, 1)
LINSPACE - EQUALLY SPACED VECTOR
CAT - CONCATENATE MATRICES
🧮 1. Basic Plotting: plot(Y) → Plots vector Y against its index (X = 1:N). plot(X, Y) → Plots Y vs. X (both must be same length). Multiple Lines : plot(X1,Y1,X2,Y2,...) 🧮 2. Line Styles, Symbols, and Colors Line Style Symbo l Description '-'. Solid line '--' '+' Dashed line ':' '*' Dotted line '-. ' 'x' Dash-dot line Colors : 'r' = red, 'g' = green, 'b' = blue, 'k' = black, etc. Plot with style : plot(x, y, '--rs', ' LineWidth ', 2, ' MarkerEdgeColor ', 'k', ' MarkerFaceColor ', 'g', ' MarkerSize ', 10) Argument Meaning '--rs' A red dashed line (--) with square markers (s) 'LineWidth', 2 Makes the line 2 points thick 'MarkerEdgeColor', Sets the border color of the square marker to
'k' black ('k') 'MarkerFaceColor', 'g' Fills the inside of the marker with green ('g') 'MarkerSize', 10 Sets the size of the markers to 10 (larger than default size) 🛠️ 3. Editing the Graph Function Description grid on / off Show or hide grid box on / off Show or hide border xlabel('text') Label X-axis ylabel('text') Label Y-axis title('text') Add title text(x, y, 'txt') Place text at (x, y) gtext('text') Click to place text interactively axis([xmin xmax, ymin ymax]) Set axis scale set(gca, 'xtick', [...]) Change the number of marker points 🧮 4. Multiple Plots 🧮 subplot(m, n, p) → Divide figure into m×n grid and activate the p-th subplot. 🧮 hold on / hold off → Keep old plots when adding new ones.
Solution
CUSTOM INPUT % MATLAB script to compute x, y, and z with user input % User input for x, y, and z x = input('Enter x: '); y = input('Enter y: '); z = input('Enter z: '); % Display results fprintf('Computed values:\n'); fprintf('x = %.6f\n', x); fprintf('y = %.6f\n', y);
disp('Matrix A:'); disp(A); disp('Matrix B:'); disp(B); % Part (b): Determine the inverse of A and B if possible if rowsA == colsA && det(A) ~= 0 A_inv = inv(A); disp('Inverse of A:'); disp(A_inv); else disp('Matrix A is not invertible.'); end if rowsB == colsB && det(B) ~= 0 B_inv = inv(B); disp('Inverse of B:'); disp(B_inv); else disp('Matrix B is not invertible.'); end % Part (c): Compute the sum of A and B (only if same dimensions) if rowsA == rowsB && colsA == colsB sumAB = A + B; disp('Sum of A and B:'); disp(sumAB); else disp('Cannot add A and B: Dimensions do not match.'); end % Part (d): Compute the product of A and B (if dimensions are compatible) if colsA == rowsB prodAB = A * B; disp('Product of A and B:'); disp(prodAB); else disp('Cannot multiply A and B: Incompatible dimensions.'); end
% Display results fprintf('x = %.6f\n', ans);
CUSTOM INPUT % Part (a): Input Matrices A and B rowsA = input('Enter the number of rows for matrix A: '); colsA = input('Enter the number of columns for matrix A: '); A = zeros(rowsA, colsA); disp('Enter elements for matrix A:'); for i = 1:rowsA for j = 1:colsA A(i, j) = input(sprintf('A(%d,%d): ', i, j)); end end rowsB = input('Enter the number of rows for matrix B: '); colsB = input('Enter the number of columns for matrix B: '); B = zeros(rowsB, colsB); disp('Enter elements for matrix B:'); for i = 1:rowsB for j = 1:colsB B(i, j) = input(sprintf('B(%d,%d): ', i, j)); end end % Display matrices disp('Matrix A:'); disp(A); disp('Matrix B:'); disp(B); % Solve the system of equations using the backslash operator solution = A \ B; % Extract the values of x, y, z x = solution(1); y = solution(2); z = solution(3); % Display the results fprintf('The solution to the system of equations is:\n'); fprintf('x = %.0f\n', x); fprintf('y = %.0f\n', y); fprintf('z = %.0f\n', z);