Help solving ODEs in a loop

  • Follow


Hi,

I can use a function to solve a set of ODEs for but I am now trying to solve a bunch of ODE pairs which rely on each other.

I have two variables xi(1) and xi(2) and 2 equation containing them which describes one agent. however i have to solve for a number of agents and in this case the ODEs depends on the the xi(1) and xi(2) of all the other agents and I do not know how to get these values out of the solver and into the function.

I think my main loop is incorrect because it solves for one set of ODEs before moving to the next set and I cannot have this because I need information from the ones to follow to process the first.

My main code looks like this:

global x0;
tspan=[0 50];

for i=1:1:20
%x0 comes from an array of initial conditions for each agent
[t,xi]=ode15s(@APF_ALL,tspan,x0(i,:));
plot(t,xi);
hold on
XLABEL('Time')
YLABEL('Position')
TITLE('APF Over Time')

end

APF_ALL is the function m-file containing the ODE data where i need the xi(1) and xi(2) values of the other agents

I hope this makes sense and I appreciate any help or advise.

Thanks
Adam
0
Reply Adam 11/21/2010 4:51:03 PM


"Adam Henderson" <adam.henderson@strath.ac.uk> wrote in message 
news:icbiln$g09$1@fred.mathworks.com...
> Hi,
>
> I can use a function to solve a set of ODEs for but I am now trying to 
> solve a bunch of ODE pairs which rely on each other.
>
> I have two variables xi(1) and xi(2) and 2 equation containing them which 
> describes one agent. however i have to solve for a number of agents and in 
> this case the ODEs depends on the the xi(1) and xi(2) of all the other 
> agents and I do not know how to get these values out of the solver and 
> into the function.
>
> I think my main loop is incorrect because it solves for one set of ODEs 
> before moving to the next set and I cannot have this because I need 
> information from the ones to follow to process the first.

So turn your "bunch of ODE pairs" into a system of ODEs and solve them all 
at once.

-- 
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 

0
Reply Steven_Lord 11/21/2010 10:25:05 PM


"Steven_Lord" <slord@mathworks.com> wrote in message <icc681$r5g$1@fred.mathworks.com>...
> 
> 
> "Adam Henderson" <adam.henderson@strath.ac.uk> wrote in message 
> news:icbiln$g09$1@fred.mathworks.com...
> > Hi,
> >
> > I can use a function to solve a set of ODEs for but I am now trying to 
> > solve a bunch of ODE pairs which rely on each other.
> >
> > I have two variables xi(1) and xi(2) and 2 equation containing them which 
> > describes one agent. however i have to solve for a number of agents and in 
> > this case the ODEs depends on the the xi(1) and xi(2) of all the other 
> > agents and I do not know how to get these values out of the solver and 
> > into the function.
> >
> > I think my main loop is incorrect because it solves for one set of ODEs 
> > before moving to the next set and I cannot have this because I need 
> > information from the ones to follow to process the first.
> 
> So turn your "bunch of ODE pairs" into a system of ODEs and solve them all 
> at once.
> 
> -- 
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on 
> http://www.mathworks.com 


do you mean make a set of equations which includes a variable for each other agent? if that is the case I will likely have around 100 variables.

Sorry if this is not what you mean.
0
Reply Adam 11/22/2010 4:32:04 PM


"Adam Henderson" <adam.henderson@strath.ac.uk> wrote in message 
news:ice5u4$bi2$1@fred.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message 
> <icc681$r5g$1@fred.mathworks.com>...
>>
>>
>> "Adam Henderson" <adam.henderson@strath.ac.uk> wrote in message 
>> news:icbiln$g09$1@fred.mathworks.com...
>> > Hi,
>> >
>> > I can use a function to solve a set of ODEs for but I am now trying to 
>> > solve a bunch of ODE pairs which rely on each other.
>> >
>> > I have two variables xi(1) and xi(2) and 2 equation containing them 
>> > which describes one agent. however i have to solve for a number of 
>> > agents and in this case the ODEs depends on the the xi(1) and xi(2) of 
>> > all the other agents and I do not know how to get these values out of 
>> > the solver and into the function.
>> >
>> > I think my main loop is incorrect because it solves for one set of ODEs 
>> > before moving to the next set and I cannot have this because I need 
>> > information from the ones to follow to process the first.
>>
>> So turn your "bunch of ODE pairs" into a system of ODEs and solve them 
>> all at once.
>>
>> -- 
>> Steve Lord
>> slord@mathworks.com
>> To contact Technical Support use the Contact Us link on 
>> http://www.mathworks.com
>
>
> do you mean make a set of equations which includes a variable for each 
> other agent? if that is the case I will likely have around 100 variables.

Yes; what you described is a set of coupled ODEs.

"this case the ODEs depends on the the xi(1) and xi(2) of all the other 
agents"

-- 
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 

0
Reply Steven_Lord 11/23/2010 1:45:37 AM

