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:
ode113 counting integration steps

Subject: ode113 counting integration steps

From: Mirko

Date: 4 Jul, 2010 15:44:05

Message: 1 of 3

Hi, I have this problem:

I'm using ode113 with the syntax

[t,states]=ode113(@rates,[0,t_final],[initial_conditions],);

In the rates function I compute the error by taking the difference between two states I got from ode; i have something like this in the rates function:

dydt=rates(t,states)

error=states(1)-states(2)

error_array(counter,1)=error;

if length(error_array>=2)

    if (error_array(end)-error-arrray(end-1))
      DO SOME THING WITH THE LAST TWO VALUES OF THE ARRAY
    end

end

end %end rates function

I tried to define in the main program outside the rates function a global variable called counter, set its value at 1, and modified the rates function thys way:

dydt=rates(t,states)

global counter;

error=states(1)-states(2)

error_array(counter,1)=error;

if length(error_array>=2)

    if (error_array(end)-error-arrray(end-1))
      DO SOME THING WITH THE LAST TWO VALUES OF THE ARRAY
    end

end
counter=counter+1;
end %end rates function

but it doesn't work, for example the counter variable at the end of the integration process has a vallue that is twice-1 the number of the length of the t or states arrays.

Can someone help me?

Thanks

Subject: ode113 counting integration steps

From: Faraz Afzal

Date: 4 Jul, 2010 16:02:20

Message: 2 of 3

"Mirko " <vgsangiuliano@gmail.com> wrote in message <i0qa85$f6e$1@fred.mathworks.com>...
> Hi, I have this problem:
>
> I'm using ode113 with the syntax
>
> [t,states]=ode113(@rates,[0,t_final],[initial_conditions],);
>
> In the rates function I compute the error by taking the difference between two states I got from ode; i have something like this in the rates function:
>
> dydt=rates(t,states)
>
> error=states(1)-states(2)
>
> error_array(counter,1)=error;
>
> if length(error_array>=2)
>
> if (error_array(end)-error-arrray(end-1))
> DO SOME THING WITH THE LAST TWO VALUES OF THE ARRAY
> end
>
> end
>
> end %end rates function
>
> I tried to define in the main program outside the rates function a global variable called counter, set its value at 1, and modified the rates function thys way:
>
> dydt=rates(t,states)
>
> global counter;
>
> error=states(1)-states(2)
>
> error_array(counter,1)=error;
>
> if length(error_array>=2)
>
> if (error_array(end)-error-arrray(end-1))
> DO SOME THING WITH THE LAST TWO VALUES OF THE ARRAY
> end
>
> end
> counter=counter+1;
> end %end rates function
>
> but it doesn't work, for example the counter variable at the end of the integration process has a vallue that is twice-1 the number of the length of the t or states arrays.
>
> Can someone help me?
>
> Thanks

I tried to go through your problem about twice. I tried to figure out what you expect from MATLAB to do , what is your target with this code, But I am really unable to understand it.. Your code is readable very much self explanatory but I am sure once i know what you exxpect from MATLAB after runnign this code I can hellp you.. Atleast i will try..

Let me know more , or may be someone else take over this..

Regards,
Faraz

Subject: ode113 counting integration steps

From: Mirko

Date: 4 Jul, 2010 16:10:38

Message: 3 of 3

"Faraz Afzal" <farazafzal@gmail.com> wrote in message <i0qbac$mqe$1@fred.mathworks.com>...
> "Mirko " <vgsangiuliano@gmail.com> wrote in message <i0qa85$f6e$1@fred.mathworks.com>...
> > Hi, I have this problem:
> >
> > I'm using ode113 with the syntax
> >
> > [t,states]=ode113(@rates,[0,t_final],[initial_conditions],);
> >
> > In the rates function I compute the error by taking the difference between two states I got from ode; i have something like this in the rates function:
> >
> > dydt=rates(t,states)
> >
> > error=states(1)-states(2)
> >
> > error_array(counter,1)=error;
> >
> > if length(error_array>=2)
> >
> > if (error_array(end)-error-arrray(end-1))
> > DO SOME THING WITH THE LAST TWO VALUES OF THE ARRAY
> > end
> >
> > end
> >
> > end %end rates function
> >
> > I tried to define in the main program outside the rates function a global variable called counter, set its value at 1, and modified the rates function thys way:
> >
> > dydt=rates(t,states)
> >
> > global counter;
> >
> > error=states(1)-states(2)
> >
> > error_array(counter,1)=error;
> >
> > if length(error_array>=2)
> >
> > if (error_array(end)-error-arrray(end-1))
> > DO SOME THING WITH THE LAST TWO VALUES OF THE ARRAY
> > end
> >
> > end
> > counter=counter+1;
> > end %end rates function
> >
> > but it doesn't work, for example the counter variable at the end of the integration process has a vallue that is twice-1 the number of the length of the t or states arrays.
> >
> > Can someone help me?
> >
> > Thanks
>
> I tried to go through your problem about twice. I tried to figure out what you expect from MATLAB to do , what is your target with this code, But I am really unable to understand it.. Your code is readable very much self explanatory but I am sure once i know what you exxpect from MATLAB after runnign this code I can hellp you.. Atleast i will try..
>
> Let me know more , or may be someone else take over this..
>
> Regards,
> Faraz

Thank you for replying.

I found on the web that using the 'OutputFcn' property of odeset I could call a function to increment a counter.
When Matlab integrates my function it does many steps and many calls to my function rates, so in my case i got much contings than the successful ones.
I read about the 'OutputFcn' property of odeset that should call a function defined in this property only when successful integration occurs, but I'm not able to write such a function to get my goal.

Regards
Mirko

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