CREATING BASIC SCRIPTS
PROJECTILE 1
POWEER!
Stupid
pig..
Angry Birds®
Newton`s equation
Newton`s equation
Newton`s equation – algorithm
Newton`s equation – algorithm
Newton`s equation – algorithm example
Newton`s equation – algorithm example
Projectile in LibreOffice Calc
t: 0 to 3 [s]
t: 0 to 3 [s]
Acceleration in x direction is 0.
Now, let`s try to change
initial values of the
velocity, Δt and initial
angle.
What do you observe?
PROJECTILE TASK
CALCULATE AND PLOT REAL VALUE OF THE
Y COORDINATE. COMPARE IT WITH THE
VALUE OF Y CALCULATED WITH NEWTON`S
EQUATION.
PROJECTILE IN MATLAB– initial values
clc
clear all
v=100;
% [m/s] initial value of velocity
alpha=pi/4; % [rad] initial value of angle
g=9.81;
% [m/(s^2)] gravity constant
dt=0.1;
%[s] single time step
In MATLAB you are not supposed to define type of variable
PROJECTILE – initial values
%position
x(1)=0; %[m]
y(1)=0; %[m]
% initial values of vx and vy
vx(1)=v*cos(alpha); %[m/s]
vy(1)=v*sin(alpha); %[m/s]
Number one in brackets mean, that x variable is list (contains many values), and now first
value is being defined
PROJECTILE – initial values
%position
x(1)=0; %[m]
y(1)=0; %[m]
% initial values of vx and vy
vx(1)=v*cos(alpha);%[m/s]
vy(1)=v*sin(alpha);%[m/s]
Number one in brackets mean, that x variable is list (contains many values), and now
first value is being defined
PROJECTILE - loop
for s=2:144
% for each s from 2 to 144. Started from 2, because 1 is
initial value (already defined)
vx(s)=vx(s-1);
vy(s)=vy(s-1)-g*dt; %new vy = previous vy - gravity in delta t
x(s)=x(s-1)+vx(s)*dt; %new position
y(s)=y(s-1)+vy(s)*dt;
end
Loop starts with for (and condition), then repeatable intructions - each after indent, and 'end'.
New vx (in point s) = vx in previous point (s-1)
PROJECTILE - loop
for s=2:144
BUT SOME
EXAMPLES
FIRST
% for each s from 2 to 144. Started from 2, because 1 is
initial value (already defined)
vx(s)=vx(s-1);
vy(s)=vy(s-1)-g*dt; %new vy = previous vy - gravity in delta t
x(s)=x(s-1)+vx(s)*dt; %new position
y(s)=y(s-1)+vy(s)*dt;
end
Loop starts with for (and condition), then repeatable intructions - each after indent, and 'end'.
New vx (in point s) = vx in previous point (s-1)
PROJECTILE – loop example
for s=2
vx(2)=vx(2-1);
vy(2)=vy(2-1)-g*dt; %new vy = previous vy - gravity * timestep
x(2)=x(2-1)+vx(2)*dt; %new position
y(2)=y(2-1)+vy(2)*dt;
end
PROJECTILE – loop example
for s=3
vx(3)=vx(3-1);
vy(3)=vy(3-1)-g*dt; %new vy = previous vy - gravity * timestep
x(3)=x(3-1)+vx(3)*dt; %new position
y(3)=y(3-1)+vy(3)*dt;
end
PROJECTILE - loop
for s=2:144
% for each s from 2 to 144. Started from 2, because 1 is
initial value (already defined)
vx(s)=vx(s-1);
vy(s)=vy(s-1)-g*dt; %new vy = previous vy - gravity in delta t
x(s)=x(s-1)+vx(s)*dt; %new position
y(s)=y(s-1)+vy(s)*dt;
end
Loop starts with for (and condition), then repeatable instructions - each after indent, and 'end'.
New vx (in point s) = vx in previous point (s-1)
PROJECTILE - plot
plot (x,y)
xlabel('distance')
ylabel('height')
Plot will be opened in new window while executing script
Plot(x-axis values list, y-axis values list)
Every text in code is required to be in quotes
PROJECTILE - results
300
250
height
200
150
100
50
0
0
200
400
600
distance
800
1000
1200
CHANGE:
plot (x,y)
xlabel('distance')
ylabel('height')
TO
plot1(x,y,dt,vx,vy)
RUN CALCULATION
PROJECTILE TASK
ADD TO THE LOOP AND CALCULATE POTENTIAL
ENERGY, KINETIC ENERGY AND TOTAL ENERGY,
THEN PLOT THEM.
IF YOU PLOT ALL ENERGIES OVER TIME WHAT DO YOU OBSERVE?
PROJECTILE TASK
ADD TO THE LOOP AND CALCULATE POTENTIAL
ENERGY, KINETIC ENERGY AND TOTAL ENERGY,
THEN PLOT THEM.
IF YOU PLOT THESE ENERGIES OVER TIME WHAT DO YOU OBSERVE?
Remember to
add the initial
values of the
energies
before the
loop…
PROJECTILE – initial value of energy
%energy - initial values
ep(1)=g*y(1)*m; %potential energy
ek=(vx(1)^2+vy(1)^2)*m/2; %kinetic energy
ec(1)=ek(1)+ep(1); %total energy
PROJECTILE– energies added to loop
for s=2:144
vx(s)=vx(s-1);
vy(s)=vy(s-1)-g*dt;
x(s)=x(s-1)+vx(s)*dt;
y(s)=y(s-1)+vy(s)*dt;
ep(s)=g*y(s)*m;
ek(s)=(vx(s)^2+vy(s)^2)*m/2;
ec(s)=ep(s)+ek(s);
t(s)=t(s-1)+dt;
end
PROJECTILE - plot
plot (t,ec)
xlabel('time')
ylabel('energy')
legend ('total energy','Location', 'NorthEast')
% plot of each energy
plot (t,ec,t,ek,t,ep)
xlabel('time')
ylabel('energy')
legend ('total energy','kinetic energy','potential energy','Location',
'NorthEast')
Result!
12000
total energy
kinetic energy
potential energy
10000
energy
8000
6000
4000
2000
0
0
5
10
time
15
Result!
12000
total energy
kinetic energy
potential energy
10000
NOW CHANGE
VALUE OF
STEPS TO 2:150
energy
8000
6000
4000
2000
0
0
5
10
time
15
PROJECTILE - results
6000
total energy
kinetic energy
potential energy
5000
4000
energy
3000
2000
1000
0
-1000
0
5
10
time
15
PROJECTILE - results
6000
total energy
kinetic energy
potential energy
5000
4000
energy
3000
2000
1000
0
-1000
0
5
10
time
15
SPRING
ONE MASS
1
2
Equilibrium position
5
3
s
4
Attachment point
SPRING – initial values
clc
clear all
%initial values
x=4; %extention
k=0.1; %spring constant
m=1; %mass
dt=0.1; % time step
SPRING – initial values
t(1)=0; %time
x(1)=x; %position
v(1)=0; %velocity
SPRING - loop
for i=2:510
v(i)=v(i-1)+(-k*x(i-1)/m)*dt;
x(i)=x(i-1)+dt*v(i);
t(i)=t(i-1)+dt;
end
SPRING - plot
plot(t,x)
xlabel('time')
legend('position','Location','NorthEast')
SPRING - results
5
position
4
3
2
1
0
-1
-2
-3
-4
-5
0
10
20
30
time
40
50
60
1
2
Equilibrium position
5
3
s
4
Attachement point
ADD ENERGIES (to the loop)
ep(i)=k*x(i)^2/2;
ek(i)=m*v(i)^2/2;
ec=ep+ek;
THEN ANIMATION
plot3(x,dt,v,t,ep,ek,ec)
RUN CALCULATION
ENERGY CONSERVATION LAW
ENERGY CONSERVATION LAW
ENERGY CONSERVATION LAW
ENERGY CONSERVATION LAW
ENERGY CONSERVATION LAW
ADD after your loop:
v=v*sqrt(m/k);
PLOT:
plot(x,v)
5
RESULT
4
3
2
x
1
0
-1
-2
-3
-4
-5
-5
-4
-3
-2
-1
0
1
sqrt(m/k)*v
2
3
4
5
CHANGE:
plot(x,v)
TO
plot4(v,x,dt)
RUN CALCULATION
SPRING
TWO MASSES
SPRING – initial values
clc
clear all
l=2;
m=1;
k=3;
dt=0.1;
SPRING – initial values
v1(1)=0;
v2(1)=0;
x1(1)=-0.5;
x2(1)=l+1;
t(1)=0;
SPRING - loop
SPRING - plot
plot(t,x1,t,x2)
xlabel('t')
ylabel('x')
legend ('x1','x2','Location', 'NorthWest')
SPRING - results
3.5
x1
x2
3
2.5
2
x
1.5
1
0.5
0
-0.5
-1
0
5
10
t
15
CHANGE:
plot(t,x1,t,x2)
xlabel('t')
ylabel('x')
legend ('x1','x2','Location', 'NorthWest')
TO
plot5(x1,x2,dt,v1,v2,t)
RUN CALCULATION
SPRING TASK
ADD TO THE LOOP AND CALCULATE POTENTIAL
ENERGY, KINETIC ENERGY AND TOTAL ENERGY,
THEN PLOT THEM.
IF YOU PLOT ALL ENERGIES OVER TIME WHAT DO YOU OBSERVE?
4
ek
ep
ec
3.5
3
RESULTS
en
2.5
2
1.5
1
0.5
0
0
5
10
t
15
THE PURPOSE!
Cl2 molecule
k = 322.7 N/m – spring constant for chlorine system
x=
5*10-12
m – typical atomic displacement
𝑚 𝑚
𝜇 = 𝑚 1+𝑚2 = 2.903 ∗ 10−26 𝑘𝑔 – reduced mass
1
2
Two body problem reduced to one body problem by introduction of reduced mass:
𝑑2𝑥 k
+ x=0
𝑑𝑡 2 𝜇
We can use our program to get…
Atomic motion
Spectrum
Fourier’s transform
wavenumber
1/1.678*1013
𝜈 = 559.72 𝑐𝑚−1
1.678*1013
© Copyright 2026 Paperzz