How to extend curve fit beyond data points?

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:
%

 Accepted Answer

jonas
jonas on 10 Jul 2018
Edited: jonas on 11 Jul 2018
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 Comments

I still can't get it to work: I can't get both my data points and a fit line to display.
How would you suggest do implement your line into my code?
Here is an example code with some made-up data. I suspect that the issue was that I wrote fitresults instead of fitresult? I've fixed the answer.
xData=1:.5:20;
yData=(2.*Xdata.^-1.5)+0.02.*rand(size(Xdata));
%%Fit: 'Raw Data'.
[xData, yData] = prepareCurveData( xData, yData );
% 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' );
x=linspace(0,25,100);
y=(fitresult.a).*x.^(fitresult.b);
h=plot(xData,yData,'.k',...
x,y,'b');
h(1).MarkerSize = 12;
h(2).LineWidth = 1;
legend( [h(2) h(1)], 'time vs. diameter', 'Raw Data', 'Location', 'NorthEast' );
% Label axes
xlabel diameter
ylabel time
grid on
hold on;
axis([0 25 0 3])
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.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 10 Jul 2018

Commented:

on 10 Jul 2020

Community Treasure Hunt

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

Start Hunting!