MATLAB: Fitting equation to data points

fitting equation to datapoints

Hello,
I have the data points
x = [22.96, 88.2, 114.275, 85.3] y = [0.1422, 0.157967, 0.202167, 0.1612]
and I want to fit the equations
y =(L/Vr)-((L/4.316)*cosd(x))
to determine the value of L and Vr.
L should be in the range from 0.0 – 1.7
Vr should be in the range from 2.0 – 6.0
Can anyone help?
Thank you

Best Answer

Try this:
fy = @(L,Vr,x) (L./Vr)-((L./4.316).*cosd(x));
x = [22.96, 88.2, 114.275, 85.3];
y = [0.1422, 0.157967, 0.202167, 0.1612];
B = fmincon(@(b) norm(y - fy(b(1),b(2),x)), [1; 4], [],[],[],[],[0 2],[1.7 6.0])
fprintf('L = %.15f\nVr = %.15f\n', B)
xv = linspace(min(x), max(x));
figure(1)
plot(x, y, 'pg')
hold on
plot(xv, fy(B(1),B(2),xv), '-r')
hold off
grid
L = 0.347528298961989
Vr = 2.000000558931784