Data fitting by non linear discrete equation.

2 views (last 30 days)
xdata=[5.0 4.2 4.3 4.3 4.3 4.1 3.7 3.6 3.9 5.9 6.7 5.4 5.1 5.5 6.2 6.7 6.9 6.2 6.1 6.3 5.9 5.5 5.5 5.5 5.5];
ydata=[2.0 2.5 1.9 2.1 2.0 2.1 2.7 4.0 3.8 3.9 3.6 3.2 3.2 3.1 3.2 3.7 3.7 3.2 3.2 3.5 3.5 3.2 3.1 3.3 3.1];
I want to fit the following equations to this data: I want to estimate all the parameter. (alpha and beta lie between 0 and 1, a lies between 0 and alpha, b lies between 0 and beta and c can be any value from 0 to infinity.) Thanks in advance.
  2 Comments
Ankur Pal
Ankur Pal on 24 Nov 2020
Edited: Ankur Pal on 24 Nov 2020
I have tried lsqcurvefit but could not understand how to frame these equations. Also how to limit the parameters.

Sign in to comment.

Accepted Answer

Rik
Rik on 24 Nov 2020
The code below uses fminsearch, which means you don't need the optimization toolbox. The downside is that it is sensitive to a local minimum, depending on your initial parameter estimate.
xdata=[5.0 4.2 4.3 4.3 4.3 4.1 3.7 3.6 3.9 5.9 6.7 5.4 5.1 5.5 6.2 6.7 6.9 6.2 6.1 6.3 5.9 5.5 5.5 5.5 5.5];
ydata=[2.0 2.5 1.9 2.1 2.0 2.1 2.7 4.0 3.8 3.9 3.6 3.2 3.2 3.1 3.2 3.7 3.7 3.2 3.2 3.5 3.5 3.2 3.1 3.3 3.1];
initial_guess=0.5*ones(1,5);
fitted_params=fminsearch(@(fit_vals) costfun(fit_vals,xdata,ydata),initial_guess);
[alpha,beta,a,b,c]=deal(fitted_params(1),fitted_params(2),fitted_params(3),fitted_params(4),fitted_params(5));
x0=xdata(1);y0=ydata(1);elems=numel(xdata);
[x_fitted,y_fitted]=f_g(fitted_params,x0,y0,elems);
disp([x_fitted;y_fitted])
Columns 1 through 17 5.0000 3.7777 2.9288 2.3394 1.9301 1.6459 1.4485 1.3114 1.2163 1.1502 1.1043 1.0724 1.0503 1.0349 1.0241 1.0161 1.0095 2.0000 0.9821 0.9821 0.9821 0.9821 0.9821 0.9821 0.9821 0.9821 0.9821 0.9821 0.9821 0.9821 0.9820 0.9816 0.9799 0.9767 Columns 18 through 25 1.0039 0.9990 0.9946 0.9906 0.9871 0.9835 0.9810 0.9765 0.9734 0.9701 0.9670 0.9639 0.9615 0.9581 0.9577 0.9489
function cost=costfun(fit_vals,xdata,ydata)
x0=xdata(1);y0=ydata(1);elems=numel(xdata);
[x,y]=f_g(fit_vals,x0,y0,elems);
cost= sum((x-xdata).^2) + sum((y-ydata).^2);
[alpha,beta,a,b,c]=deal(fit_vals(1),fit_vals(2),fit_vals(3),fit_vals(4),fit_vals(5));
if ( alpha<0 || alpha>1 ) || ...
( a<0 || a>alpha ) || ...
( beta<0 || beta>1 ) || ...
( b<0 || b>beta ) || ...
c<0
cost=inf;
end
end
function [x,y]=f_g(params,x0,y0,elems)
[alpha,beta,a,b,c]=deal(params(1),params(2),params(3),params(4),params(5));
x=zeros(1,elems);x(1)=x0;
y=zeros(1,elems);y(1)=y0;
for n=2:elems
x(n)=(1-alpha)*x(n-1) + a/(1+exp(-c*(x(n-1)-y(n-1))));
y(n)=(1-beta )*y(n-1) + b/(1+exp(-c*(x(n-1)-y(n-1))));
end
end

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!