How to extend curve fit beyond data points?
61 次查看(过去 30 天)
显示 更早的评论
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:
%
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!