# simultaneously, fitting two functions with two databases

3 views (last 30 days)

Show older comments

I have two data sets y1(x00) and y2(x00) in the x00 range, as:

x00=[0,1,1.5,2,2.5,3,3.5,4,4.5,5]*2*pi/3;

y1=[0,1.082,2.27,3.91,5.93,8.24,10.69,13.02,14.83,15.55];

y2=[36.39,36.16,35.81,35.24,34.40,33.27,31.88,30.39,29.11,29.00];

I need fitting both functions f1=@(c1,c21,c22,c3,x) and f2=@(c1,c21,c22,c3,x), with these data in ranges x00.

Note that [c1,..,c4] are required constant coefficients and x is the varibale in range of the x00. I need to obtain the [c1,..,c4] parameters.

In my search, I found nonlinear least squre methodes with matlab, but please take into account that I am new to Matlab and can only curve fit very basic data points.

x00=[0,1,1.5,2,2.5,3,3.5,4,4.5,5]*2*pi/3;

y1=[0,1.082,2.27,3.91,5.93,8.24,10.69,13.02,14.83,15.55];

y2=[36.39,36.16,35.81,35.24,34.40,33.27,31.88,30.39,29.11,29.00];

f1=@(c1,c21,c22,c3,x) sqrt( 2.0*c21*cos(1.5*x) - 3.0*c3 - 2.0*c21 - 2.0*c22 - 3.0*c1 + 2.0*c22*cos(1.5*x) - 1.0*exp(-x*1.5i)*(c1*c3 + 2.0*c1^2*exp(x*4.5i) + 2.0*c1^2*exp(x*1.5i) + 5.0*c1^2*exp(x*3.0i) + 5.0*c3^2*exp(x*3.0i) + 2.0*c3^2*exp(x*6.0i) + 6.0*c21^2*exp(x*3.0i) + 6.0*c22^2*exp(x*3.0i) + 2.0*c3^2 - 8.0*c21^2*exp(x*3.0i)*cos(1.5*x) + 2.0*c21^2*exp(x*3.0i)*cos(3.0*x) - 8.0*c22^2*exp(x*3.0i)*cos(1.5*x) + 2.0*c22^2*exp(x*3.0i)*cos(3.0*x) + 6.0*c1*c3*exp(x*4.5i) + 6.0*c1*c3*exp(x*1.5i) + 4.0*c1*c3*exp(x*3.0i) + c1*c3*exp(x*6.0i) - 12.0*c21*c22*exp(x*3.0i) + 16.0*c21*c22*exp(x*3.0i)*cos(1.5*x) - 4.0*c21*c22*exp(x*3.0i)*cos(3.0*x))^(1/2));

f2=@(c1,c21,c22,c3,x) sqrt( 2.0*c21*cos(1.5*x) - 3.0*c3 - 2.0*c21 - 2.0*c22 - 3.0*c1 + 2.0*c22*cos(1.5*x) + exp(-x*1.5i)*(c1*c3 + 2.0*c1^2*exp(x*4.5i) + 2.0*c1^2*exp(x*1.5i) + 5.0*c1^2*exp(x*3.0i) + 5.0*c3^2*exp(x*3.0i) + 2.0*c3^2*exp(x*6.0i) + 6.0*c21^2*exp(x*3.0i) + 6.0*c22^2*exp(x*3.0i) + 2.0*c3^2 - 8.0*c21^2*exp(x*3.0i)*cos(1.5*x) + 2.0*c21^2*exp(x*3.0i)*cos(3.0*x) - 8.0*c22^2*exp(x*3.0i)*cos(1.5*x) + 2.0*c22^2*exp(x*3.0i)*cos(3.0*x) + 6.0*c1*c3*exp(x*4.5i) + 6.0*c1*c3*exp(x*1.5i) + 4.0*c1*c3*exp(x*3.0i) + c1*c3*exp(x*6.0i) - 12.0*c21*c22*exp(x*3.0i) + 16.0*c21*c22*exp(x*3.0i)*cos(1.5*x) - 4.0*c21*c22*exp(x*3.0i)*cos(3.0*x))^(1/2));

