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.

Risposte (1)

Alan Weiss
Alan Weiss il 14 Set 2018
Perhaps this example is relevant.
Alan Weiss
MATLAB mathematical toolbox documentation

2 Commenti

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

Accedi per commentare.

Richiesto:

il 14 Set 2018

Commentato:

il 17 Set 2018

Community Treasure Hunt

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

Start Hunting!

Translated by