# Finding y-intercept for loop

조회 수: 4 (최근 30일)
University 2023년 10월 27일
댓글: Star Strider 2023년 10월 27일
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

댓글을 달려면 로그인하십시오.

### 채택된 답변

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 —
u = squeeze(LD1.ux);
Sizeu = size(u)
Sizeu = 1×2
24 31
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이전 댓글 10개 표시이전 댓글 10개 숨기기
University 2023년 10월 27일
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 2023년 10월 27일
As always, my pleasure!
I am glad it now works as you want it to!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by