53 views (last 30 days)

Show older comments

Khalid Mahmood
on 9 Apr 2021 at 15:04

Fangjun Jiang
on 8 Apr 2021 at 17:22

Edited: Fangjun Jiang
on 9 Apr 2021 at 16:53

Your code has problem. If you have "clear all" at the begining to clear the variables in the memory, then your code has error.

t has n elements. v has n+1 elements. So plot(t,v) will have error.

The resulting curve is due to whatever data left in t or v.

It is a good practice to pre-allocate memory for your variables.

t=zeros(n,1);

v=zeros(n,1);

and then do your for-loop.

Fangjun Jiang
on 9 Apr 2021 at 16:52

This example code could illustrate the problem.

t=0:155;

v=sin(t*pi/400);

plot(t,v);

t(end)=0;

figure;

plot(t,v);

Khalid Mahmood
on 8 Apr 2021 at 17:59

Edited: Khalid Mahmood
on 8 Apr 2021 at 18:03

v(1)=2; %initial v=v0=vi

a(1)=9.8; %g

n=40; dt=2.5; %use values like dt 0.1, 0.5 1.1,1.5, 2, 2.5, 5 and see the result

D=1;rho=0.75;area=.2;m=7.5;

s(1)=0;

t(1)=0;

for i=1:n

t(i+1)=dt*(i-1) %t0=0;

v(i+1)=v(i)+a(i)*dt; %vf=vi+at

a(i+1)=9.8-(D*rho*area/(2*m))*(v(i+1)^2);%acceleration

s(i+1)=s(i)+v(i)*dt; %position

end

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!