How to determine an x value at a specific y value?
35 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Star Strider
2020-11-12
编辑:Star Strider
2020-11-12
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 个评论
Walter Roberson
2020-11-12
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!