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

조회 수: 23 (최근 30일)
Tom Goodland
Tom Goodland 2022년 1월 17일
편집: Star Strider 2022년 1월 17일
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.

답변 (2개)

KSSV
KSSV 2022년 1월 17일
편집: KSSV 2022년 1월 17일
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
KSSV
KSSV 2022년 1월 17일
Try interp1 again.
Tom Goodland
Tom Goodland 2022년 1월 17일
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);

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2022년 1월 17일
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
Tom Goodland
Tom Goodland 2022년 1월 17일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by