필터 지우기
필터 지우기

How to draw first order best fit in case of hold on figure?

조회 수: 1 (최근 30일)
Safi ullah
Safi ullah 2018년 1월 9일
댓글: Star Strider 2018년 1월 10일
Hi everybody, I have used the following code, and get the fig as shown below.
x = [9.384,39.27];
Y1 = 6.49e-12 * x.^(-4);
Y2 = 4.36e-10 * x.^(-5);
Y3 = 1.69e-10 * x.^(-4.3);
loglog(x,Y1,'*')
hold on
loglog(x,Y2,'*')
hold on
loglog(x,Y3,'*')
set(gca,'xtick',[9.384 39.27]);
set(gca,'xticklabel',{'A','B'});
Now I need to draw the best fit between A and B (best fit line draw from points at A towards points at B). I know the best fit in case of simple x and y plot, but due to hold on I do not know how to do this. Any guidance will be appreciated thanks.
  댓글 수: 2
Image Analyst
Image Analyst 2018년 1월 9일
What does hold on have to do with anything? Are you wanting to know how to use polyfit() to fit a line through the points, like this?
xt = [x(1), x(1), x(1), x(2), x(2), x(2)]
yt = [Y1(1), Y2(1), Y3(1), Y1(2), Y2(2), Y3(2)]
coefficents = polyfit(xt, yt, 1)
Safi ullah
Safi ullah 2018년 1월 9일
@ Image Analyst thanks for response. Actually I want to to draw the best fit between the A and B points.

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

채택된 답변

Star Strider
Star Strider 2018년 1월 10일
Try this:
x = [9.384,39.27];
Y1 = 6.49e-12 * x.^(-4);
Y2 = 4.36e-10 * x.^(-5);
Y3 = 1.69e-10 * x.^(-4.3);
xv = reshape([x; x; x], [], 1); % Create X-Vector
YM = reshape([Y1; Y2; Y3], [], 1); % Create Y-Vector
DM = [xv(:) ones(size(xv(:)))]; % Design Matrix
B = DM \ YM; % Estimate Parameters
YFit = DM * B; % Fit Regression For Plot
figure(1)
loglog(x,Y1,'*')
hold on
loglog(x,Y2,'*')
loglog(x,Y3,'*')
loglog(xv, YFit, '-r') % Plot Regression
hold off
set(gca,'xtick',[9.384 39.27]);
set(gca,'xticklabel',{'A','B'});
  댓글 수: 3
Safi ullah
Safi ullah 2018년 1월 10일
@ Image Analyst, actually first I do not understand what you have mentioned in comment. As the answer of Star Strider exactly give me my problem solution and after your second comment I also understand your code. @ Image Analyst and Star Strider thanks both of you.

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

추가 답변 (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