Hi all,
I have the following function:
function [m,yinter] = radial_lines(X,Y,xi,yi)
if X==Y
m=inf;
yinter= X;
return
end
% Calculate the slope:
m = (Y - yi)./((X) - xi);
% Solve for y-intercept
yinter = yi - m.*xi;
fplot(yinter)
end
%% data
X=[6,7,10,14,13,9];
Y=[12,9,7,8,10,14];
xi=10;
yi=10;
The issue i am running into is that when using the radial_lines function, I get a plot with horizontal lines. However I need the lines to have a slope. I am not quite sure what i am doing wrong when calculating the slopes. I should be getting an arry of m values but the code does not give me that output. Can some one kinldy help me out? Thanks.

6 件のコメント

Star Strider
Star Strider 2022 年 10 月 1 日
I am not certain what your code is doing.
An easier way to calculate the parameters for a linear regression is:
%% data
X=[6,7,10,14,13,9];
Y=[12,9,7,8,10,14];
B = [X(:) ones(size(X(:)))] \ Y(:) % Parameters: B(1) = m, B(2) = b
B = 2×1
-0.3344 13.2885
Yfit = [X(:) ones(size(X(:)))] * B; % Regression Fit (For 'plot’)
figure
plot(X, Y, 'p', 'DisplayName','Data')
hold on
plot(X, Yfit, '-r', 'DisplayName','Regression')
hold off
grid
xlabel('X')
ylabel('Y')
legend('Location','best')
axis([5 15 6 15])
If you want statistics on the fit, use fitlm.
Mariam Shahab
Mariam Shahab 2022 年 10 月 1 日
Thank you for your resposne. Just to clreaify, i am not calutaling a linear regression. I am trying to get the equations of lines that start from (xi,yi ). The hexagon illustatred is formed by X and Y arrays.
Torsten
Torsten 2022 年 10 月 1 日
I am trying to get the equations of lines that start from (xi,yi ).
Then
yinter = Y - m.*X
not
yinter = yi - m.*xi
Mariam Shahab
Mariam Shahab 2022 年 10 月 1 日
Ok, thank you. I am still getting horizontal lines though (so m=0). Any advice for the slope of theses lines?
Torsten
Torsten 2022 年 10 月 1 日
編集済み: Torsten 2022 年 10 月 1 日
Slope and y-intercept can be obtained from my answer given below.
X=[6,7,10,14,13,9];
Y=[12,9,7,8,10,14];
xi=10;
yi=10;
hold on
for i=1:6
plot([X(i),xi],[Y(i),yi])
end
hold off
Mariam Shahab
Mariam Shahab 2022 年 10 月 1 日
Thank you for your help. This is the graph I was aimming for.

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2022 年 10 月 1 日

0 投票

That clears up much.
Plotting them is simply this —
%% data
X=[6,7,10,14,13,9];
Y=[12,9,7,8,10,14];
xi=10;
yi=10;
figure
plot([ones(size(X))*xi; X], [ones(size(Y))*yi; Y], '-k')
grid
axis([5 15 6 15])
axis('equal')
text(X*1.01,Y*1.01,compose('(%2d,%2d)',[X;Y].'))
It is not necessary to plot the individual regressions, although you can certainly do that if you want to. Getting the slopes and intercepts may be enough, and your function does that.
.

その他の回答 (1 件)

Torsten
Torsten 2022 年 10 月 1 日
編集済み: Torsten 2022 年 10 月 1 日

0 投票

yinter is not a function. So use "plot" instead of "fplot" (whatever abscissa data you might want to choose).
Not that yinter(3) = Inf which is not plotted.
%% data
X=[6,7,10,14,13,9];
Y=[12,9,7,8,10,14];
xi=10;
yi=10;
[m yinter] = radial_lines(X,Y,xi,yi)
m = 1×6
-0.5000 0.3333 Inf -0.5000 0 -4.0000
yinter = 1×6
15.0000 6.6667 Inf 15.0000 10.0000 50.0000
function [m,yinter] = radial_lines(X,Y,xi,yi)
for i=1:numel(X)
if X(i)==xi
m(i)=inf;
yinter(i)= inf;
else
% Calculate the slope:
m(i) = (Y(i) - yi)/(X(i) - xi);
% Solve for y-intercept
yinter(i) = Y(i) - m(i)*X(i);
end
end
end

カテゴリ

ヘルプ センター および File ExchangeSpline Postprocessing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by