How to bundle function input for optimization

4 vues (au cours des 30 derniers jours)
Hashim
Hashim le 4 Avr 2022
Réponse apportée : Nipun le 25 Jan 2024
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 commentaires
Star Strider
Star Strider le 4 Avr 2022
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 le 4 Avr 2022
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.

Connectez-vous pour commenter.

Réponses (1)

Nipun
Nipun le 25 Jan 2024
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

Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by