Fitting to a function involving a matrix - error For standard eigenproblem EIG(A), A must be square.

I am trying to fit to a function f(x,y) of the form
function[E]=three_orbital_dispersion(kx,ky,E0,Emp,Ems,tmp,tms,tx,tmps,tmpx,tmsx)
a=3.95;
f1=2.*cos(kx.*a)+4.*cos(0.5.*kx.*a).*cos((0.75^0.5).*ky.*a);
f2=exp(1i.*3^(-0.5).*ky.*a)+2.*exp(-0.5.*1i.*3^(-0.5).*ky.*a).*cos(0.5.*kx.*a);
cf2=conj(f2);
H=[Emp+f1.*tmp,f1.*tmps,f2.*tmpx;f1.*tmps,Ems+f1.*tms,f2.*tmsx;cf2.*tmpx,cf2.*tmsx,f1.*tx];
energy=min(eig(H));
E=E0+energy;
When I evaluate this on its own it works fine
e.g.
three_orbital_dispersion(0.3,0.7,1,1,1,1,-1,1,1,1,0.1)
gives
ans=-1.658
however, when I try and use the function to fit to a custom model in cftool I get the following error message
Expression three_orbital_dispersion(kx,ky,E0,Emp,Ems,tmp,tms,tx,tmps,tmpx,tmsx) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:
Error in fittype expression ==> three_orbital_dispersion(kx,ky,E0,Emp,Ems,tmp,tms,tx,tmps,tmpx,tmsx)
??? For standard eigenproblem EIG(A), A must be square.
What's going on? The matrix H is square, and the function seems to work fine outside the fit.
P.S. I get the same problem if I write a script using
ft = fittype( 'three_orbital_dispersion(kx,ky,E0,Emp,Ems,tmp,tms,tx,tmps,tmpx,tmsx)', 'independent', {'kx', 'ky'}, 'dependent', 'e' );
In which case the error messages are
Error using fittype/testCustomModelEvaluation (line 12) Expression three_orbital_dispersion(kx,ky,E0,Emp,Ems,tmp,tms,tx,tmps,tmpx,tmsx) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated: Error in fittype expression ==> three_orbital_dispersion(kx,ky,E0,Emp,Ems,tmp,tms,tx,tmps,tmpx,tmsx) ??? For standard eigenproblem EIG(A), A must be square.
Error in fittype>iCreateFittype (line 371) testCustomModelEvaluation( obj );
Error in fittype (line 328) obj = iCreateFittype( obj, varargin{:} );
Error in three_orbital_fit_script (line 5) ft = fittype( 'three_orbital_dispersion(kx,ky,E0,Emp,Ems,tmp,tms,tx,tmps,tmpx,tmsx)', 'independent', {'kx', 'ky'}, 'dependent', 'e' );
Caused by: Error using fittype/evaluate (line 102) Error in fittype expression ==> three_orbital_dispersion(kx,ky,E0,Emp,Ems,tmp,tms,tx,tmps,tmpx,tmsx) ??? For standard eigenproblem EIG(A), A must be square.

5 个评论

Make sure that "three_orbital_dispersion" is only called with scalar values for the input data, not vectors or matrices.
Best wishes
Torsten.
I'm pretty sure it's only called with scalar values - kx,ky are the x and y data given to the fitting tool, and all the other parameters are scalar parameters. I've fitted to other functions with no problems (being careful to use elementwise multiplication) - it's the determinant of the matrix which seems to be the issue. If cftool is calling the function with the vectors kx,ky, how do I get round this?
Thanks, Sam
Generate E in a for-loop from ii=1 to ii=length(kx).
Best wishes
Torsten.
Add a DISP statement to display the SIZE of H immediately before you call EIG, then run the code to reproduce the problem. It will show you what size MATLAB thinks H is; if that's not the size you think it should be, then you can work backwards (setting breakpoints on earlier lines and rerunning the example) to debug why it's a different size.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by