All Codes
All Codes
% Subfunction: ComputeForcesAndAccelerations
% |- Input: Mass properties (m, c, k), Current state variables (displacement, velocity)
% |- Output: Acceleration, Spring Force, Damper Force
% |- Call ComputeAcceleration
% |- Call ComputeSpringForce
% |- Call ComputeDamperForce
% Subfunction: ComputeAcceleration
% |- Input: Mass properties (m), Current state variables (displacement, velocity)
% |- Output: Acceleration
% Subfunction: ComputeSpringForce
% |- Input: Spring constant (k), Current displacement
% |- Output: Spring force
% |- Compute force exerted by the spring
% Subfunction: ComputeDamperForce
% |- Input: Damping coefficient (c), Current velocity
% |- Output: Damping force
% |- Compute force exerted by the damper
% Subfunction: UpdateStateVariables
% |- Input: Current state variables (displacement, velocity), Acceleration
% |- Output: Updated state variables
% Subfunction: StoreResults
% |- Input: Results structure, Time, Displacement, Velocity, Acceleration
% |- Output: Updated Results structure
% |- Store time, displacement, velocity, acceleration in the structure
% Subfunction: PlotResults
% |- Input: Results structure
% |- Output: Plot Displacement, Velocity, Acceleration vs Time
% |- Plot the results using MATLAB plotting functions
flow chart
function solveVibrationSystem()
% Initialize system parameters
m1 = 1; m2 = 1; k1 = 10; k2 = 10; c1 = 0.5; c2 = 0.5;
initial_conditions = [0; 0]; % Initial displacement and velocity
% Time vector
t = 0:0.01:10;
% Solve system
[time, displacement, velocity, acceleration] = solveSystem(m1, k1, c1, m2, k2,
c2, initial_conditions, t);
% Plot results
figure;
subplot(3, 1, 1);
plot(time, displacement(:, 1), 'b', time, displacement(:, 2), 'r');
title('Displacement vs Time');
legend('Mass1', 'Mass2');
subplot(3, 1, 2);
plot(time, velocity(:, 1), 'b', time, velocity(:, 2), 'r');
title('Velocity vs Time');
legend('Mass1', 'Mass2');
subplot(3, 1, 3);
plot(time, acceleration(:, 1), 'b', time, acceleration(:, 2), 'r');
title('Acceleration vs Time');
legend('Mass1', 'Mass2');
end
% Preallocate arrays
displacement = zeros(length(t), 2);
velocity = zeros(length(t), 2);
acceleration = zeros(length(t), 2);
x2 = x2 + v2 * dt + 0.5 * a2 * dt^2;
v2 = v2 + a2 * dt;
% Store results
displacement(i, :) = [x1, x2];
velocity(i, :) = [v1, v2];
acceleration(i, :) = [a1, a2];
end
% Initial conditions
x1_0 = 0; % Initial displacement of mass 1
v1_0 = 0; % Initial velocity of mass 1
x2_0 = 0; % Initial displacement of mass 2
v2_0 = 0; % Initial velocity of mass 2
% Simulation parameters
dt = 0.1; % Time step
t_end = 10; % Simulation time
% Initialize variables
t = 0:dt:t_end;
x1 = zeros(size(t));
v1 = zeros(size(t));
x2 = zeros(size(t));
v2 = zeros(size(t));
% Plot results
plot(t, x1, 'r', t, x2, 'b');
title('Displacement vs Time');
xlabel('Time (s)');
ylabel('Displacement');
legend('Mass 1', 'Mass 2');
Q solution code
% Initialize arrays
omega = zeros(1, 50);
omega2 = zeros(1, 50);
d = zeros(4, 50);
x = zeros(2, 50);
p = zeros(2, 50);
%*********************************************************%
%Calculating the amplitude and the phase for each frequency
%defined by the frequency vector.
%*********************************************************%
for i = 1:50
omega(i) = 2 * pi * f(i); % omega in terms of frequency
omega2(i) = omega(i) * omega(i); % squaring omega
a11 = -omega2(i) * m + k; % representing the left hand side matrix...
a12 = omega(i) * c; % matrix of the single matrix equation
a21 = -omega(i) * c; % equation
a22 = -omega2(i) * m + k;
a = [a11, a12; a21, a22];
b = inv(a);
c1 = [0; 0; fi];
d(1, i) = b(1, :) * c1;
d(2, i) = b(2, :) * c1;
d(3, i) = b(3, :) * c1;
d(4, i) = b(4, :) * c1;
x(1, i) = sqrt(abs(d(1, i))^2 + abs(d(3, i))^2);
x(2, i) = sqrt(abs(d(2, i))^2 + abs(d(4, i))^2);
p(1, i) = atan(d(1, i) / d(3, i)) * 180 / pi;
if p(1, i) < 0 % to check whether the angle is negative or not
p(1, i) = 180 + p(1, i);
else
p(1, i) = p(1, i);
end
if p(2, i) < 0
if d(4, i) < 0
p(2, i) = -180 + p(2, i);
else
p(2, i) = 180 + p(2, i);
end
else
p(2, i) = p(2, i);
end
end
% Display results
disp('Results saved to vibration_results.xlsx');
disp('Frequency Amplitude1 Amplitude2 Phase1 Phase2');
disp([f', x', p']);
% User input
user_input = input('Enter a value: ');
% Plot results
figure;
subplot(2, 1, 1);
plot(f, x(1, :), 'b', f, x(2, :), 'r');
xlabel('Frequency');
ylabel('Amplitude');
legend('Amplitude 1', 'Amplitude 2');
title('Amplitude vs Frequency');
subplot(2, 1, 2);
plot(f, p(1, :), 'b', f, p(2, :), 'r');
xlabel('Frequency');
ylabel('Phase (degrees)');
legend('Phase 1', 'Phase 2');
title('Phase vs Frequency');
% 3D plot
figure;
plot3(f, x(1, :), p(1, :), 'b');
hold on;
plot3(f, x(2, :), p(2, :), 'r');
xlabel('Frequency');
ylabel('Amplitude');
zlabel('Phase (degrees)');
legend('Mode 1', 'Mode 2');
title('Amplitude and Phase vs Frequency');
grid on;
% UI components
massLabel = uicontrol('Style', 'text', 'String', 'Mass (kg)', 'Position', [20,
350, 80, 20]);
massInput = uicontrol('Style', 'edit', 'Position', [100, 350, 50, 20]);
% Plot area
plotArea = axes('Parent', fig, 'Units', 'pixels', 'Position', [350, 50, 200,
300]);
% Simulation function
function simulate(~, ~)
% Get user inputs
m = str2double(get(massInput, 'String'));
k = str2double(get(springInput, 'String'));
c = str2double(get(dampingInput, 'String'));
x0 = str2double(get(initialInput, 'String'));
% Time vector
t = linspace(0, 10, 1000);
% System response
sys = tf([1], [m, c, k]);
[time, response] = step(sys, t);