Asked by Arun Kumar Bar
on 16 Sep 2018

I have two sets of dependent variable column vectors (y1, y2) for a set of independent variable column vectors (x). I want to fit the (x, y1) plot with the function f1(ca. f1 = (a + (b-a)*(1 + c*x^d))) and the (x, y2) plot with the function f2(ca. f2 = (b-a)*c*x^d)) simultaneously. Could anyone please help?

Many thanks, in advance! Arun

Answer by Torsten
on 17 Sep 2018

Accepted Answer

Use "lsqcurvefit" with the data set xdata = (x,x),ydata = (y1,y2).

Best wishes

Torsten.

Arun Kumar Bar
on 25 Sep 2018

Hi Torsten, Thanks a lot for your help! Could you please help me out from the associated following problem?

As I mentioned earlier, for the set of independent vectors x1, there are two sets of vectors, y1 and y2. So essentially, I should be able to fit them separately, ca. (x1 vs y1) and (x1 vs y2) plots. When I use APP in MATLAB, it works fine with the following custom equation:

y=f(x1)=m1+((m2-m1)*(1+(((2*pi*x1*m3)^(1-m4))*sin(m4*pi/2)))/(1+(2*((2*pi*x1*m3)^(1-m4)*sin(m4*pi/2)))+((2*pi*x1*m3)^((2-(2*m4))))))

But, it does not work in CommandWindow when I use the same equation in the function:

fun=@(m,x1) (m(1)+((m(2)-m(1)).*(1+(((2.*pi.*x1.*m(3)).^(1-m(4))).*sin(m(4).*pi/2)))/(1+(2.*((2.*pi.*x1.*m(3)).^(1-m(4)).*sin(m(4).*pi/2)))+((2.*pi.*x1.*m(3)).^((2-(2.*m(4))))))));

The error is a bit surprising to me! Both the x1 and y1 are 24x1 matrices (column vectors). While, the fun is returning a 24x24 matrix, with all identical elements.

Suppose, if I use, m0=[1.1; 17; 1.85; 0.54]; % these are the parameters obtained from the best fit using APP with the above custom equation; fun(m0,x1) returns a 24x24 matrix with all identical elements. Could you please suggest where I am going wrong ?

Kind regards, Arun

Torsten
on 25 Sep 2018

... .*pi/2)))./(1+(2.* ...

instead of

... .*pi/2)))/(1+(2.* ...

Best wishes

Torsten.

Arun Kumar Bar
on 25 Sep 2018

That's great! Thanks a lot, Torsten!

Kind regards, Arun

Sign in to comment.

Answer by Torsten
on 17 Sep 2018

xdata=[x(:,1); x(:,1)];

ydata=[y1(:,1); y2(:,1)];

fun=@(x,xdata) [f1(xdata(1:numel(xdata)/2),x(1),x(2),x(3),x(4)); f2(xdata(1:numel(xdata)/2),x(1),x(2),x(3),x(4))];

x0=[- - - -];

x=lsqcurvefit(fun,x0,xdata,ydata);

where f1(...),f2(...) must be column vectors each of the same size as x.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Kaushik Lakshminarasimhan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/419277-simultaneous-fitting-of-two-plots-with-two-functions#comment_611116

## Arun Kumar Bar (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/419277-simultaneous-fitting-of-two-plots-with-two-functions#comment_611253

Sign in to comment.