Least Squares fit of model function

Hi guys!
I need help with a least square method fit for the model function a*cosh(b*x)+c but im not sure how to do it without the curve fitting tool (see solution of code below). I am not sure have to split the a and b or the cosh(b*x) to create a matrix and use the A\y backslash command in matlab to get the variables of a b and c. I tried putting b as 1 to solve it but i dont think i get the right answer (se code below the %curve fitting tool)
I was given 15 different points (x,y).
% Curve fitting tool <-- This should be right i guess
% g = fittype('a*cosh(b*x) + c','coeff',{'a','b','c'});
% options = fitoptions(g);
% fitobject = fit(x',y',g,options);
% fit3 = (fitobject.a)*cosh((fitobject.b)*x) + fitobject.c;
% Without the curve fitting tool <-- Probably wrong
x=[ . . . . . . . . . . . . . . .]
y=[ . . . . . . . . . . .. . . .]
x=cosh(1*x)';
A=[cosh(x) ones(size(x))]
ab=A\y
n2=[]
for i=1:15
n=ab(1)*x(i)+ab(2);
n2(i)=n;
end
plot(x,n2,'ro')
Do you have any clues on how to solve this one without the curve fitting tool?
I would be really greatful if you can help me with this one!

 Accepted Answer

Everyong had the fminsearch function, so use it to do nonlinear curve fitting:
f = @(b,x) b(1).*cosh(b(2).*x) + b(3);
x=[ . . . . . . . . . . . . . . .]
y=[ . . . . . . . . . . .. . . .]
[B,fval] = fminsearch(@(b) norm(y - f(b,x)), [0.5; 0.5; 0.5])
figure
plot(x, y, 'pg', 'MarkerFaceColor','g')
hold on
plot(x, f(B,x), '-r')
hold off
grid
legend('Data', 'Regression')
Nonlinear parameter estimation can be very sensitive to the initial parameter estimates (here [0.5; 0.5; 0.5]), so you may have to experiment with them to get a good fit to your data.

2 Comments

Many many thanks!
Now I understand how to do it :)
As always, my pleasure!
That’s the reason we’re here!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!