I am trying to model solutions to a harmonic oscillator in physics using matlab and get "Array indices must be positive integers or logical values"."

5 views (last 30 days)
Why am I getting the "Array indices must be positive integers or logical values." error?
% Set the parameters of the harmonic oscillator
k = 1; % Spring constant
m = 1; % Mass of the particle
% Define the potential energy function of the harmonic oscillator
U = @(x) 0.5 * k * x.^2;
% Define the kinetic energy function of the harmonic oscillator
T = @(p) 0.5 * m * p.^2;
% Define the Hamiltonian of the harmonic oscillator
H = @(x,p) T(p) + U(x);
% Set the initial state of the particle
x0 = 1; % Initial position
p0 = 0; % Initial momentum
% Set the time step and the total simulation time
dt = 0.01;
Tmax = 20;
% Initialize the arrays for storing the position and momentum of the particle
x = x0;
p = p0;
% Perform the numerical integration of the equations of motion
for t = 0 : dt : Tmax
% Calculate the derivative of the position and momentum
dxdt = p / m;
dpdt = -k * x;
% Update the position and momentum of the particle
x = x + dxdt * dt;
p = p + dpdt * dt;
% Store the updated position and momentum in the arrays
x_array(t/dt+1) = x;
p_array(t/dt+1) = p;
end
% Calculate the energy of the particle at each time step
E_array = H(x_array, p_array);
% Plot the first and eighth energy probability functions
figure;
subplot(2,1,1);
histogram(E_array(1:end), 'Normalization', 'pdf');
title('First energy probability function');
subplot(2,1,2);
histogram(E_array(1:end), 'Normalization', 'pdf');
title('Eighth energy probability function');

Answers (1)

Jon
Jon on 8 Dec 2022
Edited: Jon on 8 Dec 2022
The indices you are using in the expression x_array(t/dt+1) etc are not integers. MATLAB indexes arrays using either integer or logical (true false) indices. You must convert your code to use integer indices.
Also, for future reference, you can use the code button on the MATLAB answers so that your code will be nicely formatted (like in the MATLAB editor). Also makes it easier for people to copy and paste the code out to try running it.
  1 Comment
Jon
Jon on 8 Dec 2022
Here's an example of how you might modify your code to use integer indices
% Set the parameters of the harmonic oscillator
k = 1; % Spring constant
m = 1; % Mass of the particle
% Define the potential energy function of the harmonic oscillator
U = @(x) 0.5 * k * x.^2;
% Define the kinetic energy function of the harmonic oscillator
T = @(p) 0.5 * m * p.^2;
% Define the Hamiltonian of the harmonic oscillator
H = @(x,p) T(p) + U(x);
% Set the initial state of the particle
x0 = 1; % Initial position
p0 = 0; % Initial momentum
% Set the time step and the total simulation time
dt = 0.01;
Tmax = 20;
% Initialize the arrays for storing the position and momentum of the particle
x = x0;
p = p0;
% Perform the numerical integration of the equations of motion
t = 0 : dt : Tmax; % assign time vector
numPoints = numel(t); % number of points to evaluate
x_array = zeros(numPoints,1); % preallocate array to hold positions
p_array = zeros(numPoints,1); % preallocate array to hold momentums
for k = 1:numPoints
% Calculate the derivative of the position and momentum
dxdt = p / m;
dpdt = -k * x;
% Update the position and momentum of the particle
x = x + dxdt * dt;
p = p + dpdt * dt;
% Store the updated position and momentum in the arrays
x_array(k) = x;
p_array(k) = p;
end

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!