How to determine an x value at a specific y value?

35 Ansichten (letzte 30 Tage)
Will Jeter
Will Jeter am 12 Nov. 2020
Bearbeitet: Star Strider am 12 Nov. 2020
I need help finding the value of t and B when S=0.001. I got my plot to work but not sure how to write my code to find these values at a specific S value.
t = [0:1:50];
Y0 = [0.05 5]; % initial values
[tout, Yout] = ode15s(@func2,t,Y0)
plot(tout,Yout)
xlabel('t (Time)')
ylabel('B and S ')
legend('B','S')
title('ODE15s')
function dy = func2(t,x)
kr = 0.4;
k = 0.003;
dy = zeros(2,1);
dy(1) = (kr.*x(1).*x(2))./(k+x(2))
dy(2) = -(0.75.*kr.*x(1).*x(2))./(k+x(2))
end

Antworten (2)

Star Strider
Star Strider am 12 Nov. 2020
Bearbeitet: Star Strider am 12 Nov. 2020
Try this:
t = [0:1:50];
Y0 = [0.05 5]; % initial values
[tout, Yout] = ode15s(@func2,t,Y0);
[Smin,Sminidx] = min(Yout(:,2))
idxrng = 1:Sminidx;
Sval = 0.001;
Time_and_B_at_S = interp1(Yout(idxrng,2), [tout(idxrng) Yout(idxrng,1)], Sval)
txtstr = sprintf('\\leftarrowAt S = %.3f\n t = %.3f\n B = %.3f',Sval, Time_and_B_at_S);
figure
plot(tout,Yout)
xlabel('t (Time)')
ylabel('B and S ')
legend('B','S')
title('ODE15s')
text(Time_and_B_at_S(1), Sval, txtstr, 'HorizontalAlignment','left', 'VerticalAlignment','middle')
function dy = func2(t,x)
kr = 0.4;
k = 0.003;
dy = zeros(2,1);
dy(1) = (kr.*x(1).*x(2))./(k+x(2));
dy(2) = -(0.75.*kr.*x(1).*x(2))./(k+x(2));
end
Choose ‘Sval’ to be whatever value you want (within the limits of ‘S’).
EDIT — (12 Nov 2020 at 22:44)
I did not initially see that you specified a value for ‘S’ that you want to interpolate. (Was that added later?) I have changed my code to reflect thae requested ‘S’ value.

Walter Roberson
Walter Roberson am 12 Nov. 2020
t = [0:1:50];
Y0 = [0.05 5]; % initial values
[tout, Yout] = ode15s(@func2,t,Y0)
tout = 51×1
0 1 2 3 4 5 6 7 8 9
Yout = 51×2
0.0500 5.0000 0.0748 4.9814 0.1116 4.9538 0.1666 4.9125 0.2488 4.8509 0.3713 4.7590 0.5541 4.6219 0.8268 4.4174 1.2334 4.1124 1.8400 3.6575
plot(tout,Yout)
xlabel('t (Time)')
ylabel('B and S ')
legend('B','S')
title('ODE15s')
targetS = 0.001;
[~, minidx] = min(abs(Yout(:,2)-targetS));
fprintf('Closest time to S = %g is %g with B = %g and S = %g\n', targetS, t(minidx), Yout(minidx,:));
Closest time to S = 0.001 is 17 with B = 6.71667 and S = 4.85264e-20
function dy = func2(t,x)
kr = 0.4;
k = 0.003;
dy = zeros(2,1);
dy(1) = (kr.*x(1).*x(2))./(k+x(2));
dy(2) = -(0.75.*kr.*x(1).*x(2))./(k+x(2));
end
This is not an especially good match. To get closer, I would recommend using an Event Function to detect
x(2) - targetS
for crossing 0 and non-terminal. And use the third output of ode45() to get the time of the event.

Kategorien

Mehr zu Lighting, Transparency, and Shading finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by