How to extend curve fit beyond data points?

조회 수: 19 (최근 30일)
Nicholas Pfaff
Nicholas Pfaff 2018년 7월 10일
댓글: Gamra Samha 2020년 7월 10일
I have plotted my data and fitted a curve onto it. However, I am not able to extend my fit beyond my data points (I want it to go through my points and through the axis).
My code:
function [fitresult, gof] = createFit(diameter, time)
%%Fit: 'Raw Data'.
[xData, yData] = prepareCurveData( diameter, time );
% Set up fittype and options.
ft = fittype( 'power1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [155.522818446907 -1.88432816467686];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data
figure( 'Name', 'Raw Data' );
h = plot( fitresult,'b', xData, yData, '.k' );
h(1).MarkerSize = 12;
h(2).LineWidth = 1;
legend( h, 'time vs. diameter', 'Raw Data', 'Location', 'NorthEast' );
% Label axes
xlabel diameter
ylabel time
grid on
hold on;
axis([0 22 0 41]);
Produced graph through my code:
How I want it to look:
%

채택된 답변

jonas
jonas 2018년 7월 10일
편집: jonas 2018년 7월 11일
You have the coefficients of the 'power1' function stored in fitresult. It probably looks something like this:
fitresult =
General model Power1:
f(x) = a*x^b
Coefficients (with 95% confidence bounds):
a = 1.46 (1.22, 1.69)
b = 0.40 (0.38, 0.43)
with 1.46 and 0.40 being the coefficients. Just create new bounds by
x=linspace(xmin,xmax,n)
and plug it into the equation
y=(fitresult.a).*x.^(fitresult.b)
plot(x,y)
  댓글 수: 4
Nicholas Pfaff
Nicholas Pfaff 2018년 7월 11일
Thank you!
Gamra Samha
Gamra Samha 2020년 7월 10일
Hi,
I am having some sort of a similar problem. I have x and y values and I need to fit them expoentially. Below is my code.
xData = [0.9,2,3,4.1,5.2,6.2,7.3,8.4,9.5,10.5,11.6,12]';
yData = [216.4684,153.2911,108.3671,82.3165,58.4557,47.2278,40.1013,36.9241,32.3291,31.3671,30.0000,27.9747]';
[xData, yData] = prepareCurveData (xData, yData);
ft = fittype( 'exp1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Levenberg-Marquardt';
opts.Display = 'Off';
opts.StartPoint = [200.802517208848 -0.198913568004103];
[f1,gof,output] = fit(xData,yData,ft,opts);
% Plot fit with data.
figure
h_1 = plot(f1, x, y );
legend( h_1, 'y vs. x', 'untitled fit 1', 'Location', 'NorthEast' );
% Label axes
xlabel x
ylabel y
grid on
I am not able though to make the curve meet the first point of the y values well. The curve as well does not meet the y axis as it should be.
I have also used your suggested code above, but for some reason the data points and curve do not meet, even when I change the values in linspace. Also the curve does not meet the y-axis.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by