Curve Fit Transient Functions using Optimization

I am trying to curve fit test data using optimization. I have 2 functions which characterize the data that are dependent on each other and their previous time steps, similar to what is shown below.
ydata = csvread('testdata.csv')
y1(t) = y1(t-1) + a^2 + b -( (c + y2(t-1) ) / d )
y2(t) = (b*c/a) + ( y1(t-1) - y2(t-1) ) / d )
err(t) = (y1(t) - ydata(t))^2
total_err = sum(err)
I am trying to curve fit y1 to the data by minimizing the total_err term by adjusting a, b, c, and d. Would I use fmincon or lsqcurvefit for this? If so, I cannot figure out how to apply them to these equations. Any advice is appreciated.

回答(1 个)

Perhaps this example is relevant.
Alan Weiss
MATLAB mathematical toolbox documentation

2 个评论

Thank you for the response. So I can plug in:
x(1) = a
x(2) = b
x(3) = c
x(4) = d
y2 = @(x,xdata) (x(2)*x(3)/x(1)) + ( y1(xdata-1) - y2(xdata-1) ) / x(4) )
y1 = @(x,xdata) y1(xdata-1) + x(1)^2 + x(2) -( (x(3) + y2(xdata-1) ) / x(4) )
x0 = [1 1 1 0];
[x,resnorm,~,exitflag,output] = lsqcurvefit(y1,x0,t,y)
But the function still tries to reference itself evaluated at a previous time step. I know the initial value of the function at y1(0), so I calculate the rest by looping. However I am not sure how to capture that effect with an anonymous function.
You are free to write a full function, with as many conditions as you need to handle edge cases, something like
function z = myfun(x,xdata)
for I = 1:size(xdata,1)
if I = 1
% your code here
else
% more code here
end
end
Alan Weiss
MATLAB mathematical toolbox documentation

请先登录,再进行评论。

类别

提问:

2018-9-14

评论:

2018-9-17

Community Treasure Hunt

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

Start Hunting!

Translated by