I am trying to do this problem, but I'm having some issues with steps 2 3 and 4. I thought I did it right, but the values stored in the "velocites" vector all share the same value and they shouldn't. Any help would be appreciated
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Caleb Wilson
el 31 de Ag. de 2017
InitialVelocity = 2 * rand(1) - 1;
F = 1; tau = 1; N = T/dt; T = 10; dt = 0.01; n = 0;
velocities = zeros(1,N); for i = 1:N n = n + 1; Pc = dt/tau; X = rand; if X < Pc vel = ((n-1)*dt) + F * dt; %Collision occurs else vel = F * dt; %No collision occurs end velocities(i) = vel; end
velocities
1 comentario
Respuesta aceptada
Jan
el 31 de Ag. de 2017
Editada: Jan
el 31 de Ag. de 2017
Your assumption is wrong: the values stored in the "velocites" vector do not share the same value. About 1 of 100 is different. Simply try it:
all(velocities == velocities(1))
You get FALSE in the vast majority of cases.
But at least most of the elements of velocities contain the same value , because this is the expected result of the code. In 99% of the cases the elements are set to:
vel = F * dt;
Velocity is force times time. Hm. Perhaps you mean:
a = F / m; % With m is the mass and a the acceleration
vel = vel + a * dt
0 comentarios
Más respuestas (1)
John BG
el 5 de Sept. de 2017
Editada: John BG
el 6 de Sept. de 2017
Hi Caleb
I have reproduced the steps listed in your question, not to optimise code or speed, but to precisely reproduce the experiment.
1.
start variables
clear all;clc;close all
dt=.01;tau=1;m=1;N=1e3;T=N*dt
F=1; % N = kg*m/s^2
pcol=dt/tau % collision probability
n=[1:1:N]; % we can use it but k comes handy
t=[0:dt:T-dt]; % not used
2.
Single run
v=zeros(1,N);
v(1)=randi([-100 100],1,1)/100; % step 1)
for k=2:1:N
X=randi([0 1],1,1);
if X v(k)=F*dt; end
if ~X v(k)=v(k-1)+F*dt; end
end
3.
100 runs
N2=100;
Vn=zeros(N2,N);
for s=1:1:N2
v=zeros(1,N);
v(1)=randi([-100 100],1,1)/100;
for k=2:1:N
X=randi([0 1],1,1);
if X v(k)=F*dt; end
if ~X v(k)=v(k-1)+F*dt; end
end
Vn(s,:)=v;
end
.
zoomed image, full runs are N=1e3 samples long.
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178112/image.jpeg)
.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
.
For the readers
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178113/image.jpeg)
0 comentarios
Ver también
Categorías
Más información sobre Axis Labels en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!