Clear Filters
Clear Filters

plotting dates over continuous numeric data

2 views (last 30 days)
john doe
john doe on 28 Apr 2018
Answered: Peter Perkins on 30 Apr 2018
So I am using ODE45 to solve a system of equations, and then I plot the solution. The problem I am having is that for each y value, the corresponding x value is a decimal in most cases. Therefore the datetime function is not working, and I cannot seem to edit the xtick values to account for it.
z=0;
Z=0;
i = [ 1 2 3 4 5];
it =[0 28 56 91 119];
j = [3 4 5 6 7];
jt = [0 28 56 91 119];
kt = [8 9 8 7 6];
k = [0 28 56 91 119];
TSPAN = [0 300];
ICC = 500;
for P_1 = [1]
for P_2 = [2]
for P_3 = [3]
[t,y] = ode45(@(t,y) ODE(t,y,P_1,P_2,P_3,it,i,jt,j,kt,k), TSPAN, ICC);
z=z+1;
Z=Z+1;
OutT{z,:} = [t];
OutY{Z,:} = [y];
end
end
end
plot(t,y);
with the ode file
function dydt = ODE(t,y,P_1,P_2,P_3,it,i,jt,j,k,kt)
i = interp1(it, i, t, 'linear', 'extrap');
j = interp1(jt, j, t, 'linear', 'extrap');
k = interp1(kt, k, t, 'linear', 'extrap');
dydt = P_1*i-P_2*j*y-P_3*k*y;
end
The dates that I want to plot correspond to [0 28 56 91 119], and they are;
07-Jul-2015
04-Aug-2015
01-Sep-2015
06-Oct-2015
03-Nov-2015
Any ideas how I can get this to work?

Answers (2)

Star Strider
Star Strider on 28 Apr 2018
Converting your ‘t’ values to date numbers and then to datetime or similar formats is likely going to be an unproductive use of your time, unless they absolutely all must be datetime variables.
I would just do something like this:
Dc = {'07-Jul-2015', '04-Aug-2015', '01-Sep-2015', '06-Oct-2015', '03-Nov-2015'};
figure
plot(t,y);
grid
set(gca, 'XTickLabel',[])
text(jt, -10*ones(size(jt)), Dc, 'Rotation',30, 'FontSize',8, 'HorizontalAlignment','right')

Peter Perkins
Peter Perkins on 30 Apr 2018
If you have this
>> x = [0 28 56 91 119]
x =
0 28 56 91 119
why not just plot your y values against this?
>> d = datetime(2015,7,7) + caldays(x)
d =
1×5 datetime array
07-Jul-2015 04-Aug-2015 01-Sep-2015 06-Oct-2015 03-Nov-2015
It's probably a bit more complicated than that, but probably not much: I guess that x vector is the ticks you want. t is, I guess, the full vector of x values, and they likely include fractional parts? But if t is relative to midnight of 7-Jul, you can plot against d = datetime(2015,7,7) + days(t), because days will accept fractional values (it assumes you mean "24 hour days" whereas caldays allows for the fact that days might also be 23 or 25 hours, and therefore a fraction is not well-defined).
Then set the ticks to the shorter datetime vector.

Categories

Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!