Finding y-intercept for loop
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Star Strider
2023년 10월 27일
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)
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
figure(1)
plot(y,V,yintercept,0,'rx');
yline(0);
xlabel y-coordinate;
ylabel Velocity
.
댓글 수: 12
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!