doing polyvar giving two conditions

조회 수: 2 (최근 30일)
seema niran
seema niran 2016년 1월 16일
댓글: Star Strider 2016년 1월 16일
i have a matrix xx(100*1) of elements less than and greater than 70 and a y matrix.p1=polyfit(xx,y,1)when xx<70 and p2=polyfit(xx,y,1);when xx>70 . i need to polyvar(p1,xx)for xx<70 and polyvar(p2,xx)for xx>70 and get the two condition as a single matrix. i tried with if loop but getting answer only for the else condition.is it possible to get answer in both conditions?

채택된 답변

Star Strider
Star Strider 2016년 1월 16일
See if this does what you want:
xx = linspace(1, 140); % Create Data
y = randi(99, 1, 100); % Create Data
p1 = polyfit(xx(xx<70), y(xx<70), 1);
p2 = polyfit(xx(xx>=70), y(xx>=70), 1);
y1 = polyval(p1,xx(xx<70));
y2 = polyval(p2,xx(xx>=70));
figure(1)
plot(xx(xx<70), y1, xx(xx<70),y(xx<70))
hold on
plot(xx(xx>=70), y2, xx(xx>=70),y(xx>=70))
hold off
grid
  댓글 수: 2
seema niran
seema niran 2016년 1월 16일
yes upto y1 and y2 ,it is correct .but i need y1 and y2 as a single matrix y such that y contain element of y1 for xx<70 and y2 for xx>70
Star Strider
Star Strider 2016년 1월 16일
Call the matrix (actually vector) containing ‘y1’ and ‘y2’, ‘yfit’ (or something more original), and since both are row vectors, define it as:
yfit = [y1 NaN y2];
If you want to define ‘xxfit’ in the same way (albeit redundantly in my example):
xxfit = [xx(xx<70) NaN xx(xx>=70)];
You could then plot them as:
figure(2)
plot(xx, y, 'bp') % Plot Original Data
hold on
plot(xxfit, yfit, '-r') % Plot Fitted Regression Lines
hold off
grid
The NaN values are to break up the regression lines and to make both vectors have equal lenghs.
Providing that your independent variables are well-behaved (such as ‘xx’ is here) you can do your regressions as I have done them here. If they are discontinuous or have extremely high magnitudes, you would have to centre and scale them to get the best and most robust fit. That involves adding the ‘S’ and ‘mu’ outputs to your polyfit calls and using them as well in your polyval calls. This is inconvenient, but will result in reliable parameter estimates and a reliable fit to the data. See the documentation for the functions for a full explanation.
And if you really want to ‘gild the lily’ and report the confidence intervals for the parameters ‘p1’ and ‘p2’, use the absolutely brilliant File Exchange contribution polyparci. Try it! you’ll like it!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by