MATLAB Answers

Sangmin
0

second order data fitting using least squares

Asked by Sangmin
on 17 Jan 2019
Latest activity Edited by Torsten
on 18 Jan 2019
hi
I am trying to fitting the data
I want to fitting the data 1 by least square fitting it to a quadratic function around the position of maximum data2(*)
f(x) = a(x-x0)^2 + b(x-x0) + c
where C is an additive constant C = f(x0) = 1.
I used several method (ex data fitting tool...) but failed
If you konw how to solve, pleast let me know
untitled.jpg

  2 Comments

x0 is a fitting parameter or set to a fixed value ?
x0 is one of the data with the largest value.
x = [0.81 0.85 0.91 1.00 1.17 1.33 1.36 1.37 1.39 1.40 1.42]
y = [0.58 0.69 0.81 0.93 1 0.91 0.84 0.80 0.74 0.67 0.59]
x0 is maximum point (1.17 1)
I want to fit the quadratic curve through x0

Sign in to comment.

2 Answers

Answer by Torsten
on 18 Jan 2019
Edited by Torsten
on 18 Jan 2019

x = [0.81 0.85 0.91 1.00 1.17 1.33 1.36 1.37 1.39 1.40 1.42];
y = [0.58 0.69 0.81 0.93 1 0.91 0.84 0.80 0.74 0.67 0.59];
x0 = x(5);
y0 = y(5);
xtrans = x - x0;
ytrans = y - y0;
xtrans = xtrans.';
ytrans = ytrans.';
%mat = [sum(xtrans.^4) sum(xtrans.^3);sum(xtrans.^3) sum(xtrans.^2)];
%rhs = [sum(xtrans.^2.*ytrans); sum(xtrans.*ytrans)];
mat = [xtrans.^2 xtrans];
rhs = ytrans;
sol = mat\rhs;
a = sol(1);
b = sol(2);
fun = @(x)a*(x-x0).^2+b*(x-x0)+y0;
yfit = fun(x);
plot(x,y,x,yfit)

  0 Comments

Sign in to comment.


Answer by Akira Agata
on 18 Jan 2019

Another possible solution:
x = [0.81 0.85 0.91 1.00 1.17 1.33 1.36 1.37 1.39 1.40 1.42]';
y = [0.58 0.69 0.81 0.93 1 0.91 0.84 0.80 0.74 0.67 0.59]';
x0 = x(5);
y0 = y(5);
modelfun = @(a,x) a(1)*(x - x0).^2 + a(2)*(x - x0) + y0;
beta0 = [-1 1]; % Initial guess
mdl = fitnlm(x,y,modelfun,beta0);
xq = linspace(min(x),max(x))';
figure
scatter(x,y)
hold on
plot(xq,predict(mdl,xq))
fitting.png

  0 Comments

Sign in to comment.