optimising a parameter not directly involved in the objective function to be minimised

someone kindly help me write a code to optimise h using fminsearch such that SSE is minimised. h is is not directly invloved in solving for SSE but is involved in the first differential equation whose output, 'L' is used in the subquest non-differential equations.
I am trying to replicate a publication in which it's mentioned that, 'equation 1 is a 1st order ODE solved by 4th order Runge-kutta solver which facilitates simultaneous solution of equation 2-4 at each time step'. I wonder how to simulataneously solve the differential equation and non-differential equation using ODE45. At the moment am only solving for equation 1 using the ODE45 and then solve for the non-differential equations.
Thank you.
%constants; KA, KV, p, D, Cs, mo, Ni and V
% Cb_exp is a column vector
dLdt = -(KA / (3 * p * KV)) * (D / h) .* (Cs - Cb_exp); %equation 1
mi_j = p * KV *((L).^3)*Ni; % Equation 2
Cb_cal = (mo - mi_j) / V; % Equation 3
S = KA * (L).^2 * Ni / V; % Equation 4
SSE= sum(Cb_cal-Cb_exp)^2

2 Comments

As far as I understand your equations, mi_j, Cb_cal and S can be directly computed once you know L. So what's the problem ? You don't need to "solve" for them with the integrator you use.
That's right @Torsten, was just wondering if there was a way since the publication mentioned, they simulataneously solved it using ODE45. Must be my misinterpretation. How about the optimisation of h? do you have any suggestions for that?

Sign in to comment.

Answers (1)

t_exp = ...;
Cb_exp = ...; % experimental data
KA =...;
KV =...;
p =...;
D =...;
Cs =...;
mo =...;
Ni =...;
V =...; % constants
h0 = ...; % Guess value for h
h = lsqnonlin(@(h)fun(h,t_exp,Cb_exp,KA, KV, p, D, Cs, mo, Ni,V),h0)
function res = fun(h,t_exp,Cb_exp,KA, KV, p, D, Cs, mo, Ni,V)
tspan = t_exp; % Assumes t_exp starts with t = 0
L0 = ...; % Initial value for L at t=0
fun_ode = @(t,L) -(KA / (3 * p * KV)) * (D / h) * (Cs - interp1(t_exp,Cb_exp,t));
[~,L] = ode45(@fun_ode,tspan,L0);
mi_j = p * KV * L.^3 * Ni;
Cb_cal = (mo - mi_j) / V;
res = Cb_cal - Cb_exp;
end

2 Comments

Hi @Torsten, I get the following error from the code you shared.
t_exp = [0 30 60 90 120 180 240 300 360 420 480 540 600]';
Yexpt= [0.0000 0.1321 0.2447 0.3343 0.4122 0.5298 0.6246 0.6869 0.7348 0.7729 0.7931 0.8114 0.8272]';
V=100;
V=V*1e12;
Cb_exp= (Yexpt.*0.00425)./V;
Cs=4.25e-15;
mo=1;
v=200.3;
mw=206.28;
p=mo/((mo/mw)*v);
p = p * 1e-12;
Lc=50;
D=8.88*10^-6;
D=D* 1e8;
KA=pi;
KV=pi/6;
Ni=18806.1232307428;
h0 = Lc/2;
h = lsqnonlin(@(h)fun(h,t_exp,Cb_exp,KA, KV, p, D, Cs, mo, Ni,V),h0)
Unrecognized function or variable 'fun_ode'.

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);

Error in solution>fun (line 29)
[~,L] = ode45(@fun_ode,tspan,L0);

Error in solution>@(h)fun(h,t_exp,Cb_exp,KA,KV,p,D,Cs,mo,Ni,V) (line 22)
h = lsqnonlin(@(h)fun(h,t_exp,Cb_exp,KA, KV, p, D, Cs, mo, Ni,V),h0)

Error in lsqnonlin (line 242)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
function res = fun(h,t_exp,Cb_exp,KA, KV, p, D, Cs, mo, Ni,V)
tspan = t_exp;
L0 = 462; % Initial value for L at t=0
fun_ode = @(t,L) -(KA / (3 * p * KV)) * (D / h) * (Cs - interp1(t_exp,Cb_exp,t));
[~,L] = ode45(@fun_ode,tspan,L0);
mi_j = p * KV * L.^3 * Ni;
Cb_cal = (mo - mi_j) / V;
res = Cb_cal - Cb_exp;
end
[~,L] = ode45(fun_ode,tspan,L0);
res = 1e16*(Cb_cal - Cb_exp);
instead of
[~,L] = ode45(@fun_ode,tspan,L0);
res = Cb_cal - Cb_exp;

Sign in to comment.

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Products

Release

R2020b

Asked:

Pc
on 29 Feb 2024

Edited:

on 29 Feb 2024

Community Treasure Hunt

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

Start Hunting!