Solving Differential Equations With
Show older comments
Hi, I'm new in Matlab and I have a problem with ODE45 comand.
Basically, I'm trying to solve the second order differential equation of a spring (ke) -mass (me) -damper (ce) system. With [0; 0] initial conditions. It follows the code:
[t,x] = ode45(@SMD,[0 Tmax],[0; 0]);
plot(t,x(:,1))
SMD is defined in a function as:
function dxdt = SMD(t,x,me,ce,ke,Ft)
fe=2;
Qsie=0.05;
me=32000;
ce=2*Qsie*(2*pi*fe)*me;
ke=me*(2*pi*fe)^2;
dxdt = [x(2); (-ce*x(2)-ke*x(1)+Ft)/me];
But Ft is a force vector (varying on time) calculated previously. How can I import this vector to dxdt function?
Is there any restriction in relation to the Length of vector Ft? I asked this question because I dont know the time increment of the integration when I use ODE45. The vectors of the analysis may be different Lengths (Ft and x).
Other question: there is a form of the variables me, ce and ke be imported from the main program? When I try compile the code without indicate the values of them in the function dxdt, an error was reported.
Thanks very much!
1 Comment
Torsten
on 14 Mar 2022
What is the time vector corresponding to Ft ?
Accepted Answer
More Answers (3)
Torsten
on 14 Mar 2022
fe=2;
Qsie=0.05;
me=32000;
ce=2*Qsie*(2*pi*fe)*me;
ke=me*(2*pi*fe)^2;
[t,x] = ode45(@(t,x)SMD(t,x,me,ce,ke,tt,Ft),[0 Tmax],[0; 0]);
function dxdt = SMD(t,x,me,ce,ke,tt,ft)
Ft = interp1(tt,ft,t);
dxdt = [x(2); (-ce*x(2)-ke*x(1)+Ft)/me];
end
where tt is the time vector corresponding to Ft.
8 Comments
Igor Braz Gonzaga
on 14 Mar 2022
Torsten
on 14 Mar 2022
Except that "fe" and "Tmax" do not appear in your list of parameter definitions, I don't see an error in your code.
Igor Braz Gonzaga
on 15 Mar 2022
You do not define tt for use in Ft, but if you replace tt with t then I see no error message.
However if you switch to ode15s you would get an error. That is because you are doing linear interpolation for Ft, but linear interpolation breaks the mathematics of the Runge-Kutta methods, which require that the second derivatives of the functions are continuous.
%Structure date
fe=2;
Qsie=0.05;
me=32000;
ce=2*Qsie*(2*pi*fe)*me;
ke=me*(2*pi*fe)^2;
%Time date
Tmax=30; %change the value of this variable reports a problem
t=0:1/50:Tmax;
Ft=50*750*sin(t*5);
[temp,x] = ode45(@(temp,x)solve_(temp,x,me,ce,ke,t,Ft),[0 50],[0; 0]);
plot(temp,x(:,2))
function dxdt = solve_(temp,x,me,ce,ke,t,ft)
Ft = interp1(t,ft,temp);
dxdt = [x(2); (-ce*x(2)-ke*x(1)+Ft)/me];
end
Igor Braz Gonzaga
on 15 Mar 2022
This is a general remark, not directly related to the error you receive:
The function Ft cannot be stochastic if you use an ODE solver from MATLAB's solver suite.
The inputs for the solvers are assumed at least continuous. So the result of using the Ft curve from above will either be that the solver stops integration or that the results will be arbitrary.
For the error message, please include your complete code.
Igor Braz Gonzaga
on 15 Mar 2022
Walter Roberson
on 15 Mar 2022
None of the ode*() functions support discontinuities in the equations such as might be due to random forces or impulses.
The Finance toolbox has some support for Stochastic Differential Equations; https://www.mathworks.com/help/finance/stochastic-differential-equation-sde-models.html
For the ode*() routines, you would need to stop integrating at every point that randomness occurs, and resume integrating again.
Sam Chak
on 15 Mar 2022
If this is an exercise in the Mechanics class, usually
will be given. Anyhow, there are generally 3 cases for the force vector,
:
will be given. Anyhow, there are generally 3 cases for the force vector,
:- Case 1: feedback control force;
- Case 2: no force input (free response);
- Case 3: external disturbing force (can be a sinusoidal signal or a constant force).
You can investigate the system response by selecting the type of force (Ft1, Ft2, or Ft3) on the “dydt(2)” line.
function dydt = odefcn(t, y)
dydt = zeros(2,1);
fe = 2;
Omega = 2*pi*fe; % undamped natural frequency
Qsie = 0.05; % damping ratio
me = 32000; % mass coefficient
ce = 2*Qsie*(Omega)*me; % damping coefficient
ke = me*(Omega)^2; % stiffness coefficient
A = [0 1; -ke/me -ce/me]; % state matrix derived from ODE (for Case #1 only)
B = [0; 1/me]; % input matrix derived from ODE (for Case #1 only)
p = [-1+1i -1-1i]; % eigenvalues from desired x" + 2*x' + 2*x = 0 (for Case #1 only)
K = place(A, B, p); % control gains (for Case #1 only)
Ft1 = - K(1)*y(1) - K(2)*y(2); % Case #1: control force
Ft2 = 0; % Case #2: no force (free response)
Ft3 = me*cos(Omega*t); % Case #3: disturbing sinusoidal force
dydt(1) = y(2);
dydt(2) = (- ce*y(2) - ke*y(1) + Ft1)/me;
end
You can also set the simulation time, tspan, and the initial condition, y0, depending on your original problem. (
,
) is the equilibrium point for Cases #1 & #2. If this is a control project, you will need to determine the eigenvalues (a.k.a poles) from the desired system response and then the control gains can be designed using the pole placement technique.
tspan = linspace(0, 20, 2001)';
y0 = [0.1; 0];
[t, y] = ode45(@(t, y) odefcn(t, y), tspan, y0);
plot(t, y)
Results:



13 Comments
Igor Braz Gonzaga
on 15 Mar 2022
Sam Chak
on 16 Mar 2022
I didn't notice that you posted
in the comment section. But this looks like a harmonic load. Can you reconfirm the math equation for
? If it is a critical information, you can actually edit your Question and put the new info there.
in the comment section. But this looks like a harmonic load. Can you reconfirm the math equation for
? If it is a critical information, you can actually edit your Question and put the new info there.Else, please clarify if you are free to assign
as any discrete-time random load, or if
can be converted to a Stairstep signal.
as any discrete-time random load, or if
can be converted to a Stairstep signal.
Igor Braz Gonzaga
on 16 Mar 2022
Torsten
on 16 Mar 2022
Are you sure about your loop from above ? It will only give you one single value for Ft since Ft is a scalar.
Igor Braz Gonzaga
on 16 Mar 2022
Edited: Igor Braz Gonzaga
on 16 Mar 2022
Sam Chak
on 16 Mar 2022
Thanks for your reply. I guess you are trying to model the discrete oscillation that is generated by random number of pedestrians walking over a rigid body structure. Since you can freely model it, can you show the desired
equation in LaTeX format?
equation in LaTeX format?
Igor Braz Gonzaga
on 16 Mar 2022
Igor Braz Gonzaga
on 16 Mar 2022
Sam Chak
on 16 Mar 2022
Thanks for the input files. As suggested by @Torsten, I have made an approximation of the Ft using the bell curve or normal distribution function with added noise. Is this good enough for the vibration analysis of the structure? It is also possible to make a skewed distribution shape, if needed. I think using the Gamma distribution function.
t = [0:0.02:15]';
Amp = 3.5e4;
mean = 7.5
stddev = 1.5
x = Amp*stddev*sqrt(2*pi)*normpdf(t, mean, stddev);
y = awgn(x, 30, 'measured');
plot(t, [x y])

This way, you should be able to run ODE45.
Igor Braz Gonzaga
on 16 Mar 2022
Torsten
on 16 Mar 2022
As pointed out several times, you cannot feed the differential equation with stochastic input.
So either you accept a way to smoothen the Ft curves or we should quit discussion here.
Walter Roberson
on 16 Mar 2022
As I posted earlier,
The Finance toolbox has some support for Stochastic Differential Equations; https://www.mathworks.com/help/finance/stochastic-differential-equation-sde-models.html
If you do not use that, then you need to stop integration each time you have a new random event.
Igor Braz Gonzaga
on 18 Mar 2022
1 Comment
Walter Roberson
on 18 Mar 2022
acel=diff(y(:,2));
That assumes that the items are all the same time apart.
You should use gradient(y(:,2), tt)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!












