Finding y-intercept for loop
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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 —
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
University
2023년 10월 27일
편집: University
2023년 10월 27일
Thank you so much for your assisstance. I'm finding it difficult to plot the intercept against xivals. Is there any way I can do that? I was expected the length of yintercept to be the same as the length of xivals. From the V=u(1,:); I guess you just pick the first index of xivals. I want to loop over all the xivals so that I can plot plot(xivals,yintercept).
As always, my pleasure!
The problem is that they are not the same lengths, so plotting them against each other is not possible. They share the row size of‘u’ (‘u’ is 24x31) not the column size, and that defines ‘V’. The only way to make them the same lengtrhs is to interpolate ‘u’ to be the same size as ‘xivals’ (or the reverse of that) or to plot the columns of ‘u’ against ‘xivals’, however that does not work because of how ‘u’ is defined. It does not appear to have any intercepts in that direction. You could possibly take the intercepts of all the rows of ‘V’ and plot that against ‘xivals’.
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)
for k1 = 1:size(u,1)
V = u(k1,:);
intsx = find(diff(sign(V))); % Approximate Indices Of Intersections
for k2 = 1:numel(intsx)
idxrng = max(1,intsx(k2)-1) : min(numel(V),intsx(k2)+1); % Index Range
yintercept(k1,k2) = interp1(V(idxrng), y(idxrng), 0); % Interpolated Values Of Intersections
end
end
yintercept
yintercept = 24×2
1.0e-06 *
-0.6311 1.0000
-0.0016 1.0000
-0.0845 1.0000
-0.4855 1.0000
-0.0900 1.0000
-0.0956 1.0000
-0.1011 1.0000
-0.1067 1.0000
-0.1124 1.0000
-0.1181 1.0000
v1 = ones(size(u(1,:)));
figure(1)
hold on
for k = 1:size(u,1)
plot3(y,u(k,:),v1*xivals(k))
plot3(yintercept(k,:),[0 0], [1 1]*xivals(k),'rx');
end
hold off
zl = zlim;
patch([xlim flip(xlim)], zeros(1,4), [[1 1]*zl(1) [1 1]*zl(2)], [1 1 1]*0.5, 'FaceAlpha',0.25, 'EdgeColor','none')
xlabel y-coordinate;
ylabel Velocity
zlabel xivals
grid on
view(-27.5,30)

Fortunately, there are only two values of ‘yintercept’ for each row of ‘u’ so regular matrices (not cell arrays) will work to store them. The gray patch replaces the yline call here.
.
University
2023년 10월 27일
편집: University
2023년 10월 27일
Thank you so much Star.... So if can make u size the same as that of the xivals size, I can then plot (xivals, intercept) ? So here i have attached uxl.m file which is the same size as xivals
xivals =[0 0.001 0.01 0.1 0.2 0.5 0.6 0.7 0.8 1:1:20 60 100];
I would appreciate it if you could use this to plot (xivals, y-intercept). xivals is 31 and uxl is 31 now.
As always, my pleasure!
The row size of ‘u’ (24) ias already the same size as ‘xivals’. The only way to plot ‘yintercept’ as a function of ‘xivals’ is the way I did it using a 3D representation. Interpolating ‘xivals’ to be (1x31) could be possible, however I doubt that it would be correct.
If you want to do it, this works —
xivals =[0:1:21 60 100]
xivals = 1×24
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 60 100
xq = linspace(1, numel(xivals), 31); % Query Vector
xivals31 = interp1((1:numel(xivals)), xivals, xq)
xivals31 = 1×31
0 0.7667 1.5333 2.3000 3.0667 3.8333 4.6000 5.3667 6.1333 6.9000 7.6667 8.4333 9.2000 9.9667 10.7333 11.5000 12.2667 13.0333 13.8000 14.5667 15.3333 16.1000 16.8667 17.6333 18.4000 19.1667 19.9333 20.7000 39.2000 69.3333
.
University
2023년 10월 27일
편집: University
2023년 10월 27일
Sorry, I had to upload the u which has the same size as xivals now.
xivals =[0 0.001 0.01 0.1 0.2 0.5 0.6 0.7 0.8 1:1:20 60 100];
and uxl.m file has 31 x 1x 31 now.
Star Strider
2023년 10월 27일
The ‘u’ originally provided was (24x31). A (31x31) version should work with the original code, however ‘xivals’ is still (1x24) so that problem persists, unless there is a new version of ‘xivals’.
University
2023년 10월 27일
편집: University
2023년 10월 27일
So sorry , the new xivals is 31 also Strider.
size(xivals)
ans = 1 31
That's new xivals.
Star Strider
2023년 10월 27일
What do you want to do with the new ‘xivals’ vector?
If you want my help with it, I need the new ‘u’ matrix as well, and to know what you want to do.
-----
(My apologies for the delay. I just wasted about 30 minutes recovering my desktop machine from yet another Win 11 crash caused by some sort of bloatware call ‘autopilot’ that then completely corrupted my desktop layout as well. I hate Windows. My next computer is going to be Linux. If that also means that I can’t use any other Micro$oft apps, being completely free from Micro$oft works for me.)
University
2023년 10월 27일
Sorry about that Strider. Attached the new u values and corresponding new xivals is
xivals =[0 0.001 0.01 0.1 0.2 0.5 0.6 0.7 0.8 1:1:20 60 100];
I just want a measure of plot(xivals, yintercept(1:, 2)). Something like that.
I am a bit confused.
Do you want to do the same thing with ‘y’ or should ‘xivals’ replace ‘y’ in the code?
Try this (previous version with new ‘u’ and ‘xivals’) —
LD1 = load('uval31.mat');
u = squeeze(LD1.ux);
Sizeu = size(u)
Sizeu = 1×2
31 31
LD2 = load('yarray.mat');
yarray = LD2.yarray;
xivals =[0 0.001 0.01 0.1 0.2 0.5 0.6 0.7 0.8 1:1:20 60 100];
y=yarray;
V=u(1,:);
loc=diff(sign(V)) & abs(y(1:end-1))<0.001;
% yintercept=y(loc)
for k1 = 1:size(u,1)
V = u(k1,:);
intsx = find(diff(sign(V))); % Approximate Indices Of Intersections
for k2 = 1:numel(intsx)
idxrng = max(1,intsx(k2)-1) : min(numel(V),intsx(k2)+1); % Index Range
yintercept(k1,k2) = interp1(V(idxrng), xivals(idxrng), 0); % Interpolated Values Of Intersections
end
end
yintercept
yintercept = 31×2
0.5533 100.0000
0.5428 100.0000
0.2978 100.0000
13.5207 100.0000
9.9913 100.0000
7.7154 100.0000
7.4728 100.0000
7.2979 100.0000
7.1652 100.0000
6.9767 100.0000
v1 = ones(size(u(1,:)));
figure(1)
hold on
for k = 1:size(u,1)
plot3(y,u(k,:),v1*xivals(k))
plot3(yintercept(k,:),[0 0], [1 1]*xivals(k),'rx');
end
hold off
zl = zlim;
patch([xlim flip(xlim)], zeros(1,4), [[1 1]*zl(1) [1 1]*zl(2)], [1 1 1]*0.5, 'FaceAlpha',0.25, 'EdgeColor','none')
xlabel y-coordinate;
ylabel Velocity
zlabel xivals
grid on
view(-27.5,30)

.
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!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Exploration and Visualization에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