"Steven_Lord" <slord@mathworks.com> wrote in message <icf6c1$rc1$1@fred.mathworks.com>...
> 
> 
> "Adam Henderson" <adam.henderson@strath.ac.uk> wrote in message 
> news:ice5u4$bi2$1@fred.mathworks.com...
> > "Steven_Lord" <slord@mathworks.com> wrote in message 
> > <icc681$r5g$1@fred.mathworks.com>...
> >>
> >>
> >> "Adam Henderson" <adam.henderson@strath.ac.uk> wrote in message 
> >> news:icbiln$g09$1@fred.mathworks.com...
> >> > Hi,
> >> >
> >> > I can use a function to solve a set of ODEs for but I am now trying to 
> >> > solve a bunch of ODE pairs which rely on each other.
> >> >
> >> > I have two variables xi(1) and xi(2) and 2 equation containing them 
> >> > which describes one agent. however i have to solve for a number of 
> >> > agents and in this case the ODEs depends on the the xi(1) and xi(2) of 
> >> > all the other agents and I do not know how to get these values out of 
> >> > the solver and into the function.
> >> >
> >> > I think my main loop is incorrect because it solves for one set of ODEs 
> >> > before moving to the next set and I cannot have this because I need 
> >> > information from the ones to follow to process the first.
> >>
> >> So turn your "bunch of ODE pairs" into a system of ODEs and solve them 
> >> all at once.
> >>
> >> -- 
> >> Steve Lord
> >> slord@mathworks.com
> >> To contact Technical Support use the Contact Us link on 
> >> http://www.mathworks.com
> >
> >
> > do you mean make a set of equations which includes a variable for each 
> > other agent? if that is the case I will likely have around 100 variables.
> 
> Yes; what you described is a set of coupled ODEs.
> 
> "this case the ODEs depends on the the xi(1) and xi(2) of all the other 
> agents"
> 
> -- 
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on 
> http://www.mathworks.com 


Ah ok thanks, this will be difficult for me as there will be so many. I was also hoping to be able to change the number of agents quite easily but if i have to make long equations containting each agent then this will not be possible.
0
Reply Adam 11/23/2010 11:29:05 AM

"Adam Henderson" <adam.henderson@strath.ac.uk> wrote in message <icg8i1$qgr$1@fred.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <icf6c1$rc1$1@fred.mathworks.com>...
> > 
> > 
> > "Adam Henderson" <adam.henderson@strath.ac.uk> wrote in message 
> > news:ice5u4$bi2$1@fred.mathworks.com...
> > > "Steven_Lord" <slord@mathworks.com> wrote in message 
> > > <icc681$r5g$1@fred.mathworks.com>...
> > >>
> > >>
> > >> "Adam Henderson" <adam.henderson@strath.ac.uk> wrote in message 
> > >> news:icbiln$g09$1@fred.mathworks.com...
> > >> > Hi,
> > >> >
> > >> > I can use a function to solve a set of ODEs for but I am now trying to 
> > >> > solve a bunch of ODE pairs which rely on each other.
> > >> >
> > >> > I have two variables xi(1) and xi(2) and 2 equation containing them 
> > >> > which describes one agent. however i have to solve for a number of 
> > >> > agents and in this case the ODEs depends on the the xi(1) and xi(2) of 
> > >> > all the other agents and I do not know how to get these values out of 
> > >> > the solver and into the function.
> > >> >
> > >> > I think my main loop is incorrect because it solves for one set of ODEs 
> > >> > before moving to the next set and I cannot have this because I need 
> > >> > information from the ones to follow to process the first.
> > >>
> > >> So turn your "bunch of ODE pairs" into a system of ODEs and solve them 
> > >> all at once.
> > >>
> > >> -- 
> > >> Steve Lord
> > >> slord@mathworks.com
> > >> To contact Technical Support use the Contact Us link on 
> > >> http://www.mathworks.com
> > >
> > >
> > > do you mean make a set of equations which includes a variable for each 
> > > other agent? if that is the case I will likely have around 100 variables.
> > 
> > Yes; what you described is a set of coupled ODEs.
> > 
> > "this case the ODEs depends on the the xi(1) and xi(2) of all the other 
> > agents"
> > 
> > -- 
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on 
> > http://www.mathworks.com 
> 
> 
> Ah ok thanks, this will be difficult for me as there will be so many. I was also hoping to be able to change the number of agents quite easily but if i have to make long equations containting each agent then this will not be possible.

Right OK, I have been trying what you suggested and I think I can get it provided it is acceptable to create the set of equations in a loop, e.g.

xi(1) to xi(20) can be set using something like:

for i=1:1:20
   dxi(i)=x(i)+x(i+1)+x(i+2)....;
end

however I am getting this error and I do not understand why:

??? Error using ==> odearguments at 116
Solving APF_ALL2 requires an initial condition vector of length 40.

Error in ==> ode15s at 228
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

