fminsearch gives weird zick zack solution
Show older comments
So, I wanna implement a parameter fit to fit meassurement data to an equation (specifically, I wanna fit data to the "improved generalised steinmetz equation", which you can see here. The average over time period T from eq 1.11 is ignored, since I want the current value).
For a reason I don't understand, the solution fminsearch (and also lsqcurvefit) delivers is just completely off and goes zick-zack through my meassured data. Can someone of you maybe spot the flaw? In my mind it has to be somewhere in the implementation of the 2 equations, which are linked above too, since lsqcurvefit and fminsearch both deliver the same bs.
EDIT: Here is a screenshot of the plot:

Here is my code: At the beginning, I import data (B, P) from a file (which is not included below):
B_pp = max(B) - min(B); % peak-to-peak flux density
T = 1./f;
dt = T ./ 1000;
t = 0:dt:T;
k1 = @(x) x(1)./( (2*pi).^(x(2)-1) .* ...
integral(@(theta) abs(cos(theta)).^x(2) .* 2.^(x(3)-x(2)), 0, 2*pi) );
pv = @(x) k1(x) .* abs(gradient(B, t(2)-t(1))).^x(2) .* B_pp.^(x(3)-x(2));
x = fminsearch(@(x) vecnorm(pv(x) - P), [1,1,1]);
%[x, resnorm, residual] = lsqcurvefit(pv, [0,0,0], B, P, [-Inf, -Inf, -Inf],[Inf, Inf, Inf],opts);
figure; grid on;
plot(B, P, 'bo', B, pv(x), 'r-');
xlabel('Magnetic field B [T]'); ylabel('Power loss p [W/m^3]');
legend('Experimental data', 'Fitted function'); title('Experimental vs. fitted');
9 Comments
Torsten
on 10 Jun 2023
Could you include your fitting function in a mathematical notation ?
sbr
on 11 Jun 2023
Torsten
on 11 Jun 2023
And you think you can just take the value of the integrand
pv = @(x) k1(x) .* abs(gradient(B, t(2)-t(1))).^x(2) .* B_pp.^(x(3)-x(2));
instead of the time-averaged integrand as done in (1.11) ?
sbr
on 11 Jun 2023
My guess is that you are overfitting with the model you use.
k1(x)*B_pp^(x(3)-x(2)) is (almost) independ of your measurement data. Thus you can use only one constant to reflect this expression.
A second constant (in your code: x(2)) can be used for the exponent in the expression abs(gradient(B, t(2)-t(1))).^x(2).
But you use three free constants instead of two in your model.
I suggest you make a test with a model of the form
pv = @(x) x(1) * abs(gradient(B, t(2)-t(1))).^x(2)
and see if it works better.
sbr
on 11 Jun 2023
Torsten
on 11 Jun 2023
Could you plot abs(gradient(B,t(2)-t(1))) over t ?
sbr
on 11 Jun 2023
And by multiplication by a constant and an exponentiation you expect a smooth curve like the blue one ? No chance. I think you have to reconsider if delta B in your integral really means max(B) - min(B). I doubt it. If it wouldn't depend on t as your setting implies , it could have been taken out of the integral.
Answers (1)
It seems unlikely to me that x0=[1,1,1] would be an accurate initial guess. Surely, you chose that arbitrarily. Since you only have 3 unknown parameters, it shouldn't be too computationally expensive to do a discrete grid search for a better initial point.
Categories
Find more on Curve Fitting Toolbox 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!


