Help with time dependent ODE with a piece-wise component that needs to mirror a convolution integral

4 views (last 30 days)
Really new here, hardly aware of what's happening. I'm working on a time dependent ODE with a piece-wise function being a portion of it. Of the form mx''(t) + kx(t) = F(t). I will be using y in place of x going forward. On the interval 0-0.2, 0.2-0.6, and 0.6-1.0 F(t) changes: 2.5t, 0.75 - 1.25t, 0, respectively. Initial values are all zero.
tspan = 0:0.01:2;
for i = 0:numel(tspan)
if tspan(1,i) < 0.2
odefcn1 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(2.5.*tspan(1,i)))./5];
elseif (tspan(1,i) >= 0.2) && (tspan(1,i) < 0.6)
odefcn1 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(0.75 - 1.25.*tspan(1,i)))./5];
elseif tspan(1,i) >= 0.6
odefcn1 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(0.*tspan(1,i)))./5];
y0_1 = 0;
ydot0_1 = 0;
ics1 = [y0_1 ydot0_1];
[t,y] = ode45(odefcn1, tspan, ics1);
yfinal = y(:,1);
I just asked a question like this yesterday, but I have learned of more assignment parameters that I didn't know. I need this setup here so that it mirrors a convolution integral. Namely, it automatically swtiches between functions based on the time we are at, so that it works with any timespan input (or at least any within reason).
I've got n error and not a clue what it means. Or how to fix it.
Index in position 2 is invalid. Array indices must be
positive integers or logical values.
Error in odefcnVibrProj3 (line 5)
if tspan(1,i) < 0.2
However changing:
for i = 0:numel(tspan) to for i = 1:numel(tspan)
Just gives me y = 0 for all time, and I know tspan(1,i) is updating like it should. Why doesn't it get applied to the if else statement properly?
Any help at all would be greatly appreciated.

Answers (1)

Torsten on 28 Sep 2022
tspan1 = 0:0.01:0.2;
odefcn1 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(2.5.*t))./5];
y0_1 = 0;
ydot0_1 = 0;
ics1 = [y0_1 ydot0_1];
[T1,Y1] = ode45(odefcn1, tspan1, ics1);
tspan2 = [0.2:0.01:0.6];
ics2 = [Y1(end,1);Y1(end,2)];
odefcn2 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(0.75 - 1.25.*t))./5];
[T2,Y2] = ode45(odefcn2, tspan2, ics2);
tspan3 = [0.6:0.01:2.0];
ics3 = [Y2(end,1);Y2(end,2)];
odefcn3 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(0.*t))./5];
[T3,Y3] = ode45(odefcn3, tspan3, ics3);




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by