1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| % This matlab program computes the kinetic and potential energies
% of a particle under the action of a conservative force
clear all;
clf;
% Initialisation of alpha and beta
alpha =1.0;
beta=0.1;
% Input the initial position
x=input('Enter the initial value of the displacement in meters:');
% set up the initial velocity, mass and time step
v=0; % Initial velocity in meters/s.
mass = 1.0 % Mass of particle
step = 0.02 %time step in seconds
Kenergy(1) =0.5 * mass * v^2; % kinitic energy
Uenergy(1) = -0.5 *alpha * x^2 + beta * x^4; % potential energy
Tenergy(1) = Kenergy(1) + Uenergy(1); % total energy
Nstep = 601
% start the calculation
% start preparing the graphics
xlabel('Time (s)'); % x-axis label
ylabel('Enrgy (J)'); % y-axis label
title('Kinitic energy (-) and potential energy (+) and total (*)')
hold on;
for istep = 1: Nstep % make the number of steps necessary
Kenergy (istep) =0.5 * mass * v^2; % kinitic energy
Uenergy (istep)= -0.5 *alpha * x^2 + beta * x^4; % potential energy
t (istep) = (istep -1) * step; % time
Force = alpha * x - 4 * beta * x^3; % compute the force on the particle
accel = Force / mass; % compute the accel. on the particle
% compute the velocity and position using Euler-Cromer algo.
v = v + accel * step; x = x + v * step;
endfor
Energylimit1 =max(Kenergy) + 0.1
Energylimit2 =min(Uenergy) - 0.1
%Energylimit =int32 (Energylimit+0.5)
axis([0, Nstep * step, Energylimit2(1), Energylimit1(1)]); % Set axis limits
plot(t,Kenergy,'r-'); % plot the Kinetic energy
plot(t,Uenergy,'b+'); % plot the potential energy |
Partager