Nonlinear data fitting using lsqnonlin for multiple variables

Hello I am trying to fit multiple variables with multiple parameters and objective functions to experimental data using lsqnonlin function and I am having difficulty to generalize the code for this.
For example consider x1, x2, x3 and x4 as the 4 variables that I want to fit. The input data/parameters is a matrix 10x2 where each column is a independent variable a and b having 10 data points corresponding to the output data Y. further I have 2 objective functions f1 and f2.
The first 5 datapoints correspond to the output with respect to variables x1 x2 and their objective function f1. The next 5 data points correspond to the output with respect to variables x3 x4 and their objective function f2 as below
f1 = (x1 + x2*a(1:5) + b*x2(1:5)^3) - y(1:5)
f2 = (x3 + x4*a(5:10) + x4*b(5:10)^2) - y(5:10)
The number of variables, parameters and functions in this expample is just for explanation and in practice may be more.
Kindly suggest how I can generalize to code to accept multiple variables, parameters and functions.Also the function above is hypothetical, so you may use a different function in your answers
Thank you

1 件のコメント

Matt J
Matt J 2019 年 6 月 19 日
編集済み: Matt J 2019 年 6 月 19 日
In your example, f1 and f2 don't share any unknown parameters. Therefore, there is no need to fit them jointly. You could run lsqnonlin once to fit f1 and a second time to fit f2.

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2019 年 6 月 19 日
編集済み: Matt J 2019 年 6 月 19 日

1 投票

There's nothing special involved,
a=___
b=___
y=___
x0=___
xlsq =lsqnonlin(@(x) modelfun(x,a,b,y), x0)
function F=modelfun(x,a,b,y)
f1 = (x(1) + x(2)*a(1:5) + x(2)*b(1:5)^3) - y(1:5);
f2 = (x(3) + x(4)*a(5:10) + x(4)*b(5:10)^2) - y(5:10);
F=[f1,f2];
end

6 件のコメント

Zaheer Shariff
Zaheer Shariff 2019 年 6 月 20 日
Hello Matt,
Thank you for the answer. Your reply does solve part of the problem I had. Also you are right during fitting there are no shared parameters but I use these fitted paramaters in another routine which is called at each iteration of the fitting function to generate revised input data until the final solution is reached.
For the next step I would like to ask you how I can I generate multiple objective functions with out hard coding them as in the above case. for example something like.(I know that the example below is not exactly correct but it may give you an idea what I am truing to do)
for j= 1:3
for i = 1:n (where n = 9)
f(j) = (x(i) + x(i+1)*a(1:5) + x(i+2)*b(1:5)^3) - y(1:5);
Stephen23
Stephen23 2019 年 6 月 20 日
".. I use these fitted paramaters in another routine which is called at each iteration of the fitting function..:"
It sounds like you need to use nested functions.
Zaheer Shariff
Zaheer Shariff 2019 年 6 月 20 日
Dear Stephen,
Yes I already use nested functions. My problem is that I have different no. of variables to be fitted for different cases. And I want to formulate a generalized objective function which will make use of the input data and generate the array of objective functions to be fitted. An example of the generalized form of objective function is shown above.
Matt J
Matt J 2019 年 6 月 20 日
編集済み: Matt J 2019 年 6 月 20 日
You can use cells if it helps you,
f{j} = (x(i) + x(i+1)*a(1:5) + x(i+2)*b(1:5)^3) - y(1:5);
but the output of the model function must be a numeric array, so you must concatenate the f{j} together at the end as if there were only a single model function,
F=horzcat(f{:})
Zaheer Shariff
Zaheer Shariff 2019 年 6 月 20 日
thank you. I will try it out.
Matt J
Matt J 2019 年 6 月 20 日
You're welcome, but please Accept-click the answer if it addressed your question.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLinear and Nonlinear Regression についてさらに検索

質問済み:

2019 年 6 月 19 日

コメント済み:

2019 年 6 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by