fminsearch with 3 variables problem: one variable does not change during optimization.

2 views (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 11 Mar 2020
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 Comments
Hao Mu
Hao Mu on 11 Mar 2020
Thanks for the answer! I am sorry that I did not clarify, the global variables are initialized previously from raw data.
Walter Roberson
Walter Roberson on 11 Mar 2020
You thought you initialized them but you didn't.
Otherwise we need enough to be able to reproduce the problem... All initialization.

Sign in to comment.

Categories

Find more on Optimization 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!