Fitting to a function involving a matrix - error For standard eigenproblem EIG(A), A must be square.
1 view (last 30 days)
Show older comments
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 Comments
Steven Lord
on 3 Aug 2015
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.
Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression 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!