I need to fit two x,y data sets to two different functions that share the same parameters. Is there a simple way to do this?

18 views (last 30 days)
I have one data set that reprents the population of one species as a function if x, and the other that represents the population another species as a function of x. These populations depend on the same paramters. When one d creases the other increases. So I have two functions, but the same parameters. I want to fit these two data sets simultaneously for the 5 parameters. I figu ed out how to fit two datasets to the same function with different parameters, but I need to fit two dTa sets to different functions with the same parameters and I cannot figure it out. Thanks for any insight. Cathy

Answers (1)

Star Strider
Star Strider on 17 Nov 2016
If you write your functions correctly, this will work. (I tested it with arbitrary data and functions. It works.)
The Code
x_mtx = [x1(:) x2(:)]; % Concatenate Vectors To Create ‘x_mtx’
y_mtx = [y1(:) y2(:)]; % Concatenate Vectors To Create ‘y_mtx’
fun1 = @(x,xdata) ...; % Objective Function #1
fun2 = @(x,xdata) ...; % Objective Function #2
fun = @(x,xdata) [fun1(x,xdata(:,1)), fun2(x,xdata(:,2))]; % Composite Function
x0 = rand(5,1); % Initial Parameter Estimates
X = lsqcurvefit(fun, x0, x_mtx, y_mtx); % Estimate Parameters
Obviously, your different ‘x’ and ‘y’ data vectors all have to have the same lengths, or you will not be able to create the ‘x_mtx’ and ‘y_mtx’ arrays, and so will not be able to estimate your parameters.
  4 Comments
Changjiang
Changjiang on 19 Nov 2023
Hi Star,
Why I got the error:
"Error using lsqcurvefit
Function value and YDATA sizes are not equal."
I just copied the code you wrote.
Best
CJ
Star Strider
Star Strider on 19 Nov 2023
I have no idea in your specific instance.
This can occur if the objective function returns matrices that are transposes of the data, so check that.

Sign in to comment.

Categories

Find more on Biological and Health Sciences in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!