fminsearch with 3 variables problem: one variable does not change during optimization.
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I am fitting a 3-element Windkessel model to some blood flow data (analog circuit shown below). The flow Q and pressure P are given, and I need to find Windkessel parameters R1, R2, and C.
My code minimizes the error between the fitted data and original data P using fminsearch. One of my variables, R1, does not change at all while two other variables are far away from the given answer. Does anyone know what is wrong with the code? Thank you very much!
% Global variables pressure, time, Q, p0, dqdt were initialized from raw data
% R1, R2, and C were estimated and not shown here.
theta = [R1 R2 C]; % R1 never changed before and after fitting.
[y,~] = fminsearch(@opt3_4W,theta);
function R = opt3_4W(theta)
global pressure time Q p0 dqdt
% Q is flow; p0 is initial pressure; dqdt is the first derivative of flow over time.
[t,p] = ode45(@(t,p) ode_3E_4W(t,p,time,Q,theta, dqdt) ,time,p0);
R = sum(abs((pressure-p).^2));
end
function dpdt = ode_3E_4W(t,p,time,flow,theta,dqdt)
q = interp1(time,flow,t);
dq = interp1(time,dqdt,t);
R1 = theta(1);
R2 = theta(2);
C = theta(3);
dpdt = q/C + (q*R1)/(R2*C) + R1*dq - p/(C*R2);
end
0 个评论
回答(1 个)
Walter Roberson
2020-3-11
Your global variables are not initialized so they are []. Your function is going to output empty or possibly 0.
Moral of the story: don't use global variables.
2 个评论
Walter Roberson
2020-3-11
You thought you initialized them but you didn't.
Otherwise we need enough to be able to reproduce the problem... All initialization.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!