Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
using ode solver in loop for different integration steps for transient coefficients

Subject: using ode solver in loop for different integration steps for transient coefficients

From: salman

Date: 13 Jun, 2012 10:43:07

Message: 1 of 1

Dear friends,

i have a set of four equations coupled, i am using the ode solver to solve all of them for two variables, t and del, i.e. i am solving all the four equations for every value of del, as follows;
%---------------------
a=540000;
x=-a:2500:a;
del=(1/(sqrt(2*pi)*a))*exp(-.5*(x./a).^2);
for u=1:length(del)
        [T,Y]=ode23tb(@(t1,y)system1(t1,y,x(u)),t1,s1);%t=first part of integration
        Y_c1(:,:,u)=Y(:,:);
end
%------------------------
but my stimulus for the system is changing in time, (2 pulses actually), so i divide my time itegration into 5 steps, three are when stimulus is zero, and the intermediate 2 are when stimulus is no zero. but the problem that i have is how to store the values of each quantity at the final integration step to be passed to next one as intitial values,becasue i have a second variable del, so i have to store final values of each of the four quantity fo every value of del. i did it like this;

for u=1:length(del)
        [T,Y]=ode23tb(@(t1,y)system1(t1,y,x(u)),t1,s1);%t=first part of integration
        Y_c1(:,:,u)=Y(:,:);
end

for u=1:length(del)
        k_c=k;
        [T,Y]=ode23tb(@(t2,y)system2(t2,y,x(u)),t2,s2);
        Y_c2(:,:,u)=Y(:,:);
end
for u=1:length(del)
        k_c=0;
        [T,Y]=ode23tb(@(t3,y)system3(t3,y,x(u)),t3,s3);
        Y_c3(:,:,u)=Y(:,:);
end
for u=1:length(del)
        k_c=k2;
        [T,Y]=ode23tb(@(t4,y)system4(t4,y,x(u)),t4,s4);
        Y_c4(:,:,u)=Y(:,:);
end
for u=1:length(del)
        k_c=0;
        [T,Y]=ode23tb(@(t5,y)system5(t5,y,x(u)),t5,s5);
        Y_c5(:,:,u)=Y(:,:);
end
       

function dydt1=system1(t1,y,del)
k_c=0;
dydt1=zeros(numel(y),1);
%del=250000;
gamma12=0;
gamma=.0000;
dydt1(1)=-(j/2)*k_c.*(y(3)-y(4))+gamma*y(2);
dydt1(2)=(j/2)*k_c.*(y(3)-y(4))-gamma*y(2);
dydt1(3)=+(j/2)*k_c.*(y(2)-y(1))-gamma12*y(3)+j*del*1*y(3);
dydt1(4)=-(j/2)*k_c.*(y(2)-y(1))+gamma12*y(4)-j*del*1*y(4);

function dydt2=system2(t2,y,del)
k_c=k;
dydt2=zeros(numel(y),1);
%del=250000;
gamma12=0;
gamma=.0000;
dydt2(1)=-(j/2)*k_c.*(y(3)-y(4))+gamma*y(2);
dydt2(2)=(j/2)*k_c.*(y(3)-y(4))-gamma*y(2);
dydt2(3)=+(j/2)*k_c.*(y(2)-y(1))-gamma12*y(3)+j*del*1*y(3);
dydt2(4)=-(j/2)*k_c.*(y(2)-y(1))+gamma12*y(4)-j*del*1*y(4);

function dydt3=system3(t3,y,del)
k_c=0;
dydt3=zeros(numel(y),1);
%del=250000;
gamma12=0;
gamma=.0000;
dydt3(1)=-(j/2)*k_c.*(y(3)-y(4))+gamma*y(2);
dydt3(2)=(j/2)*k_c.*(y(3)-y(4))-gamma*y(2);
dydt3(3)=+(j/2)*k_c.*(y(2)-y(1))-gamma12*y(3)+j*del*1*y(3);
dydt3(4)=-(j/2)*k_c.*(y(2)-y(1))+gamma12*y(4)-j*del*1*y(4);

function dydt4=system4(t4,y,del)
k_c=k2;
dydt4=zeros(numel(y),1);
%del=250000;
gamma12=0;
gamma=.0000;
dydt4(1)=-(j/2)*k_c.*(y(3)-y(4))+gamma*y(2);
dydt4(2)=(j/2)*k_c.*(y(3)-y(4))-gamma*y(2);
dydt4(3)=+(j/2)*k_c.*(y(2)-y(1))-gamma12*y(3)+j*del*1*y(3);
dydt4(4)=-(j/2)*k_c.*(y(2)-y(1))+gamma12*y(4)-j*del*1*y(4);

function dydt5=system5(t5,y,del)
k_c=0;
dydt5=zeros(numel(y),1);
gamma12=0;
gamma=.0000;
dydt5(1)=-(j/2)*k_c.*(y(3)-y(4))+gamma*y(2);
dydt5(2)=(j/2)*k_c.*(y(3)-y(4))-gamma*y(2);
dydt5(3)=+(j/2)*k_c.*(y(2)-y(1))-gamma12*y(3)+j*del*1*y(3);
dydt5(4)=-(j/2)*k_c.*(y(2)-y(1))+gamma12*y(4)-j*del*1*y(4);

finally i will concatenate all the arrays Y_c1, Y_c2,Y_c3,Y_c4,Y_c5 to get final ressult

but the problem is i cant imagine how to store the final values (as initial values fo the next run becasue of the second variable del).

any help is greatlyt appreciated.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us