Error in ==> APF_Solver_ALL2 at 25
[t,xi]=ode15s(@APF_ALL2,tspan,x0);
 
>> length(x0)

ans =

    40

you can see that my initial conditions has 40 elements, in the workspace my x0 is a 1x40 double.

Any ideas?

>> 
0
Reply Adam 11/24/2010 1:27:12 PM

I dont really know how but it seems to be away, however i not have a new error:

Error in ==> APF_ALL2 at 4
Ch=5;

??? Error using ==> feval
Output argument "dUcol" (and maybe others) not assigned during call to
"C:\......\APF_ALL2.m>APF_ALL2".

Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

Error in ==> APF_Solver_ALL2 at 21
[t,xi]=ode45(@APF_ALL2,tspan,x0);
 
>> 

my function is:

function dUcol=APF_ALL2(t,xi)

all the stuff in my function
....
....
dUcol=dU';
end

so dUcol is there. I dont really know what this means
0
Reply Adam 11/24/2010 5:03:04 PM

wish there was an edit but but I have been trying this all day and cannot see what is wrong, im sure it is very basic but i just cant get it. here is my code if anyone wants to paste it in and see. it is

#################### function ###########################

function dUcol=APF_ALL2(t,xi)

%Initial Conditions
Ch=5;
r=10;
Lr=10;
Cr=10;

%Gobal variable of number of robots, this is set in APF_Solver_ALL2
global no_robots

%Initialise vectors to stop growing inside loop
dUxrep=zeros(1,no_robots*2);
dUyrep=zeros(1,no_robots*2);
dUxrepsum=zeros(1,no_robots*2);
dUyrepsum=zeros(1,no_robots*2);
dUxatt=zeros(1,no_robots*2);
dUyatt=zeros(1,no_robots*2);
dUx=zeros(1,no_robots*2);
dUy=zeros(1,no_robots*2);
dU=zeros(1,no_robots*2);

%for each agent calculate attractive gradient (x is odd indicies, y is even)
for i=1:2:no_robots*2
    dUxatt(i)=(Ch*xi(i)*(r/(xi(i)^2 + xi(i+1)^2)^(1/2) - 1))/((r - (xi(i)^2 + xi(i+1)^2)^(1/2))^2 + 1)^(1/2);
    dUyatt(i+1)=(Ch*xi(i+1)*(r/(xi(i)^2 + xi(i+1)^2)^(1/2) - 1))/((r - (xi(i)^2 + xi(i+1)^2)^(1/2))^2 + 1)^(1/2);
    
    %for each agent calculate repulsive gradient from all other agents (x is odd indicies, y is even)
    for j=1:2:no_robots*2
      
      %if condition to stop calculating repulsive for itself
      if j~=i
      dUxrep(j)=(Cr*(xi(i) - xi(j)))/(Lr*exp(((xi(i) - xi(j))^2 + (xi(i+1) - xi(j+1))^2)^(1/2)/Lr)*((xi(i) - xi(j))^2 + (xi(i+1) - xi(j+1))^2)^(1/2));
      dUyrep(j+1)=(Cr*(xi(i+1) - xi(j+1)))/(Lr*exp(((xi(i) - xi(j))^2 + (xi(i+1) - xi(j+1))^2)^(1/2)/Lr)*((xi(i) - xi(j))^2 + (xi(i+1) - xi(j+1))^2)^(1/2));
      else
          dUxrep(j)=0;
          dUyrep(j+1)=0;
      end   
      
    end
    
    %sum all the repulsive elements
    dUxrepsum(i)=sum(dUxrep);
    dUyrepsum(i+1)=sum(dUyrep);
    
    %reset dUrep so that previous j elements will not be summed again
    dUxrep=zeros(1,no_robots*2);
    dUyrep=zeros(1,no_robots*2);
    
    %Create dU with the sum of the attractive and respulsive gradients
    dUx(i)= dUxatt(i) + dUxrepsum(i);
    dUy(i)= dUyatt(i+1) + dUyrepsum(i+1); 
    dU(i)=dUx(i);
    dU(i+1)=dUy(i);
    
    %Transpose dU to have equations in a column
    dUcol=dU';
end
    
################ main code ############################

%Set the number of robots and arena size in simulation
no_robots=5;
arena_side=20;

%Initialise vectors to stop growing inside loop
r=zeros(1,no_robots);
x0=zeros(1,no_robots*2);
x=1;

%Create random initial agent positions (x is odd indicies, y is even)
for i=1:1:no_robots*2
    %Create initital condition between 10 and -10 (arena area)
    x0(i)=arena_side*rand()-(arena_side/2);
end

%Time span
tspan=[0 50];

[t,xi]=ode45(@APF_ALL2,tspan,x0);

#####################################################

if someone can help me out ii would be much appreciated
0
Reply Adam 11/24/2010 6:56:05 PM

7 Replies
271 Views

(page loaded in 0.104 seconds)

Similiar Articles:













7/24/2012 4:09:38 AM


Reply: