Changing the differential equation using even functions (with ODE23)
4 次查看(过去 30 天)
显示 更早的评论
am using event functions for when the mass is decoupled from the 2nd spring (see diagram below), however despite changing the value of s2_on within the function, it does not appear that the ode is changing when the event is reached. (The event is definitely triggering as I found from making it terminal).
function HarmonicMotion
%% Calculation and Plotting
function plot_nf(data) % Plots values for no fluids using ODE 45
% Event Functions
inc = 0;
function [position, isterminal, direction] = isGapDec(t, x)
d = data(5);
position = x(1) - d;
isterminal = 0; % Doesn't end the ode
direction = -1; % For when mass decoupled
s2_on = 0; % i.e Cancels effect of s2_on in ode
end
function [val, isterminal, direction] = isGapInc(t, x)
d = data(5);
val = x(1) - d;
isterminal = 0; % Doesn't end the ode
direction = 1; % When mass 'attatched'
s2_on = 1;
end
disp(inc)
opt1 = odeset('Stats', 'on', 'Events', @isGapInc);
opt2 = odeset(opt1, 'Events', @isGapDec);
function [disp_vel] = calc_nf(xv, data)
% Extract data
m_c = data(1); s1 = data(2); s2 = data(3); c = data(4);
d = data(5); t_total = data(6); dt = data(7); xv0(1) = data(8);
m_f_max = data(9); fluid_r = data(10);
s2_on = 1;
x = xv(1); v = xv(2); %Sets x = xv(1), and v = xv(2) i.e d(x)/dt
disp_vel = [v; -(s1*x + c*v + s2*s2_on*(x-d))/m_c]; %%%% THE DIFFERENTIAL EQ. %%%, WITH S2_ON THE ISSUE
end
[t, xv1] = ode23(@(t,x) calc_nf(x, data), [0:dt:t_total], xv0, opt2); % passes xv1 through calc_nf with boundary cond xv0
accel = diff(xv1(:,2))./diff(t);
accel(end+1) = 0;
figure('Name','Without Fluid','NumberTitle','off');
hold on
plot(t, [xv1(:,1), xv1(:,2), accel], [0 t_total], [d d], 'k-.');
title('Plot of Harmonic Motion without Fluid');
grid minor;
xlabel('time / s');legend('Displacement / m', 'Velocity / ms^{-1}', 'Acceleration / ms^{-2}', 'Gap'); % Labels graph
hold off;
end
%% Default values and Table
%%% Default Values Array %%%
m_c = 2; % Container Mass
s1 = 16; % Spring Constant 1
s2 = 20; % Spring Constant 2
c = 2; % Damping Coef
d = 0.6; % Gap
t_total = 2; % Total Integration Time
dt = 0.01; % Integration step
xv0 = [2 0]; % Init Displacement & velocity
m_f_max = 4; % Max mass of fluid
fluid_r = 1; % Fluid mass rate
default_vals = [2 16 20 10 0.6 2 0.01 2 4 1]; % For use in table
data = [m_c s1 s2 c d t_total dt xv0(1) m_f_max fluid_r]; % For use in functions, and table
plot_nf(data);
end
I 0 个评论
回答(1 个)
Arnav
2025-3-12
Hi Fawaz,
Attempting to change the value of s2_on inside the event functions will not affect the ODE solver, because s2_on is scoped locally within those event functions. You can create a persistent variable inside the HarmonicMotion function as follows:
function HarmonicMotion
persistent s_on % persistent variable
function plot_nf(data) % Plots values for no fluids using ODE 45
% rest of the code
end
% rest of the code
end
This will create a persistent variable that is shared between the event functions. You may refer to the following MATLAB documentation link for more information about how variables are scoped:
Hope it helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!