Data fitting by non linear discrete equation.
2 views (last 30 days)
Show older comments
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
Accepted Answer
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])
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)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!