How do I create and solve a system of N coupled differential equations?
8 views (last 30 days)
Show older comments
Hey y'all, I'm having a lot of trouble with my nonlinear dynamics project. I'm attempting to model the relationship between a bridge and N pedestrians. I have to create a separate differential equation for each of these pedestrians. I'm not sure what information to include here, but this is my function file:
function f = Millennium(t, x, N)
% Set System Parameters
M = 1.13 * 10^5;
B = 1.10 * 10^4;
K = 4.73 * 10^6;
G = 30;
C = 16;
a = pi()/2;
ThetaMean = 6.47;
ThetaSD = 0.63;
Zeta = B/sqrt(4*M*K);
% Set Each Pedestrian's Footfall Rate
Omega = normrnd(ThetaMean, ThetaSD, [1 N]);
% Set Bridge's Differential Equations
f = [x(2); 1/M*((G*N*sin(x(3)))-B*x(2)-K*x(1))];
% Set Pedestrian's Differential Equations
i = 3;
for k = 1:1:N
f = [f; Omega(k)+C*(x(1)^2+(sqrt(M/K)*x(2))^2)^(1/2)*sin(atan((x(1)*sqrt(K/M))/x(2))-x(i)+a)]
i = i+1;
end
end
So for each differential equation, I'm trying to only change the Omega value, and the variable x(i) - starting with x(3), which represents each pedestrian's phase. The script that I call the function with is below:
% Set Number of Pedestrians
N = 300;
% Set Initial Conditions and Tspan
tspan = [0:1:100];
init = [0.1 0.1];
for q = 1:1:N
init = [init, 0.01];
end
[T, Results] = ode45(@Millennium, tspan, init);
Running this gives me the following errors:
Error using Millennium (line 15)
Not enough input arguments.
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ME4930Project (line 15)
[T, Results] = ode45(@Millennium, tspan, init);
Honestly, I've tried so many different ways of coding this that I just need another pair of eyes to see if anyone else can see any blatant errors. I'm sure I'm probably far off-base but any help would be greatly appreciated and I'd be happy to provide any requisite extra information. Thanks!
2 Comments
shegaw mamo
on 16 Aug 2019
Thanks. I have got new idea. could you help me?
Ommega and Sigma are not in the system of equation. How can I develop matlab syntax or code including ommega and L using ode45?
dx(1) = alpha1*x(1)-alpha1*x(1).^2./kappa1-beta1*x(1)*x(2)-epsilon*gamma*x(1);
dx(2) = alpha2*x(2)-alpha2*x(2).^2./kappa2-beta2*x(1)*x(2)-gamma*x(2);
Steven Lord
on 16 Aug 2019
This doesn't appear to be related to the original question that much, so you should ask it as a new question. When you do, include as much detail about the question you're trying to solve as you can. Assume we have no idea what you're trying to do (which is true.)
Answers (1)
Mukul Rao
on 4 Dec 2017
Hi Patrick, taking a quick look at the error message, it seems that "ode45" is attempting to call your function "Millennium" with fewer arguments than expected. Indeed, if you take a look at the documentation for "ode45", you will discover that in the function signature :
[t,y] = ode45(odefun,tspan,y0)
The function dydt = odefun(t,y), for a scalar t and a column vector y, must return a column vector dydt of data type single or double that corresponds to f(t,y). odefun must accept both input arguments, t and y, even if one of the arguments is not used in the function.
Note that "odefun" is a function that accepts two arguments (t,y), but your customer function "Millennium" expects 3 inputs (t,x,N). Therefore, when the solver attempts to call "Millennium" with just two inputs, you observe the "not enough arguments" error message.
How do you resolve this? The trick is to wrap the call to "Millennium" with another function that takes in two arguments (t,x) from "ode45" but then calls "Millenium" with (t,x,N)
Here is an example snippet:
% Set Number of Pedestrians
N = 300;
% Set Initial Conditions and Tspan
tspan = [0:1:100];
init = [0.1 0.1];
for q = 1:1:N
init = [init, 0.01];
end
MillenniumWrapper = @(t,x) Millennium(t,x,N);
% ode45 calles MilleniumWrapper with (t,x) and in turn
% MilleniumWrapper calls Millenium with (t,x,N)
[T, Results] = ode45(@MillenniumWrapper, tspan, init);
This technique of calling functions with additional parameters is documented here, I encourage you to read this as it finds wide applications in optimization problems, odes etc.
0 Comments
See Also
Categories
Find more on Ordinary Differential Equations 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!