fminsearch gives weird zick zack solution
2 views (last 30 days)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1407589/image.png)
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 11 Jun 2023
Edited: Torsten
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)
Matt J
on 10 Jun 2023
Edited: Matt J
on 10 Jun 2023
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.
2 Comments
Matt J
on 11 Jun 2023
Then your model (as coded currently) may be inconsistent with your data. But that is an additional reason a grid search could help you. If you get poor agreement even after a grid search, you know you have bug in your model function code.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!