Finding y-intercept for loop

3 visualizaciones (últimos 30 días)
University
University el 27 de Oct. de 2023
Comentada: Star Strider el 27 de Oct. de 2023
Hi,
In the m.uvals file I 24 x 1 x 31 matrix, 24 are the index of xivals and 31 are the values of u and m.array file is the y array.
Please how can find the y-intercept for each value of xivals? I have tried code below for one of xivals, which seems to work. Also why is the value of the intercept not at the point where y exactly intercept x ?
xivals =[0:1:21 60 100];
y=yarray;
V=squeeze(u(1, :,:))';
loc=diff(sign(V)) & abs(y(1:end-1))<0.001;
yintercept=y(loc);
figure(1)
plot(y,V,yintercept,0,'rx');
yline(0);
xlabel y-coordinate;
ylabel Velocity

Respuesta aceptada

Star Strider
Star Strider el 27 de Oct. de 2023
I made some minor changes to your code, and added a loop to return reasonably precise values of ‘y’ for each intersection. Your idea was appropriate, however the indices themselves will not be accurate enough for most purposes. It is necessary to interpolate to get reasonably precise values.
Try this —
LD1 = load('uval.mat');
u = squeeze(LD1.ux);
Sizeu = size(u)
Sizeu = 1×2
24 31
LD2 = load('yarray.mat');
yarray = LD2.yarray;
xivals =[0:1:21 60 100];
y=yarray;
V=u(1,:);
loc=diff(sign(V)) & abs(y(1:end-1))<0.001;
% yintercept=y(loc)
intsx = find(diff(sign(V))); % Approximate Indices Of Intersections
for k = 1:numel(intsx)
idxrng = max(1,intsx(k)-1) : min(numel(V),intsx(k)+1); % Index Range
yintercept(k) = interp1(V(idxrng), y(idxrng), 0); % Interpolated Values Of Intersections
end
yintercept
yintercept = 1×2
1.0e-06 * -0.6311 1.0000
figure(1)
plot(y,V,yintercept,0,'rx');
yline(0);
xlabel y-coordinate;
ylabel Velocity
.
  12 comentarios
University
University el 27 de Oct. de 2023
Thank you so much for your help. I have been able to get through it. I just did this:
plot(xivals, yintercept(:, 1), xivals, yintercept(:, 2)).
Star Strider
Star Strider el 27 de Oct. de 2023
As always, my pleasure!
I am glad it now works as you want it to!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by