Attempting to get a line of best fit for a custom function

2 views (last 30 days)
I am trying to get a line of best fit for a custom function. Here is my code and the plot that it produces. The problem is that the line seems to be touching every point, which is not what I want. Any suggestions? Also I understand that I can use the curve fitting app, but I prefer doing this by writing up a code
clc;
x(1,:) = Y1;
x(2,:) = Dmge;
plot(x(1,:),x(2,:),'.');
A= 5500; B=0.19; C=2.3;
goemp_fit1 = @(y)sum((y(1)*exp(-exp(-y(2)*(x(1,:)-y(3))))-x(2,:)).^2);
coeff = fminsearch(goemp_fit1,[A B C]);
A = coeff(1);
B = coeff(2);
C = coeff(3);
x2 = x(1,:);
y2 = A*exp(-exp(-B*(x-C)));
plot(x2,y2,'-m')

Accepted Answer

Walter Roberson
Walter Roberson on 9 Sep 2018
x2 = x(1,:);
that is one row and multiple columns
y2 = A*exp(-exp(-B*(x-C)));
that involves all of x, so it involves two rows and multiple columns. Perhaps you should be using
y2 = A*exp(-exp(-B*(x2-C)));

More Answers (0)

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!