main content

fit ode parameters using optimization variables -凯发k8网页登录

this example shows how to find parameters that optimize an ordinary differential equation (ode) in the least-squares sense, using optimization variables (the problem-based approach).

problem

the problem is a multistep reaction model involving several substances, some of which react with each other to produce different substances.

for this problem, the true reaction rates are unknown. so, you need to observe the reactions and infer the rates. assume that you can measure the substances for a set of times t. from these observations, fit the best set of reaction rates to the measurements.

model

the model has six substances, c1 through c6, that react as follows:

  • one c1 and one c2 react to form one c3 at rate r1

  • one c3 and one c4 react to form one c5 at rate r2

  • one c3 and one c4 react to form one c6 at rate r3

the reaction rate is proportional to the product of the quantities of the required substances. so, if yi represents the quantity of substance ci, then the reaction rate to produce c3 is r1y1y2. similarly, the reaction rate to produce c5 is r2y3y4, and the reaction rate to produce c6 is r3y3y4.

in other words, the differential equation controlling the evolution of the system is

dydt=[-r1y1y2-r1y1y2-r2y3y4 r1y1y2-r3y3y4-r2y3y4-r3y3y4r2y3y4r3y3y4].

start the differential equation at time 0 at the point y(0)=[1,1,0,1,0,0]. these initial values ensure that all of the substances react completely, causing c1 through c4 to approach zero as time increases.

express model in matlab

the diffun function implements the differential equations in a form ready for solution by ode45.

type diffun
function dydt = diffun(~,y,r)
dydt = zeros(6,1);
s12 = y(1)*y(2);
s34 = y(3)*y(4);
dydt(1) = -r(1)*s12;
dydt(2) = -r(1)*s12;
dydt(3) = -r(2)*s34   r(1)*s12 - r(3)*s34;
dydt(4) = -r(2)*s34 - r(3)*s34;
dydt(5) = r(2)*s34;
dydt(6) = r(3)*s34;
end

the true reaction rates are r1=2.5, r2=1.2, and r3=0.45. compute the evolution of the system for times zero through five by calling ode45.

rtrue = [2.5 1.2 0.45];
y0 = [1 1 0 1 0 0];
tspan = linspace(0,5);
soltrue = ode45(@(t,y)diffun(t,y,rtrue),tspan,y0);
yvalstrue = deval(soltrue,tspan);
for i = 1:6
    subplot(3,2,i)
    plot(tspan,yvalstrue(i,:))
    title(['y(',num2str(i),')'])
end

figure contains 6 axes objects. axes object 1 with title y(1) contains an object of type line. axes object 2 with title y(2) contains an object of type line. axes object 3 with title y(3) contains an object of type line. axes object 4 with title y(4) contains an object of type line. axes object 5 with title y(5) contains an object of type line. axes object 6 with title y(6) contains an object of type line.

optimization problem

to prepare the problem for solution in the problem-based approach, create a three-element optimization variable r that has a lower bound of 0.1 and an upper bound of 10.

r = optimvar('r',3,"lowerbound",0.1,"upperbound",10);

the objective function for this problem is the sum of squares of the differences between the ode solution with parameters r and the solution with the true parameters yvals. to express this objective function, first write a matlab function that computes the ode solution using parameters r. this function is the rtoode function.

type rtoode
function solpts = rtoode(r,tspan,y0)
sol = ode45(@(t,y)diffun(t,y,r),tspan,y0);
solpts = deval(sol,tspan);
end

to use rtoode in an objective function, convert the function to an optimization expression by using fcn2optimexpr. see .

myfcn = fcn2optimexpr(@rtoode,r,tspan,y0);

express the objective function as the sum of squared differences between the ode solution and the solution with true parameters.

obj = sum(sum((myfcn - yvalstrue).^2));

create an optimization problem with the objective function obj.

prob = optimproblem("objective",obj);

solve problem

to find the best-fitting parameters r, give an initial guess r0 for the solver and call solve.

