Least square fitting - lsqcurvefit - multiple equations - not enough input arguments -

Hello everyone,
I am trying to fit some experimental data to a cerain model,
my data:
x_exp = [0.109112654
0.174029442
0.2775686
0.442708583
0.706098923
1.126193863
1.796225113
2.864892769
4.569366345
7.28791982
];
y_exp = [5247.044317
8170.755912
12604.15367
19261.43738
29160.90041
42700.48525
63815.24278
95016.30828
140804.9657
207196.2571
];
i want to fit this data using lsqcurvefit, but the problem is my model is not a one line code, it is contructed as follows, it has 4 parameters, o is the variable and my final output is G, i want to fit my y_exp with G:
% parameters are k , tau , Gg , G0
A = (o*tau)^-k * cos (k*pi/2);
B = (o*tau)^-k * sin (k*pi/2);
G1 = G0 + (((Gg-G0)* (1+A))/ (((1+A)^2)+B^2));
G2 = ((Gg-G0)* (-B)/ (((1+A)^2)+B^2));
G = ((G1^2 + G2^2)^0.5);
when i used lsqcurvefit, i constructed the code as follows, but i keep getting a message (Not enough input arguments):
% define A & B
A = @(x,xdata)(xdata*x(2))^-x(1) * cos (x(1)*pi/2);
B = @(x,xdata)(xdata*x(2))^-x(1) * sin (x(1)*pi/2);
% Define G' & G''
G1 = @(x,xdata)x(4) + (((x(3)-x(4))* (1+A(x)))/ (((1+A(x))^2)+B(x)^2));
G2 = @(x,xdata)((x(3)-x(4))* (-B(x))/ (((1+A(x))^2)+B(x)^2));
% Define G
G = @(x,xdata)((G1(x)^2 + G2(x)^2).^0.5);
x0 = [0.5 1E-4 1E+7 1E-5];
[x,resnorm,~,exitflag,output] = lsqcurvefit(G,x0,x_exp,y_exp)
can any one please help me with this?

 Accepted Answer

There are few errors in writing the equations. Following code correct those
A = @(x,xdata)(xdata*x(2)).^-x(1) * cos (x(1)*pi/2);
B = @(x,xdata)(xdata*x(2)).^-x(1) * sin (x(1)*pi/2);
% Define G' & G''
G1 = @(x,xdata)x(4) + (((x(3)-x(4))* (1+A(x,xdata)))./(((1+A(x,xdata)).^2)+B(x,xdata).^2));
G2 = @(x,xdata)((x(3)-x(4))* (-B(x,xdata))./(((1+A(x,xdata)).^2)+B(x,xdata).^2));
% Define G
G = @(x,xdata)((G1(x,xdata).^2 + G2(x,xdata).^2).^0.5);
x0 = [0.5 1E-4 1E+7 1E-5];
[x,resnorm,~,exitflag,output] = lsqcurvefit(G,x0,x_exp,y_exp)
However, now the issue is that lsqcurvefit converges to a wrong output, which is not optimal. Are you sure your model is correct?

7 Comments

Hello Ameer,
thank you very much for your prompt response. i really appreciate it. the code now works. i will give you the full dataset so that you can test if it still converges.
but i have a question, how did you know that it converged to a wring output? i appreciate if you can explain this point.
x_exp = [0.109112654
0.174029442
0.2775686
0.442708583
0.706098923
1.126193863
1.796225113
2.864892769
4.569366345
7.28791982
11.62388202
18.53953344
29.56966526
47.16219564
75.2214365
];
y_exp = [5247.044317
8170.755912
12604.15367
19261.43738
29160.90041
42700.48525
63815.24278
95016.30828
140804.9657
207196.2571
301081.9241
431989.2732
596608.3725
836341.5988
1164292.297
];
It converges to the wrong output because the value of variable 'resnorm' is very large. It shows that the predicted and actual values have a very large error. You can also see the predicted values using this
G(x, x_exp)
and these are very different as compared to y_exp. I tried with the new data points, but the result is still the same. Unfortunately, there is no definite way to solve such issues. This might be an issue with the mathematical model. It is also possible that this problem is caused by the initial point. Sometimes, nonlinearity and the model also make it difficult to find a solution.
thank you very much for your explanation, it is clear to me. maybe the values are high because the data has a logarithmic nature. also, i changed the parameter initial point and it definetly enhanced the model.
i have also made a code to calculate R square value.
N = size (y_exp,1); % Sample size
q = 1; % number of independent variables
Se = (sum(((y_exp(:) - G(x,x_exp)).^2))/(N-q))^0.5;
Sy = (sum(((y_exp(:) - mean (y_exp)).^2))/(N-1))^0.5;
R = 1 - ((Se/Sy)^2)
Hi, I get the results below:
Root of Mean Square Error (RMSE): 4082.58620349913
Sum of Squared Residual: 250012651.635021
Correlation Coef. (R): 0.999937252810041
R-Square: 0.999874509557292
Parameter Best Estimate
---------- -------------
x1 -0.790131390160572
x2 0.00322067503166002
x3 -5466.87384660526
x4 4093378.92702334
Hello Alex,
thank you for your comment.
did you get the RMSE and SSE values using my code? or did you use another code?
thanks,
The solution posted by Alex is calculated using another optimization package, called 1stOpt. That is different software, so MATLAB code cannot be used in that.
gotcha. thank you very much for clearing that out. i really appreciate your time and efforts.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!