Docsity

MATLAB Programming Exercises: Matrix Operations and Graphing, Exercises of Mathematical Methods

Partial preview of the text

Download MATLAB Programming Exercises: Matrix Operations and Graphing and more Exercises Mathematical Methods in PDF only on Docsity!

MATLAB Chap 2

1. Exercise 1

>> (2.3^2*1.7)/sqrt((1-0.8^2)^2+(2-sqrt(0.87))^2)

ans =

>> sqrt(5)

ans =

  1. Exercise 2

% Expression 1

expr1 = (-3.5)^3 + exp(6)/log(524) + 206^(1/3);

expr

expr

% Expression 2

expr2 = 15 * (sqrt(10) + 3.7^2) / (log10(1365) + 1.9);

expr

expr2 =

% Expression 3

expr3 = exp(2*x)/sqrt(14+x^2-x);

% Expression 4

expr4 = sin((7pi)/9) / (cos((5pi)/7))^2 + (1/7) * tan((5*pi)/12);

expr

expr4 =

MATLAB Chap 3

1.Operator

A=[1 2 3; 5 6 7; 9 10 11];

A(2,3)

ans =

A(:,2)

ans =

A(1,:)

ans =

A(:,3)

ans =

B=A(:)

  1. Data processing

% Define column vectors

x = [3; 2; 6; 8];

y = [4; 1; 3; 5];

% a) Sum of x plus each element of y

a_result = sum(x) + y;

% b) Element-wise exponentiation: x.^y

b_result = x.^y;

% c) Element-wise division: y ./ x

c_result = y ./ x;

% d) Element-wise multiplication: x .* y → z

z = x .* y;

% e) Sum of elements of z → w

w = sum(z);

DIAG - DIAGONAL MATRIX

D = diag([1 2 3])

% D =

TOEPLITZ - MATRIX WITH CONSTANT DIAGONALS

c = [1 2 3]

r = [1 4 5]

E = toeplitz(c, r)

% c = [1 2 3]

% r = [1 4 5]

% E =

TRIU - UPPER TRIANGULAR MATRIX

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

F = triu(M)

% F =

TRIL - LOWER TRIANGULAR MATRIX

G = tril(M)

% G =

RAND - MATRIX WITH RANDOM ELEMENTS IN [0, 1)

H = rand(3)

% H =

% If you want range [-1, 1]

H2 = -1 + 2*rand(3)

% H2 =

LINSPACE - EQUALLY SPACED VECTOR

I = linspace(1, 10, 5)

% I = [1.0000 3.2500 5.5000 7.7500 10.0000]

CAT - CONCATENATE MATRICES

J1 = [1 2; 3 4]

J2 = [5 6; 7 8]

K = cat(1, J1, J2)

% K =

MATLAB Graphing

1.2D Graphs

🧮 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.

MATLAB Exercise

Solution

1.Problem 1

% Compute A

A = ( 3 *sqrt( 3 )/ 4 ) + 24 * ( ( 1 / 12 ) - ( 1 /( 5 * 2 ^ 5 )) - ( 1 /( 28 * 2 ^ 7 )) -

( 1 /( 72 * 2 ^ 9 )) );

% Compute B

B = 3 + ( 1 / 60 ) * ( 8 + ( 2 * 3 )/( 7 * 8 * 3 ) * ( 13 + ( 3 * 5 )/( 10 * 11 * 3 ) * ( 18 +

% Display results

fprintf('A = %.6f\n', A);

fprintf('B = %.6f\n', B);

  1. Problem 2 WITH NO INPUT

% MATLAB script to compute x, y, and z

% Compute x

x = (2*sqrt(3))/5;

% Compute y

y = (2*pi)/exp(-x^3 / 3);

% Compute z

z = 4x + log10(2y);

% Display results

fprintf('x = %.6f\n', x);

fprintf('y = %.6f\n', y);

fprintf('z = %.6f\n', z);

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

  1. Problem 4 NO INPUT y = 2x^6 - 3x^4 + 4*x^2 - 1988 solve(y == 0) double(ans)

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

  1. Problem 6 % Define the system of equations f1 = @(y) asin(1.2 - cos(y)); f2 = @(x) (4 - 2*x) / 3; % Initial guess x0 = 0; y0 = 0; % Tolerance for convergence tolerance = 1e-6; % Maximum number of iterations max_iterations = 100; % Initialize variables x_old = x0; y_old = y0; iteration = 0; disp('Iteration | x | y |'); disp('-----------------------------------------'); while iteration < max_iterations % Jacobi iteration x_new = f1(y_old); y_new = f2(x_old); fprintf('%9d | %11.7f | %11.7f |\n', iteration, x_old, y_old); % Check for convergence if abs(x_new - x_old) < tolerance && abs(y_new - y_old) < tolerance break; end % Update values for the next iteration x_old = x_new; y_old = y_new; iteration = iteration + 1; end fprintf('\nSolution found after %d iterations:\n', iteration); fprintf('x = %f\n', x_new); fprintf('y = %f\n', y_new); % Check the solution