r0.r = [1 1 1];
[rsol,sumsq] = solve(prob,r0)
solving problem using lsqnonlin.
local minimum found.
optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
rsol = struct with fields:
    r: [3x1 double]
sumsq = 3.8659e-15

the sum of squared differences is essentially zero, meaning the solver found parameters that cause the ode solution to match the solution with true parameters. so, as expected, the solution contains the true parameters.

disp(rsol.r)
    2.5000
    1.2000
    0.4500
disp(rtrue)
    2.5000    1.2000    0.4500

limited observations

suppose that you cannot observe all the components of y, but only the final outputs y(5) and y(6). can you obtain the values of all the reaction rates based on this limited information?

to find out, modify the function rtoode to return only the fifth and sixth ode outputs. the modified ode solver is in rtoode2.

type rtoode2
function solpts = rtoode2(r,tspan,y0)
solpts = rtoode(r,tspan,y0);
solpts = solpts([5,6],:); % just y(5) and y(6)
end

the rtoode2 function simply calls rtoode and then takes the final two rows of the output.

create a new optimization expression from rtoode2 and the optimization variable r, the time span data tspan, and the initial point y0.

myfcn2 = fcn2optimexpr(@rtoode2,r,tspan,y0);

modify the comparison data to include outputs 5 and 6 only.

yvals2 = yvalstrue([5,6],:);

create a new objective and new optimization problem from the optimization expression myfcn2 and the comparison data yvals2.

obj2 = sum(sum((myfcn2 - yvals2).^2));
prob2 = optimproblem("objective",obj2);

solve the problem based on this limited set of observations.

[rsol2,sumsq2] = solve(prob2,r0)
solving problem using lsqnonlin.
local minimum possible.
lsqnonlin stopped because the final change in the sum of squares relative to 
its initial value is less than the value of the function tolerance.
rsol2 = struct with fields:
    r: [3x1 double]
sumsq2 = 2.1616e-05

once again, the returned sum of squares is essentially zero. does this mean that the solver found the correct reaction rates?

disp(rsol2.r)
    1.7811
    1.5730
    0.5899
disp(rtrue)
    2.5000    1.2000    0.4500

no; in this case, the new rates are quite different from the true rates. however, a plot of the new ode solution compared to the true values shows that y(5) and y(6) match the true values.

figure
plot(tspan,yvals2(1,:),'b-')
hold on
ss2 = rtoode2(rsol2.r,tspan,y0);
plot(tspan,ss2(1,:),'r--')
plot(tspan,yvals2(2,:),'c-')
plot(tspan,ss2(2,:),'m--')
legend('true y(5)','new y(5)','true y(6)','new y(6)','location','northwest')
hold off

figure contains an axes object. the axes object contains 4 objects of type line. these objects represent true y(5), new y(5), true y(6), new y(6).

to identify the correct reaction rates for this problem, you must have data for more observations than y(5) and y(6).

plot all the components of the solution with the new parameters, and plot the solution with the true parameters.

figure
yvals2 = rtoode(rsol2.r,tspan,y0);
for i = 1:6
    subplot(3,2,i)
    plot(tspan,yvalstrue(i,:),'b-',tspan,yvals2(i,:),'r--')
    legend('true','new','location','best')
    title(['y(',num2str(i),')'])
end

figure contains 6 axes objects. axes object 1 with title y(1) contains 2 objects of type line. these objects represent true, new. axes object 2 with title y(2) contains 2 objects of type line. these objects represent true, new. axes object 3 with title y(3) contains 2 objects of type line. these objects represent true, new. axes object 4 with title y(4) contains 2 objects of type line. these objects represent true, new. axes object 5 with title y(5) contains 2 objects of type line. these objects represent true, new. axes object 6 with title y(6) contains 2 objects of type line. these objects represent true, new.

with the new parameters, substances c1 and c2 drain more slowly, and substance c3 does not accumulate as much. but substances c4, c5, and c6 have exactly the same evolution with both the new parameters and the true parameters.

see also

| |

related topics

网站地图