can someone explain this code?
조회 수: 2 (최근 30일)
이전 댓글 표시
https://www.mathworks.com/matlabcentral/answers/5054-solving-an-ode-with-best-fit-adjustment-to-empirical-observations Teja's email bounced back. Can someone else help? Thank you
댓글 수: 0
채택된 답변
Jason Whitfield
2018년 7월 26일
The basic idea of the code is that you optimize the parameters of the ODE so that it matches your experimental data as closely as possible. To do this, the optimizer needs some sort of cost function to evaluate how close the parameters are to their true values. So, the cost function should take in test parameters and return a value correlating to their distance from their true values.
Here is the cost function in the given code.
function COST = COST(x,T,ytrue)
y0 = x(1);
A = x(2);
B = x(3);
% The cost function calls the ODE solver.
[tout,yout] = ode45(@dydt,T,y0,[],A,B);
COST = sum((yout - ytrue).^2);
In this case, the cost function first evaluates the ODE with the given test parameters. Then, it finds the sum of squared differences between the output of the ODE and the experimental data. As these data sets become more similar, the differences will become smaller, and the overall cost will be lower.
Now that we have a cost function, we can simply call the optimizer with the function and some arbitrary initial conditions.
x0 = [0.4 3.9 1.2]; % Just some Initial Condition
ub = [5 5 5]; % Upper bounds
lb = [0 0 0]; % Lower bounds
F = @(x) COST(x,T,ytrue);
xout = fmincon(F,x0,[],[],[],[],lb,ub); %<-- FMINCON is the optimizer
The optimizer will use the cost function to find the parameters that create an ODE closest to your experimental data.
The rest of the code in the example is for defining the ODE and experimental data, or for plotting the results. Let me know if you have more questions.
댓글 수: 3
Jason Whitfield
2018년 7월 27일
The notation "F = @(x) COST(x,T,ytrue);" creates a new function F that takes an input vector x and calls the cost function with that vector and the constants T and ytrue as arguments. So, x is a vector of parameters passed to the cost function by the optimizer. We give the optimizer an initial condition, "x0 = [0.4 3.9 1.2]" that it will use as x for the first call to the cost function. After that, it will automatically adjust the values in x to get the optimal solution. This initial condition is totally arbitrary, but an initial condition closer to the solution will probably converge faster.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!