Any support will help me, thanks.

Please assist.

##### 3 Comments

### Accepted Answer

Torsten
on 14 Aug 2022

I think you will have to work on the representation of f1 and f2 without the imaginary unit:

c10 = 3.0;

c210 = 1.0;

c220 = -1.0;

c30 = 0.5;

C0 = [c10,c210,c220,c30];

x = [0,1,1.5,2,2.5,3,3.5,4,4.5,5]*2*pi/3;

y1 = [0,1.082,2.27,3.91,5.93,8.24,10.69,13.02,14.83,15.55];

y2 = [36.39,36.16,35.81,35.24,34.40,33.27,31.88,30.39,29.11,29.00];

f1 =@(c1,c21,c22,c3) real(2.0*c21*cos(1.5*x) - 3.0*c3 - 2.0*c21 - 2.0*c22 - 3.0*c1 + 2.0*c22*cos(1.5*x) - exp(-x*1.5i).*(c1*c3 + 2.0*c1^2*exp(x*4.5i) + 2.0*c1^2*exp(x*1.5i) + 5.0*c1^2*exp(x*3.0i) + 5.0*c3^2*exp(x*3.0i) + 2.0*c3^2*exp(x*6.0i) + 6.0*c21^2*exp(x*3.0i) + 6.0*c22^2*exp(x*3.0i) + 2.0*c3^2 - 8.0*c21^2*exp(x*3.0i).*cos(1.5*x) + 2.0*c21^2*exp(x*3.0i).*cos(3.0*x) - 8.0*c22^2*exp(x*3.0i).*cos(1.5*x) + 2.0*c22^2*exp(x*3.0i).*cos(3.0*x) + 6.0*c1*c3*exp(x*4.5i) + 6.0*c1*c3*exp(x*1.5i) + 4.0*c1*c3*exp(x*3.0i) + c1*c3*exp(x*6.0i) - 12.0*c21*c22*exp(x*3.0i) + 16.0*c21*c22*exp(x*3.0i).*cos(1.5*x) - 4.0*c21*c22*exp(x*3.0i).*cos(3.0*x))).^(1/2) - y1.^2;

f2 =@(c1,c21,c22,c3) real(2.0*c21*cos(1.5*x) - 3.0*c3 - 2.0*c21 - 2.0*c22 - 3.0*c1 + 2.0*c22*cos(1.5*x) + exp(-x*1.5i).*(c1*c3 + 2.0*c1^2*exp(x*4.5i) + 2.0*c1^2*exp(x*1.5i) + 5.0*c1^2*exp(x*3.0i) + 5.0*c3^2*exp(x*3.0i) + 2.0*c3^2*exp(x*6.0i) + 6.0*c21^2*exp(x*3.0i) + 6.0*c22^2*exp(x*3.0i) + 2.0*c3^2 - 8.0*c21^2*exp(x*3.0i).*cos(1.5*x) + 2.0*c21^2*exp(x*3.0i).*cos(3.0*x) - 8.0*c22^2*exp(x*3.0i).*cos(1.5*x) + 2.0*c22^2*exp(x*3.0i).*cos(3.0*x) + 6.0*c1*c3*exp(x*4.5i) + 6.0*c1*c3*exp(x*1.5i) + 4.0*c1*c3*exp(x*3.0i) + c1*c3*exp(x*6.0i) - 12.0*c21*c22*exp(x*3.0i) + 16.0*c21*c22*exp(x*3.0i).*cos(1.5*x) - 4.0*c21*c22*exp(x*3.0i).*cos(3.0*x))).^(1/2) - y2.^2;

F = @(c)[f1(real(c(1)),real(c(2)),real(c(3)),real(c(4))),f2(real(c(1)),real(c(2)),real(c(3)),real(c(4)))];

C = lsqnonlin(F,C0)

norm(F(C))

C = real(C)

##### 2 Comments

Torsten
on 15 Aug 2022

### More Answers (0)

### See Also

### Products

### Community Treasure Hunt

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

Start Hunting!