Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot work with matrix data #291

Open
thudjx opened this issue Jan 6, 2025 · 0 comments
Open

Cannot work with matrix data #291

thudjx opened this issue Jan 6, 2025 · 0 comments

Comments

@thudjx
Copy link

thudjx commented Jan 6, 2025

Sorry to ask question in the issue page here, but I really have struggled with the problem following for a time and cannot figure it out.

I am trying to use NLsolve to perform least-square-fitting. The kpts acts as xs in normal sense, which is of shape (n, 2) with each row be a point in 2d. The data to be fitted are the eigenvals at those points, which is obtained through dirac_2layer:

function dirac_1layer(kpts, p)
	γ0, V1 = p
	eigs = Array{Float64}(undef, 2, size(kpts, 1))
	for ik in axes(eigs, 2)
		f = f_gen(vec(kpts[ik, :]))
		fc = conj(f)
		h11 = [0 -γ0*f; -γ0*fc 0]
		hpot = diagm([V1, V1])
		eigs[:, ik] = eigvals(h11 .+ hpot)
	end
	return vec(eigs)
end

Random.seed!(1234)
# - initial values of params -
γ0 = 2.5
V10 = -0.097611
# - generate testing data -
kpts = rand(100, 2)
ys0 = dirac_1layer(kpts, [γ0, V10])
ys = ys0 .+ randn(size(ys0)) .* 0.1	
# - fit -
function residual!(dy, p)
	dy .= dirac_1layer(kpts, p) .- ys
end
nlsolve(residual!, [γ0, V10])

The error is like:
image

In fact, I also have tried the not in-place syntax:

...
# - fit -
function residual(p)
	return dirac_1layer(kpts, p) .- ys
end
	
nlsolve(residual, [γ0, V10])

And the error occur at the same line:
image

Is there something I am missing here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant