Download presentation
Presentation is loading. Please wait.
1
INTRODUCTION TO MATLAB
Creating basic scripts
2
PROJECTILE 1
3
POWEER! Stupid pig.. Angry Birds®
4
Newton`s equation wektory
5
Newton`s equation Dv/dt = a!
6
Dv/dt=a
7
Newton`s equation – algorithm
we
8
Newton`s equation – algorithm
Nad r wektor
9
Newton`s equation – algorithm example
10
Newton`s equation – algorithm example
11
Projectile in LibreOffice Calc
12
t: 0 to 3 [s]
13
t: 0 to 3 [s]
14
Acceleration in x direction is 0.
RADIAN X(t-1) Acceleration in x direction is 0.
23
PLOT
24
scatter
26
Now, let`s try to change initial values of the velocity, Δt and initial angle.
What do you observe?
27
PROJECTILE TASK CALCULATE AND PLOT REAL VALUE OF THE Y COORDINATE. COMPARE IT WITH THE VALUE OF Y CALCULATED WITH NEWTON`S EQUATION.
30
TO NAME YOUR SERIES FOR PLOT
34
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
35
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
36
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
37
PROJECTILE - loop for s=2: % 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)
38
BUT SOME EXAMPLES FIRST
PROJECTILE - loop BUT SOME EXAMPLES FIRST for s=2: % 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)
39
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
40
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
41
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
42
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
43
PROJECTILE - loop for s=2: % 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)
44
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
45
PROJECTILE - results
46
CHANGE: TO RUN CALCULATION plot (x,y) xlabel('distance')
ylabel('height') TO plot1(x,y,dt,vx,vy) RUN CALCULATION
47
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 ALL ENERGIES OVER TIME WHAT DO YOU OBSERVE? Masa!!!!!!!!!!!!!!!!!!!!!!
48
IF YOU PLOT THESE 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…
49
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
50
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
51
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) legend ('total energy','kinetic energy','potential energy','Location', 'NorthEast')
52
Result!
53
NOW CHANGE VALUE OF STEPS TO 2:150
Result! NOW CHANGE VALUE OF STEPS TO 2:150
54
PROJECTILE - results
55
PROJECTILE - results
56
SPRING One mass
57
L0
58
L0 x
59
1 2 Equilibrium position 5 3 s 4 Attachment point
61
SPRING – initial values
clc clear all %initial values x=4; %extention k=0.1; %spring constant m=1; %mass dt=0.1; % time step
62
SPRING – initial values
t(1)=0; %time x(1)=x; %position v(1)=0; %velocity
63
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
64
SPRING - plot plot(t,x) xlabel('time')
legend('position','Location','NorthEast')
65
SPRING - results
66
1 2 Equilibrium position 5 3 s 4 Attachement point
67
RUN CALCULATION ADD ENERGIES (to the loop) THEN ANIMATION
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 Kulka i wykres sinusoidy> energie rysowane> nie są zachowane bo dt jest małe > jak polepszyć??? > zadanie z kulką > feature 3 subplot(2,2,4) xlim([-limits(6) limits(6)]) ylim([-limits(7) limits(7)]) xlabel('position (x)') ylabel('velocity (v)') grid hold on Do loop subplot(2,2,4); trace4=plot(y(k),v(k),'y:o',... 'MarkerFaceColor',[.0 k/numel(y) .7058],... 'Color',[.0 k/numel(y) .7058],... 'LineWidth',3.5);
68
ARE THE CALCULATIONS CORRECT?
What about the total energy? ARE THE CALCULATIONS CORRECT?
69
ENERGY CONSERVATION LAW
Ep Ek
70
ENERGY CONSERVATION LAW
71
ENERGY CONSERVATION LAW
Looks familiar?
72
ENERGY CONSERVATION LAW
Looks familiar?
73
ENERGY CONSERVATION LAW
ADD after your loop: v=v*sqrt(m/k); PLOT: plot(x,v)
74
RESULT A
75
CHANGE: plot(x,v) TO plot4(v,x,dt) RUN CALCULATION
76
SPRING Two masses
77
1 2 L
78
2 1 L Δx1 L+Δx2
79
F1= k*Δl F2= -k*Δl 1 2 x1 x2 -0.1 L 0.1 X2-x2= Odwrocic dl
80
SPRING – initial values
clc clear all l=2; m=1; k=3; dt=0.1;
81
SPRING – initial values
x1(1)=-0.5; x2(1)=l+1; t(1)=0;
82
SPRING - loop
83
SPRING - plot plot(t,x1,t,x2) xlabel('t') ylabel('x')
legend ('x1','x2','Location', 'NorthWest')
84
SPRING - results
85
CHANGE: TO RUN CALCULATION plot5(x1,x2,dt,v1,v2,t) plot(t,x1,t,x2)
xlabel('t') ylabel('x') legend ('x1','x2','Location', 'NorthWest') TO plot5(x1,x2,dt,v1,v2,t) RUN CALCULATION
86
IF YOU PLOT ALL ENERGIES OVER TIME WHAT DO YOU OBSERVE?
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?
87
RESULTS
88
THE PURPOSE! Knowledge and experience = endless possibilieties
Creating models of particles, such as chloride and hydrogen. Working only with computer and creating real experiment. cl
89
Cl2 molecule k = 322.7 N/m – spring constant for chlorine system
𝜇= 𝑚 1 𝑚 2 𝑚 1 + 𝑚 2 =2.903∗ 10 −26 𝑘𝑔 – reduced mass x = 5*10-12 m – typical atomic displacement Two body problem reduced to one body problem by introduction of reduced mass: cl 𝑑 2 𝑥 𝑑 𝑡 2 + k 𝜇 x=0
90
We can use our program to get…
Atomic motion Spectrum Fourier’s transform cl wavenumber 1.678*1013 𝜈 = 𝑐 𝑚 −1 1/1.678*1013
91
SEE YOU TOMMOROW!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.