How to describe value changes in constant intervals?

Hi everyone, I have problem with describe value change in time in constant intervals in ode45 function. When I describe
A=200*(t>=0 & t<5)+300*(t>=5 & t<10)+550*(t>=10 & t<15)...etc.
then ode 45 understand this value change in time, but I have much more intervals from excel data and I want automatization this process of describing.
Let's say I have what the following:
tspan=(0:60:1800);
It=[1000;600;1200]; % after processing Excel data
l=0;
for k=1:3
while (l<600*k & l>=600*k-600)
A=It(k);
l=l+60;
end
end
However in this case, the ode function accepts only the last 1200 and calculates the result, for 1200 for the whole time.
Have anybody any ideas how to solve this problem?

2 Comments

I do not understand, what you want to achieve.
By the way, do you run ODE45 on a function, which is not smoothly differentiable? This would be a bad idea, see http://www.mathworks.com/matlabcentral/answers/59582#answer_72047 .
In standard differential equations I have constant inputs for ode solver and I get for example that plot:
But I want enter value which is variable and for each constant time interval could have same or different value. I would like get back response like this:
I received it through describe each time interval by hand. Therefore my question is: Is it any possible to use loop function for get similar effect? I have massive quantity data and it is nearly impossible to prescribe all of it by hand without mistake. Even if I will start prescribe it without mistake it will take me infinity.

Sign in to comment.

 Accepted Answer

Bruno Luong
Bruno Luong on 24 Oct 2018
Edited: Bruno Luong on 24 Oct 2018
Take a look at this old thread. In my answer I use tbin and s to store a piecewise constant function
S(t) := s(i) for t(i) <= t < tbin(i+1)
and pass this 2 parameters to ode45, and in turn used by the derivative function, using HISTC function.

7 Comments

I do not really see how to adapt your script to mine, I tried to use the 'piecewise' function, but I could not do something meaningful.
Your example of
A=200*(t>=0 & t<5)+300*(t>=5 & t<10)+550*(t>=10 & t<15)...etc.
can be implemented differently as following:
tbin = [0 5 10 15]; % <- n break points of the time t, n=4 here
s = [200, 300, 550]; % <- put (n-1) values read from your excel here
s(end+1) = s(end);
tspan = ... % initialize your tspan
x0 = ... % initialize Cauchy condition
[t,x] = ode15s(@(t,x) odefun(t,x,tbin,s), tspan, x0);
function dxdt = odefun(t,x,tbin,s)
[~,i] = histc(t,tbin);
A = s(i); % <- here is the value of A
dxdt = ... % compute the derivative using, t, x, A
end
If you might have any fear about solving ODE in one shoot with discontinuity (see Jan's warning), I'll indicate the right way to deal with. I don't have yet opinion as long as haven't seen your equations.
A lot of thanks! It seems it working correctly. I need one more your good advice, how I can call out graph of this calculate. Please tell me easiest way.
Do you mean calling
figure()
plot(t,x)
after getting solution from ode15s?
Yes, when I wrote before end
figure()
plot(t,x)
I got back a few empty chart windows. And I don't know which syntax I have to use to get all chart in one window after solution ode15s. I guess I should use the phrase "hold on", but my attempts were wrong.
I suggest you to open a new question on plotting issue, since it has nothing to do with this thread.

Sign in to comment.

More Answers (0)

Products

Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!