Help with time dependent ODE with a piece-wise component
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kolten Strauss
el 28 de Sept. de 2022
Comentada: Kolten Strauss
el 28 de Sept. de 2022
For starters, I am extremely new to Matlab and coding in general. 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.
%Variables
tspan1 = [0 0.2];
tspan2 = [0.2 0.6];
tspan3 = [0.6 1.0];
%maybe we only need three of these to just change per segment?
y0_1 = 0; %y0 (initial boundry condition of first piece)
y0_2 = 0; %y0 (initial boundry condition of second piece)
y0_3 = 0; %y0 (initial boundry condition of third piece)
ydot0_1 = 0; %This boundry condition doesn't change between pieces.
%ydot0_2 = 0;
%ydot0_3 = 0;
ics1 = [y0_1, ydot0_1]; %The intial conditions. (y(0) = ..., y'(0) = ...
%Piecewise Segments
odefcn1 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(2.5.*t))./5]; %first piece
odefcn2 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(0.75 - 1.25.*t))./5]; %second piece
odefcn3 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(0.*t))./5]; %third piece
%ODE45 Generated Function
[t1,y1] = ode45(@(t,y) odefcn1, tspan1, ics1);
ics2 = y1(end,:);
ics2(1) = y0_2;
[t2,y2] = ode45(@(t2,y2) odefcn2, tspan2, ics2);
ics3 = y2(end,:);
ics3(1) = y0_3;
[t3,y3] = ode45(@(t3,y3) odefcn3, tspan3, ics3);
%Concatinate Data Values, and Plot
t = [t1;t2;t3];
y = [y1;y2;y3];
plot(t,y);
Despite this huge setup, I may just have a small problem:
Error using odearguments
@(T,Y)ODEFCN1 returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned by @(T,Y)ODEFCN1 and the initial conditions vector must have the same number of
elements.
First of all, I really thought odefcn1 should be a length of two, and second of all, if I humor it with an initial input of 1 length, it gives:
Error using superiorfloat
Inputs must be floats, namely single or double.
I know this is pretty open for a question, but I have no idea how to continue past this of if I've even made some huge mistake early on. Any help would be wonderful.
0 comentarios
Respuesta aceptada
Davide Masiello
el 28 de Sept. de 2022
I think that the problem is that you already defined the ODE system as an anonymous function, therefore there's no need to call it using a function handle in ode45. See below.
%Variables
tspan1 = [0 0.2];
tspan2 = [0.2 0.6];
tspan3 = [0.6 1.0];
%maybe we only need three of these to just change per segment?
y0_1 = 0; %y0 (initial boundry condition of first piece)
y0_2 = 0; %y0 (initial boundry condition of second piece)
y0_3 = 0; %y0 (initial boundry condition of third piece)
ydot0_1 = 0; %This boundry condition doesn't change between pieces.
%ydot0_2 = 0;
%ydot0_3 = 0;
ics1 = [y0_1, ydot0_1]; %The intial conditions. (y(0) = ..., y'(0) = ...
%Piecewise Segments
odefcn1 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(2.5.*t))./5]; %first piece
odefcn2 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(0.75 - 1.25.*t))./5]; %second piece
odefcn3 = @(t,y) [y(2); -(1500.*y(1) - 1500.*(0.*t))./5]; %third piece
%ODE45 Generated Function
[t1,y1] = ode45(odefcn1, tspan1, ics1);
ics2 = y1(end,:);
ics2(1) = y0_2;
[t2,y2] = ode45(odefcn2, tspan2, ics2);
ics3 = y2(end,:);
ics3(1) = y0_3;
[t3,y3] = ode45(odefcn3, tspan3, ics3);
%Concatinate Data Values, and Plot
t = [t1;t2;t3];
y = [y1;y2;y3];
plot(t,y);
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!