Solving an ODE with changing boundary conditions
Show older comments
I have written a function to solve an ODE used to simulate a swarm of robots surrounding a moving point. What made sense to me was to have the initial and boundary conditions updated each second, and use a for loop to do this. The boundary conditions represent some of the robots which have knowledge of where the target curve/point is because of better sensing capabilities.
Parameters set:
%% Model Parameters
a= .1; % connection strength
r = 1; % radius of circle
L=.2;
f=@(t,z) 0.01; % simplified model dynamics
k=1.5; % boundary feedback gain
gamma=@(theta, t) [r*cos((2*pi)*theta);r*sin((2*pi)*theta)] + [1*t; 1*t]; % target curve
% tauM=.89; % time-delay bound
alpha=.1;
%% Simulation parameters
tf=20; % simulation time
N=20; % Number of agents
The function returns the error in x and y relative to the target curve (y0), over timespan tmesh0.
function [tmesh0,y0]=Numeric_soln_ODEs_const(a,f,k,gamma,tf,N)
% ODE modelling of MAS with a delay in the leader
disp('Solving ODEs with constant piecewise global controller...')
tic
deltaf=@(t,gamma,y) f(t,gamma+y)-f(t,gamma);
%% First iteration (t=0)
leader=ode45(@(t,y) -k*y+deltaf(t,gamma(0,0),y),[0,1],-gamma(0,0));
% Followers
h=1/(N+1);
T=-a/h*(diag(ones(1,N))-diag(ones(1,N-1),-1)); % Laplacian matrix
I_n=eye(size(gamma(0,0),1)); % n-dimensional identity matrix
gammaVal= gamma((1:N)*h,0); % Target position for the followers
disp("gammaVal size = " + num2str(size(gammaVal)));
rhs=@(t,y) kron(T,I_n)*y+deltaf(t,gammaVal(:),y)+kron([a/h;zeros(N-1,1)],I_n)*deval(leader,t);
[tmesh0,y0]=ode45(rhs,[0,1],-gammaVal(:));
% Combining data
y0=[deval(leader,tmesh0)',y0];
[a,b]=size(y0);
disp("rows="+num2str(a)+" cols="+num2str(b));
x1=gammaVal(1,:)+y0(:,3:2:end); % x values of agents
x2=gammaVal(2,:)+y0(:,4:2:end); % y values of agents
next_init = [x1(end,:);x2(end,:)]; % next iteration is the last spatial coordinates of the agents
disp("next_init size = " + num2str(size(next_init)));
%% Loop
for i = 1:1:tf-1
% Leader (Boundary conditions)
leader=ode45(@(t,y) -k*y+deltaf(t,gamma(0,i),y),[i,i+1],gamma(0,i));
% Followers
gammaVal= gamma((1:N)*h,i); % Target position for the followers
rhs=@(t,y) kron(T,I_n)*y+deltaf(t,gammaVal(:),y)+kron([a/h;zeros(N-1,1)],I_n)*deval(leader,t);
[tmesh,y]=ode45(rhs,[i,i+1],next_init);
% Combining data
y=[deval(leader,tmesh)',y];
y0 = [y0; y];
tmesh0 = [tmesh0; tmesh];
[a,b]=size(y0);
disp("rows="+num2str(a)+"cols="+num2str(b));
x1=gammaVal(1,:)+y0(:,3:2:end); % x values of agents. Have removed a value so matrix sizes match
x2=gammaVal(2,:)+y0(:,4:2:end); % y values of agents. Have removed a value so matrix sizes match
next_init = [x1(end,:);x2(end,:)]; % next iteration is the last spatial coordinates of the agents
end
disp("ODEs with global constant piecewise controller are solved in " + num2str(toc) + " sec")
Then to find the L2-Norm, I use this code in my main script:
figure('name','L2-norms of the error') ;
plot(Tode,sum(Yode.^2,2)/(N+1));
When I plot this, the error is of order 9, which is large enough for me to realise I have made a mistake when formulating. Anyone know where this could be, or just a simpler way to do it as I have a feeling I am overcomplicating?
5 Comments
Torsten
on 18 Mar 2024
Do you think anybody will be able to understand what you are doing in your code without supplying a proper mathematical description of the problem ?
Elizabeth Violet
on 19 Mar 2024
% Leader (Boundary conditions)
leader=ode45(@(t,y) -k*y+deltaf(t,gamma(0,i),y),[i,i+1],gamma(0,i));
% Followers
gammaVal= gamma((1:N)*h,i); % Target position for the followers
rhs=@(t,y) kron(T,I_n)*y+deltaf(t,gammaVal(:),y)+kron([a/h;zeros(N-1,1)],I_n)*deval(leader,t);
[tmesh,y]=ode45(rhs,[i,i+1],next_init);
You integrate the leader and follower equations subsequently in steps of 1. Wouldn't it be possible to integrate both in one call to ode45 over the complete timespan [1 tf] ? Would this solve your "boundary conditions problem" (which I still don't understand) ?
Elizabeth Violet
on 19 Mar 2024
Torsten
on 19 Mar 2024
How should I formulate the boundary conditions in MATLAB to take this into account?
Assuming the boundary condition changes at the end of the rod and you formulate the temperature distribution of the rod as a PDE
rho*cp*dT/dt = d/dx(lambda*dT/dx)
and discretize this PDE in xstart = x1 < x2 < ... < xn = xend to get the temperatures T1, T2,..., Tn, the differential equation in xend for T would be
dTn/dt = known rate
Answers (0)
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!