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).
Star Strider
2023 年 10 月 27 日
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.
Star Strider
2023 年 10 月 27 日
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.
Star Strider
2023 年 10 月 27 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
