How to bundle function input for optimization

조회 수: 4 (최근 30일)
Hashim
Hashim 2022년 4월 4일
답변: Nipun 2024년 1월 25일
Hi!
I am trying to optimize a pdepe function of the form;
function I_num_ano1 = PSw_EK_v11(k_m, D_m, s_bulk)
Here I want to optimize the k_m and D_m input via the lsqcurvefit function. I have made a separate file to accomodate the optimization function. But for that I need a single variable containing both k_m and D_m (because in the documentation the example give is of a objective function with variables x(1) and x(2)). How can I do that?
Script "PSw_EK_v11_Caller" is given below:
% The purpose of this script is to call PSw_EK_v11 and give it mutiple
% substrate concentrations and calculate the respective current values.
% Using this we can plot a [S] vs I plot and then try to fit Kavita's curve
% onto it and calculate parameters using that.
I_exp = 2.73e-03;
s_bulk = 5.00e-06;
% x = [k_m, D_m];
options = optimoptions('lsqcurvefit','Algorithm','trust-region-reflective', ...
'OptimalityTolerance',1e-13,'FunctionTolerance',1e-13);
lb = [1e03, 1e-09];
ub = [1e08, 1e-04];
k_0 = [1e03, 10e-08]; %starting point
% % lsqnonlin(@(k_m)PSw_EK_v7(k_m, s_bulk)-I_exp, k_m_0, lb, ub);
[x, resnorm,residual,exitflag,output,I_num_ano1]=lsqcurvefit(@PSw_EK_v11, k_0, s_bulk, I_exp, lb, ub, options)
  댓글 수: 2
Star Strider
Star Strider 2022년 4월 4일
Run this search for several examples of how to fit differential equations to data.
These are for fitting systems of ordinary differential equations, however the techniques should be adaptable to other systems and solvers.
Hashim
Hashim 2022년 4월 4일
So I guess I have no choice but to modify my objective function and replace all instances of k_m and D_m with a vectorized arbirtary number e.g. x(1)/x(2) etc. Is there no way I can do that in my Caller file i.e. assign k_m and D_m to x?
Perhaps something like:
x = [k_m, D_m];
Which is not working ofc.

댓글을 달려면 로그인하십시오.

답변 (1개)

Nipun
Nipun 2024년 1월 25일
Hi Hashim,
I understand that you are trying to to bundle the function parameters (in this case arrays) to pass it to the objective function,
There are many ways to bundle arrays as a single variable. Here are two of the ways you can leverage array bundling:
  1. Structures: You may create a variable "x" such that x.'1' = k_m and x.'2' = D_m
  2. Cell arrays: You can define a "x" as a cell array, with each element as the required variable.
x = {k_m, D_m};
However, since you need to fit the xdata and ydata based on the mentioned function, I believe variable "x" will be returned by the curve fitting function. Therefore, instead of assigning "k_m" and "D_m" to "x", you may want to return the output as:
k_m = x(1); D_m = x(2);
Hope this helps.
Regards,
Nipun

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by