필터 지우기
필터 지우기

Why does polyfit gives two outputs?

조회 수: 6 (최근 30일)
Rahul
Rahul 2023년 6월 18일
댓글: the cyclist 2023년 6월 19일
Hi,
I want to plot a line passing through a set of points, which is as in the code below.
To determine the slope I used polyfit.
But it gives two outputs.
I wonder which output I should consider out of the two outputs it is giving me?
clc;
clear;
phi_1 = [0.1724*pi 0.1472*pi 2.18*pi 2.25*pi 4*pi 3.588*pi 1.012*pi 4.112*pi 4.31*pi 4.25*pi 0.832*pi];
x1 = linspace(0,1,11);
figure(1)
plot(x1,phi_1,'o');
coefficients1=polyfit(x1,phi_1,1)
coefficients1 = 1×2
7.6641 3.8373
slope1=coefficients1(1);
With regards,
rc

채택된 답변

Matt J
Matt J 2023년 6월 18일
편집: Matt J 2023년 6월 18일
It doesn't give 2 outputs unless you call it with 2 output arguments. The one output that it is giving you is a vector with 2 coefficients (a slope and an intercept) because that's what a first order polynomial has.

추가 답변 (1개)

the cyclist
the cyclist 2023년 6월 18일
편집: the cyclist 2023년 6월 18일
The first line of the function description on the documentation for polyfit states precisely what those outputs are.
They are the coefficients of the descending powers of the fitted polynomial. You asked for a 1st-order polynomial, so the first element of the output is the coefficient of the linear (1st-order) term, and the second element of the output is the coefficient of the intercept (the 0th-order term).
clc;
clear;
phi_1 = [0.1724*pi 0.1472*pi 2.18*pi 2.25*pi 4*pi 3.588*pi 1.012*pi 4.112*pi 4.31*pi 4.25*pi 0.832*pi];
x1 = linspace(0,1,11);
coefficients1=polyfit(x1,phi_1,1)
coefficients1 = 1×2
7.6641 3.8373
slope1=coefficients1(1);
intercept1=coefficients1(2)
intercept1 = 3.8373
figure(1)
hold on
plot(x1,phi_1,'o');
plot([0 1],intercept1 + slope1*[0 1])
  댓글 수: 2
Rahul
Rahul 2023년 6월 19일
Hi Matt & the cyclist,
Thanks a lot for your reply.It indeed helped me.
I also wish to convert x1 from linear scale to angular scale (in radians)
I can see in the documentation, probably dist is a function which can be used but I'm not sure how to implement the parameters quatA and quatB.
kindly advice.
Sincerely,
rc
the cyclist
the cyclist 2023년 6월 19일
Assuming that you are not working with quaternions, I doubt that dist is the function you need.
Please give more detail about what you are trying to do. (You might actually want to open a new question, since your original one has been answered.)

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by