INTRODUCTION TO MATLAB Creating basic scripts
PROJECTILE 1
POWEER! Stupid pig.. Angry Birds®
Newton`s equation wektory
Newton`s equation Dv/dt = a!
Dv/dt=a
Newton`s equation – algorithm we
Newton`s equation – algorithm Nad r wektor
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. RADIAN X(t-1) Acceleration in x direction is 0.
PLOT
scatter
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.
TO NAME YOUR SERIES FOR PLOT
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)
BUT SOME EXAMPLES FIRST PROJECTILE - loop BUT SOME EXAMPLES FIRST 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 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=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 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
CHANGE: TO RUN CALCULATION plot (x,y) xlabel('distance') ylabel('height') TO plot1(x,y,dt,vx,vy) RUN CALCULATION
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!!!!!!!!!!!!!!!!!!!!!!
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…
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) legend ('total energy','kinetic energy','potential energy','Location', 'NorthEast')
Result!
NOW CHANGE VALUE OF STEPS TO 2:150 Result! NOW CHANGE VALUE OF STEPS TO 2:150
PROJECTILE - results
PROJECTILE - results
SPRING One mass
L0
L0 x
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
1 2 Equilibrium position 5 3 s 4 Attachement point
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);
ARE THE CALCULATIONS CORRECT? What about the total energy? ARE THE CALCULATIONS CORRECT?
ENERGY CONSERVATION LAW Ep Ek
ENERGY CONSERVATION LAW
ENERGY CONSERVATION LAW Looks familiar?
ENERGY CONSERVATION LAW Looks familiar?
ENERGY CONSERVATION LAW ADD after your loop: v=v*sqrt(m/k); PLOT: plot(x,v)
RESULT A
CHANGE: plot(x,v) TO plot4(v,x,dt) RUN CALCULATION
SPRING Two masses
1 2 L
2 1 L Δx1 L+Δx2
F1= k*Δl F2= -k*Δl 1 2 x1 x2 -0.1 L 0.1 X2-x2= Odwrocic dl
SPRING – initial values clc clear all l=2; m=1; k=3; dt=0.1;
SPRING – initial values 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
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
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?
RESULTS
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
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
We can use our program to get… Atomic motion Spectrum Fourier’s transform cl wavenumber 1.678*1013 𝜈 =559.72 𝑐 𝑚 −1 1/1.678*1013
SEE YOU TOMMOROW!