Use fzero but I have arrays

11 vues (au cours des 30 derniers jours)
Anastasia Zistatsis
Anastasia Zistatsis le 24 Mar 2021
I need to use fzero() to determine when the liquid stops flowing @ Q = 0. I know I need to create an equation or loop of some sort to use fzero, but I can't figure it out.
Here's my data:
t = [0,500,1000,1500,2200,2900,3600,4300,5200,6500,7000,7500];
Q = [10.55,9.576,9.072,8.640,8.100,7.560,7.020,6.480,5.688,4.752,3.348,1.404];
A hint would be incredibly helpful.
  1 commentaire
Jan
Jan le 24 Mar 2021
This is not a smooth curve. It will be more or less arbitrary, which function you will fit to the data. A linear interpolation of the 3 smallest values might be smart enough.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 24 Mar 2021
Modifié(e) : Star Strider le 24 Mar 2021
The fzero function is not appropriate here. Use interp1 instead:
t = [0,500,1000,1500,2200,2900,3600,4300,5200,6500,7000,7500];
Q = [10.55,9.576,9.072,8.640,8.100,7.560,7.020,6.480,5.688,4.752,3.348,1.404];
Q0T = interp1(Q, t, 0, 'pchip','extrap')
figure
plot(t, Q)
hold on
plot(Q0T, 0, 'xr')
hold off
grid
xlabel('t')
ylabel('Q')
legend('Data','Q_0')
text(Q0T, 0, sprintf('Flow stops at %.2f\n \\downarrow', Q0T), 'horiz','right','vert','bottom')
EDIT — (24 Mar 2021 at 21:04)
Added plot figure —
.
  2 commentaires
Anastasia Zistatsis
Anastasia Zistatsis le 24 Mar 2021
Thank you, that is incredibly helpful! Now I'm supposed to use spline for interp1. I suppose I should have mentioned that before.
When changing things around for spline, using this code I end up with this graph.
i = interp1(t, Q, 0, 'spline')
figure
plot(t, Q,'b',i,0,'xr')
xlabel('t')
ylabel('Q')
Anastasia Zistatsis
Anastasia Zistatsis le 24 Mar 2021
I figured out how to use fzero by making the interp1 line a function of x, but I'd like to get it using your method as well

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 24 Mar 2021
Modifié(e) : Jan le 24 Mar 2021
This is not a smooth curve. It will be more or less arbitrary, which function you will fit to the data. A linear interpolation of the 3 rightmost values might be smart enough:
a = polyfit(t(end-2:end), Q(end-2:end), 1)
x0 = -a(2) / a(1) % 7946.2

Community Treasure Hunt

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

Start Hunting!

Translated by