Essentially, I am trying to modify the equation passed to ode45 depending on the value of y it creates. I have attempted to do this using a piecewise function, but to no avail, I get an

Error using symengine

Unable to generate code for

piecewise for use in

anonymous functions.

error.

Is there any work around this, or perhaps another method I can use to achieve this effect.

Also attached is an image that may help explain the actual physics of the problem. Thanks

`function HarmonicMotion%Default Values`

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 = 10; % Total Integration Time

dt = 0.01; % Integration step

x_0 = 2.0; % Init Displacement

tvals = [0:dt:t_total]; x1 = nf_s1(c, m_c, s1, s2, d, dt, t_total, x_0); plot(tvals, x1) function [disp_vel] = nf_s1(c, m_c, s1, s2, d, dt, t_total, x_0) tvals = [0:dt:t_total]; syms y(t); diff_x = y(t)-d; % the adj displacment for the second spring

is_s2 = piecewise(y(t) <= d, 0, y(t) > d, 1) % turns 'on/off' the effect of the bottom spring

eqn = s1*y(t) + c*diff(y) + is_s2*s2*(diff_x) == -m_c * diff(y,2); [V] = odeToVectorField(eqn) M = matlabFunction(V, 'vars', {'t', 'Y'}) [tvals, disp_vel] = ode45(M, tvals, [x_0 0]);

## Best Answer