0% found this document useful (0 votes)
45 views8 pages

All Codes

The document describes the structure and functions of a MATLAB code to simulate a two-mass vibration system. The code initializes system parameters, defines subfunctions to compute forces, integrates the system over time using Euler's method, and plots the results of displacement, velocity and acceleration versus time.

Uploaded by

a7med.3mvr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
45 views8 pages

All Codes

The document describes the structure and functions of a MATLAB code to simulate a two-mass vibration system. The code initializes system parameters, defines subfunctions to compute forces, integrates the system over time using Euler's method, and plots the results of displacement, velocity and acceleration versus time.

Uploaded by

a7med.3mvr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Structure chart

% Vibration System Structure Chart

% Main Function: SolveVibrationSystem


% |- Initialize System Parameters
% | |- Define Masses (m1, m2), Springs (k1, k2), Dampers (c1, c2)
% | |- Set Initial Conditions (initial_displacement, initial_velocity)
% | |- Set Simulation Time and Time Step
% | |- Create Time Vector
% |- Initialize Variables for Storing Results
% | |- Create a structure to store time, displacement, velocity, acceleration
% |- Loop over Time Steps
% | |- Call ComputeForcesAndAccelerations for Mass 1
% | | |- Call ComputeSpringForce for Spring 1
% | | |- Call ComputeDamperForce for Damper 1
% | | |- Update Mass 1 state variables
% | |- Call ComputeForcesAndAccelerations for Mass 2
% | | |- Call ComputeSpringForce for Spring 2
% | | |- Call ComputeDamperForce for Damper 2
% | | |- Update Mass 2 state variables
% | |- Store Results
% |- Plot Results

% 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

function [time, displacement, velocity, acceleration] = solveSystem(m1, k1, c1, m2,


k2, c2, initial_conditions, t)
% Initialize state variables
x1 = initial_conditions(1);
v1 = initial_conditions(2);
x2 = 0;
v2 = 0;

% Preallocate arrays
displacement = zeros(length(t), 2);
velocity = zeros(length(t), 2);
acceleration = zeros(length(t), 2);

% Iterate over time steps


for i = 1:length(t)
% Call Mass1, Spring1, Damper1 modules
[a1, f1_damper] = mass1(x1, v1, k1, c1);
f1_spring = spring1(x1, k1);

% Call Mass2, Spring2, Damper2 modules


[a2, f2_damper] = mass2(x2, v2, k2, c2);
f2_spring = spring2(x2, k2);
% Update overall system state variables
x1 = x1 + v1 * dt + 0.5 * a1 * dt^2;
v1 = v1 + a1 * dt;

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

% Return time vector


time = t;
end

function [acceleration, damper_force] = mass1(displacement, velocity,


spring_constant, damper_coefficient)
% Compute acceleration of Mass1
acceleration = (-spring_constant * displacement - damper_coefficient * velocity)
/ m1;

% Compute damper force exerted by Damper1


damper_force = damper_coefficient * velocity;
end

% Define other modules (spring1, mass2, spring2, damper2) similarly...


Pesudocode.
% Define system parameters
m1 = 1; % Mass 1
m2 = 1; % Mass 2
k1 = 10; % Spring constant 1
k2 = 5; % Spring constant 2
c1 = 2; % Damping coefficient 1
c2 = 1; % Damping coefficient 2

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

% Euler integration loop


for i = 1:length(t)-1
% Compute accelerations
a1 = (-c1*v1(i) - k1*x1(i) + k2*(x2(i) - x1(i)) + c2*(v2(i) - v1(i))) / m1;
a2 = (-c2*(v2(i) - v1(i)) - k2*(x2(i) - x1(i))) / m2;

% Update velocities and positions using Euler method


v1(i+1) = v1(i) + a1 * dt;
x1(i+1) = x1(i) + v1(i+1) * dt;

v2(i+1) = v2(i) + a2 * dt;


x2(i+1) = x2(i) + v2(i+1) * dt;
end

% 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

f = linspace(0, 0.7, 50);


m = [1 0; 0 2];
k = [2 -1; -1 1];
c = [0.002 -0.001; -0.001 0.001];
fi = [1; 1];

% 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

p(2, i) = atan(d(2, i) / d(4, i)) * 180 / pi;

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

% Save results to Excel file


xlswrite('vibration_results.xlsx', [f', x', p'], 'Sheet1');

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

% Publish the code


publish('your_script.m', 'pdf');
GUI
function VibrationSystemGUI
% Create figure
fig = figure('Name', 'Vibration System GUI', 'NumberTitle', 'off', 'Position',
[100, 100, 600, 400]);

% UI components
massLabel = uicontrol('Style', 'text', 'String', 'Mass (kg)', 'Position', [20,
350, 80, 20]);
massInput = uicontrol('Style', 'edit', 'Position', [100, 350, 50, 20]);

springLabel = uicontrol('Style', 'text', 'String', 'Spring Constant (N/m)',


'Position', [20, 320, 130, 20]);
springInput = uicontrol('Style', 'edit', 'Position', [160, 320, 50, 20]);

dampingLabel = uicontrol('Style', 'text', 'String', 'Damping Coefficient (Ns/m)',


'Position', [20, 290, 150, 20]);
dampingInput = uicontrol('Style', 'edit', 'Position', [180, 290, 50, 20]);

initialLabel = uicontrol('Style', 'text', 'String', 'Initial Displacement (m)',


'Position', [20, 260, 150, 20]);
initialInput = uicontrol('Style', 'edit', 'Position', [180, 260, 50, 20]);

simulateButton = uicontrol('Style', 'pushbutton', 'String', 'Simulate',


'Position', [250, 260, 80, 30], 'Callback', @simulate);

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

% Plot the response


plot(plotArea, time, x0 + response * x0);
title(plotArea, 'Vibration System Response');
xlabel(plotArea, 'Time (s)');
ylabel(plotArea, 'Displacement (m)');
grid(plotArea, 'on');
end
end

You might also like