Trying to find an x value from a certain y value on my graph

37 views (last 30 days)
I have a graph of reactor temperature against time and I am trying to find the time the reactor temperature is at 455K.
To make this graph I used ode15 function [t,y] = ode15s(@(t,y)fun(t,y(1),y(2),y(3),y(4),y(5),y(6)), tspan, initial_conditions);
then plotted the graph using plot: figure(3) plot(t,T_2)
Does anyone know how to write code that would be able to find the time value?
I would appreciate any help, thanks.

Answers (2)

KSSV
KSSV on 17 Jan 2022
Edited: KSSV on 17 Jan 2022
idx = knnsearch(T_2,455) ;
t(idx)
If there is one-to-one mapping between (t,T_2)
iwant = interp1(T_2,t,455)
  4 Comments
Tom Goodland
Tom Goodland on 17 Jan 2022
I used it again for the concentration plot, it worked the first time I did it with Ca, however when I tried to use it cb, cs and cd I got some errors, could this be due to the one-on-one mapping you mentioned?
plot(t,[ca,cb,cs,cd])
idx = knnsearch(0.9378,ca) ;
t(idx)
iwant = interp1(ca,t,0.9378);% ca = 1.1587
idx = knnsearch(0.9378,cb) ;
t(idx)
iwant = interp1(cb,t,0.9378);
idx = knnsearch(0.9378,cs) ;
t(idx)
iwant = interp1(cs,t,0.9378);
idx = knnsearch(0.9378,cd) ;
t(idx)
iwant = interp1(cd,t,0.9378);
The errors I got were:
Error using matlab.internal.math.interp1
Sample points must be unique.
Error in interp1 (line 188)
VqLite = matlab.internal.math.interp1(X,V,method,method,Xqcol);
Error in Twob (line 21)
iwant = interp1(cb,t,0.9378);

Sign in to comment.


Star Strider
Star Strider on 17 Jan 2022
I will re-post my original Answer because I believe it addresses the problem (although without the data, it is difficult to determine that with certainty) —
t = linspace(0, 10);
y = sin(2*pi*t/3) + t/10;
yval = 1;
idx = find(diff(sign(y-yval))); % Approximate Index Values For 'yval'
for k = 1:numel(idx)
idxrng = [max([1 idx(k)-2]) : min([numel(t) idx(k)+2])]; % Index Range For Interpolation
tval(k) = interp1(y(idxrng), t(idxrng), yval); % Interpolate
end
tval
tval = 1×7
0.5893 0.9546 3.3487 4.2029 6.1877 7.3731 9.0459
figure
plot(t,y)
hold on
plot(tval, ones(size(tval))*yval, '+r', 'MarkerSize',7.5)
hold off
grid
.
  8 Comments
Tom Goodland
Tom Goodland on 17 Jan 2022
Thorsten I was looking at the ode event page on matlab last night and got quite confused, could you help define this event when T = 455 K please?

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!