This experiment introduces MATLAB and its basic commands such as matrix operations, conditional statements, loops, and plotting.
% MATLAB Basic Commands Example x = 5; % Assign value to variable y = 3; % Assign value to another variable z = x * y; % Scalar multiplication disp(['Result of multiplication: ', num2str(z)]); % Display result % Vector example v = [1, 2, 3, 4, 5]; % Define a vector disp('Vector:'); disp(v); % Plotting example figure; plot(v); title('Basic Plot'); xlabel('Index'); ylabel('Value');
This experiment involves plotting different standard signals like step, impulse, ramp, exponential, parabolic, and sinusoidal functions using MATLAB.
% Define time vector t = 0:0.1:10; % Unit step function u = double(t >= 0); % Unit impulse function impulse = [1, zeros(1, length(t)-1)]; % Unit ramp function ramp = t .* (t >= 0); % Exponential function exp_func = exp(-t); % Parabolic function parab = t.^2; % Sinusoidal function sinusoid = sin(t); % Plotting all functions figure; subplot(3,2,1); plot(t, u); title('Unit Step'); subplot(3,2,2); stem(t, impulse); title('Unit Impulse'); subplot(3,2,3); plot(t, ramp); title('Unit Ramp'); subplot(3,2,4); plot(t, exp_func); title('Exponential'); subplot(3,2,5); plot(t, parab); title('Parabolic'); subplot(3,2,6); plot(t, sinusoid); title('Sinusoidal');
This experiment studies the transient response of a series RLC circuit to different types of waveforms and verifies the results using MATLAB simulations.
% RLC Circuit Transient Response R = 1; % Resistance L = 1; % Inductance C = 1; % Capacitance f = 10; % Frequency omega = 2 * pi * f; % Angular frequency % Define time vector t = 0:0.001:1; % Apply a sinusoidal voltage source V = sin(omega * t); % Solve for the current in the circuit using the formula I = V / R; % Simplified for demonstration purposes % Plot the voltage and current figure; subplot(2,1,1); plot(t, V); title('Voltage (V)'); subplot(2,1,2); plot(t, I); title('Current (I)');
This experiment verifies the time response of a first-order and second-order system for unit step and square wave inputs using MATLAB simulations.
% Define system transfer functions for first-order and second-order systems sys1 = tf([1], [1, 1]); % First-order system sys2 = tf([1], [1, 2, 1]); % Second-order system % Time vector for simulation t = 0:0.01:10; % Step response for both systems figure; subplot(2,1,1); step(sys1, t); title('Step Response of First-Order System'); subplot(2,1,2); step(sys2, t); title('Step Response of Second-Order System');
This experiment uses MATLAB to determine the currents in various resistors connected in a network using mesh current and node voltage analysis.
% Mesh Current Analysis Example % Define the system matrix (A) and the constant matrix (B) A = [2, -1, 0; -1, 2, -1; 0, -1, 2]; B = [1; 0; 1]; % Solve for mesh currents I = A \ B; % Mesh current solutions disp('Mesh Currents:'); disp(I);
This experiment determines the Z and parameters of a given two-port network.
% Example Z parameters calculation for two-port network % Given two-port network parameters A = 2; B = 3; C = 4; D = 5; % Z-parameters Z11 = A - (B*C)/D; Z12 = B; Z21 = C; Z22 = D - (B*C)/A; disp('Z Parameters:'); disp(['Z11: ', num2str(Z11)]); disp(['Z12: ', num2str(Z12)]); disp(['Z21: ', num2str(Z21)]); disp(['Z22: ', num2str(Z22)]);
This experiment calculates the ABCD parameters of a given two-port network.
% Example ABCD parameters calculation for two-port network % Given two-port network parameters A = 2; B = 3; C = 4; D = 5; % ABCD Parameters disp('ABCD Parameters:'); disp(['A: ', num2str(A)]); disp(['B: ', num2str(B)]); disp(['C: ', num2str(C)]); disp(['D: ', num2str(D)]);
This experiment demonstrates how to fit a linear regression line through a given data set and test the goodness of fit using the mean error.
% Linear regression fitting x = [1, 2, 3, 4, 5]; % Independent variable y = [2, 4, 5, 4, 5]; % Dependent variable % Perform linear regression p = polyfit(x, y, 1); % Fit a first-degree polynomial (linear) y_fit = polyval(p, x); % Evaluate the fitted polynomial % Plot the data and the fitted line figure; plot(x, y, 'o', x, y_fit, '-'); title('Linear Regression Fit'); xlabel('x'); ylabel('y'); legend('Data points', 'Fitted line'); % Calculate mean error mean_error = mean(abs(y - y_fit)); disp(['Mean Error: ', num2str(mean_error)]);
This experiment demonstrates how to perform multiple linear regression (MLR) on a given data set and test the goodness of fit using the mean error.
% Multiple Linear Regression (MLR) x1 = [1, 2, 3, 4, 5]; % First independent variable x2 = [5, 4, 3, 2, 1]; % Second independent variable y = [2, 3, 4, 5, 6]; % Dependent variable % Create design matrix for MLR X = [ones(length(x1), 1), x1', x2']; % Add column of ones for intercept b = X \ y'; % Solve for regression coefficients % Compute the fitted values y_fit = X * b; % Plot the data and fitted values figure; scatter3(x1, x2, y, 'filled'); hold on; % Plot the fitted regression surface [X1, X2] = meshgrid(min(x1):0.1:max(x1), min(x2):0.1:max(x2)); Y = b(1) + b(2)*X1 + b(3)*X2; surf(X1, X2, Y); title('Multiple Linear Regression Fit'); xlabel('x1'); ylabel('x2'); zlabel('y'); % Calculate mean error mean_error = mean(abs(y - y_fit)); disp(['Mean Error: ', num2str(mean_error)]);
This experiment demonstrates how to solve a Linear Programming Problem (LPP) with three variables using the Simplex Method.
% Example LPP: Maximize z = 3x + 2y + 4z % Subject to constraints: % x + y + z <= 10 % 2x + y + 2z <= 15 % x, y, z >= 0 % Coefficients of the objective function f = [-3, -2, -4]; % Negative for maximization % Coefficients of the constraints A = [1, 1, 1; 2, 1, 2]; % Constraint matrix b = [10; 15]; % Right-hand side % Solve the LPP using the Simplex method options = optimset('Display', 'off'); [x, fval] = linprog(f, [], [], A, b, zeros(3,1), [], options); disp('Optimal Solution:'); disp(x); disp('Optimal Value of Objective Function:'); disp(-fval); % Change sign to get maximized value
This experiment demonstrates how to solve a transportation problem involving three variables using optimization techniques.
% Example Transportation Problem % Supply: [30, 40, 50] % Demand: [20, 40, 60] % Cost matrix: % C1 C2 C3 % S1 8 6 10 % S2 9 7 4 % S3 3 2 5 supply = [30, 40, 50]; % Supply demand = [20, 40, 60]; % Demand cost = [8, 6, 10; 9, 7, 4; 3, 2, 5]; % Cost matrix % Solve the transportation problem using linprog f = reshape(cost', [], 1); % Flatten the cost matrix into a vector A_eq = [kron(eye(length(supply)), ones(1, length(demand))); kron(ones(1, length(supply)), eye(length(demand)))]; b_eq = [supply'; demand']; x = linprog(f, [], [], A_eq, b_eq, zeros(length(f),1), []); disp('Optimal transportation plan:'); disp(reshape(x, length(supply), length(demand)));
This experiment demonstrates how to solve an assignment problem of three variables using optimization techniques.
% Example Assignment Problem % Cost matrix for assignments % A1 A2 A3 % P1 9 2 7 % P2 6 4 3 % P3 3 8 6 cost = [9, 2, 7; 6, 4, 3; 3, 8, 6]; % Cost matrix % Solve the assignment problem using Hungarian algorithm [x, fval] = munkres(cost); disp('Optimal assignment solution:'); disp(x); disp('Total cost of assignment:'); disp(fval);