linearly spaced vector between multiple (four) points?

8 views (last 30 days)
I am working on a problem, in which I do calculations with 4 weighted delta functions. Integration over the x-axis (energy) should yield zero, but the individual energy contributions can differ. So far, this works in the following code:
energy1=-0.63;
energy2=-0.3;
energy2_amplitude=0.2;
energy3=0.15;
energy4=2.7;
energy3_amplitude=0.25;
wlin=linspace(-3,3,1001);
Af = dirac(wlin-energy1); %create dirac gamma function at energy 1
idx = find(Af == Inf); %find index of amplitude function that is infinite
Af(idx) = (1-energy2_amplitude)./(wlin(2)-wlin(1)); %give delta function weight, but keep normalized over state 1+2 or 3+4 respectively
Af = Af + dirac(wlin-energy2); %add second gamma function at energy 2
idx2 = find(Af == Inf);
Af(idx2) = (energy2_amplitude)./(wlin(2)-wlin(1));
Af = Af + dirac(wlin-energy3);
idx3 = find(Af == Inf);
Af(idx3) = -(energy3_amplitude)./(wlin(2)-wlin(1));
Af = Af + dirac(wlin-energy4);
idx4 = find(Af == Inf);
Af(idx4) = -(1-energy3_amplitude)./(wlin(2)-wlin(1));
figure(1)
plot(wlin,Af)
xlabel('Energy (eV)')
ylabel('Amplitude')
Now, the problem is that the finite values of the delta function don't appear if their energy does not match the energy spacing of wlin. Slight variations of the four energies makes the Af (Amplitude function) be zero everywhere.
The energies don't necessarily stay static, so the energy vector should accomodate the variation of the energies.
So I would need to construct an energy vector that contains all energy values AND be equally spaced AND be symmetric around 0 (this is important for further calculations). I tried 1d-interpolation between the energies and finding common denominators, but to no success. How can I create a linearly spaced vector, similar to linspace, but with more constraints like in my case?
  1 Comment
Paul
Paul on 12 Feb 2024
Does this have to be done numerically? Symbolically it's straightforward
sympref('default');
syms w e1 e2 e3 e4
energy1=-0.63;
energy2=-0.3;
energy3=0.15;
energy4=2.7;
I don't quite understand what the weights are supposed to be, so we'll leave them as symbolic for now.
Af(w) = sum([e1 e2 e3 e4].*dirac(w-[energy1 energy2 energy3 energy4]))
Af(w) = 
It would take a small bit of work to plot Af once e1-e4 are defined with numbers. But not too much work
syms v
intAf(w) = int(Af(v),v,-inf,w)
intAf(w) = 
Hmm, don't know why that one term isn't expresssed in terms of heaviside
intAf = rewrite(intAf,'heaviside')
intAf(w) = 

Sign in to comment.

Answers (1)

Hassaan
Hassaan on 8 Feb 2024
Edited: Hassaan on 8 Feb 2024
@Lukas An intial attempt:
% Specified energy values
energies = [-0.63, -0.3, 0.15, 2.7];
% Calculate the minimum non-zero difference between any two unique energies
energies_sorted = sort(unique(energies));
min_diff = min(diff(energies_sorted));
% Determine a suitable range that covers all energies and is symmetric about 0
energy_range = max(abs(min(energies)), abs(max(energies)));
% Calculate the number of steps required from 0 to the maximum range, considering the minimum difference
num_steps = ceil(energy_range / min_diff);
% Ensure the total steps are odd to include 0 exactly in the center
if mod(num_steps, 2) == 0
num_steps = num_steps + 1;
end
% Calculate the actual step size to maintain symmetry
actual_step_size = (energy_range * 2) / (num_steps * 2);
% Create the symmetric, equally spaced vector
wlin_custom = linspace(-energy_range, energy_range, int32(num_steps * 2 + 1));
% Verification: Check if specified energies are approximately included in the vector
included_energies_check = arrayfun(@(e) any(abs(wlin_custom - e) < actual_step_size / 2), energies);
% Display results
disp('Generated Vector:');
Generated Vector:
disp(wlin_custom);
-2.7000 -2.4000 -2.1000 -1.8000 -1.5000 -1.2000 -0.9000 -0.6000 -0.3000 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000
disp('Step Size:');
Step Size:
disp(actual_step_size);
0.3000
disp('Included Energies Check:');
Included Energies Check:
disp(included_energies_check);
1 1 1 1
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
A multiverse of answers and solutions.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  3 Comments
Hassaan
Hassaan on 8 Feb 2024
Edited: Hassaan on 8 Feb 2024
@Dyuman Joshi First of all I really appreciate your comments and any possible issues in the anwer that you point out.
Its already mentioned 'initial attempt'.
'exact matches for certain energy values like -0.63 and 0.15 may not always be directly achievable with evenly spaced intervals while also maintaining symmetry and a fixed step size.
Given the constraints and the goal of including specific energy values within an equally spaced and symmetric vector, a precise match might require a different approach or accepting a compromise between exact inclusion of energy values and maintaining other constraints.'
Other more accurate ways may be available. The implementation may or may not be perfect. Sometimes understanding issues can happen.
A multiverse of answers and solutions.
Other community members like yourself are also welcomed to provide constructive feedback and guidance.
If it does not anwer the query its upto the OP to accept or not. He/She can provide their feedback as well.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
A multiverse of answers and solutions.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
Lukas
Lukas on 12 Feb 2024
Unfortunately the output does not contain all the energies. Also, the calculated number of energy steps (9 steps) seems way too low for this problem. It also doesn't hold for the variation of energies.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!