From: Mirko on
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
From: Faraz Afzal on
"Mirko " <vgsangiuliano(a)gmail.com> wrote in message <i0qa85$f6e$1(a)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
From: Mirko on
"Faraz Afzal" <farazafzal(a)gmail.com> wrote in message <i0qbac$mqe$1(a)fred.mathworks.com>...
> "Mirko " <vgsangiuliano(a)gmail.com> wrote in message <i0qa85$f6e$1(a)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