How to find unknown parameters in a nonlinear least squares function?

I have the rest of the code already complete, however, I believe I have the c1 and c2 unknown paratmers wrong and I am not sure how to solve for them. How would I enter the correct values of c1 and c2 considering the equation given above?
clc
close
load Downloads/dataSet1.mat t f;
subplot(4,1,1);
leastSquares(t,f,1);
load Downloads/dataSet2.mat t f;
subplot(4,1,2);
leastSquares(t,f,2);
load Downloads/dataSet3.mat t f;
subplot(4,1,3);
leastSquares(t,f,3);
load Downloads/dataSet4.mat t f;
subplot(4,1,4);
leastSquares(t,f,4);
function leastSquares(t, f, dataNum)
plot(t, f, '*r', 'MarkerSize', 3)
xlabel('x')
ylabel('y')
title(['Nonlinear Fit of Experimental Data Set #' num2str(dataNum)])
grid minor
hold on
t = t(:);
f = f(:);
A = [(t.^0.8)./(cos(3*t).^2),t.^0.8];
B = 1./f;
X = inv(A'*A)*A'*B;
c1 = X(1);
c2 = X(2);
tF = min(t):0.001:max(t);
fF = (tF.^0.8)./(c1*cos(3*tF).^2 + (1/c2));
plot(tF, fF, '-k')
legend('Experimental Data Points','Least Squares Fitted Curve', 'Location', 'best')
hold on
fH = (t.^0.8)./(c1*cos(3*t).^2 + 1/c2);
fB = mean(f);
R2=abs(1-sum((f-fH).^2)/sum((f-fB).^2));
fprintf('The results for Data Set #%d\n',dataNum)
fprintf('c1 = %.4f\n', c1)
fprintf('c2 = %.4f\n', c2)
fprintf('R^2 = %.4f\n\n', R2)
end

 Accepted Answer

A = [cos(3*t).^2,ones(numel(t),1),];
B = t.^0.8./f;
X = A\B;
%or X = inv(A'*A)*A'*B;
c1 = X(1);
c2 = 1/X(2);
instead of
A = [(t.^0.8)./(cos(3*t).^2),t.^0.8];
B = 1./f;
X = inv(A'*A)*A'*B;
c1 = X(1);
c2 = X(2);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!