필터 지우기
필터 지우기

polyfit in a for loop

조회 수: 7 (최근 30일)
shobhit mehrotra
shobhit mehrotra 2015년 2월 22일
댓글: Greig 2015년 2월 22일
I want to perform a polyfit in a for loop
for n=1:270
slopex(n) = polyfit(cos(n^3), 3*n, 1)
end
I want to store only the 270 slope values in the vector "slopex"
thanks!

채택된 답변

Greig
Greig 2015년 2월 22일
Firstly, this is a poorly conditioned problem. You are trying to fit a straight line through a single data point, so there is no unique solution.
I guess you are wanting the slope to pass through the origin? If so, and since you are then only fitting to two points, you want to use
polyfit([0, cos(n^3)], [0, 3*n], 1)
So following the loop approach, you want to use something like..
for n=1:270
Data_Fits(n,:) = polyfit([0, cos(n^3)], [0, 3*n], 1);
end
slopex=Data_Fits(:,1);
This can be done without the loop, but this only works for the simple case of passing through the origin...
n=1:270;
slopex=(3.*n)./cos(n.^3);
As a point of note, n ranges up to 270, which looks to me to be an angle in degrees? If so, you should note that cos() assumes radians, so you should use cosd() instead, or convert n to radians. Of course, if this is not what you're doing, you can completely ignore this last point.
  댓글 수: 2
shobhit mehrotra
shobhit mehrotra 2015년 2월 22일
Thank You I needed the (n, :) part
Greig
Greig 2015년 2월 22일
Please note, that without the extra zeros in the data when you call polyfit, the results will be meaningless.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by