How to determine an x value at a specific y value?
36 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Star Strider
on 12 Nov 2020
Edited: Star Strider
on 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.
0 Comments
Walter Roberson
on 12 Nov 2020
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')
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,:));
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.
0 Comments
See Also
Categories
Find more on Lighting, Transparency, and Shading 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!