# Not sure what's happening in this code and not sure where to come from here.

25 views (last 30 days)
Steve Avilesmejia on 2 Dec 2020
Answered: Alan Stevens on 3 Dec 2020
clc; clear all; close all
L = 30;
m = 68.1;
c_d = 0.25;
k = 40;
gamma = 8;
g = 9.81;
h = 0.1;
t = 0: h: 50;
n = length(t);
v = zeros(2, n);
v(:,1) = [0;0];
y = t(2)
for i = 2:n
if y(1) <= L
dy = [y(2);
g - sign(y(2))*c_d*y(2)^2/m];
else
dy = [y(2);
g - sign(y(2))*c_d*y(2)^2/m - k*(y(1)-L)/m - gamma*y(2)/m];
v(:,i) = v(:, i-1) + fun(v(:,i-1), L, m, c_d, k, gamma, g)*h;
end
end

Image Analyst on 2 Dec 2020
Can't you ask the person who wrote the code for you?
Steve Avilesmejia on 2 Dec 2020
what im trying to say is that i dnt know why its not working, i wrote it

Alan Stevens on 3 Dec 2020
You are not keeping track of parameters through time. Try
L = 30;
m = 68.1;
c_d = 0.25;
k = 40;
gamma = 8;
g = 9.81;
h = 0.1;
t = 0: h: 50;
n = length(t);
% You need dx/dt = v; and dv/dt = acceleration
% Simple Euler approach
x = zeros(1,numel(t)); % Allocate storage space
v = zeros(1,numel(t)); % Allocate stirage space
x(1) = 0; % Initial position
v(1) = 0; % Initial velocity
for i = 1:numel(t)-1
x(i+1) = x(i) + h*v(i); % update position
% Select acceleration
if x(i) < L
accn = g - sign(v(i))*c_d*v(i)^2/m;
else
accn = g - sign(v(i))*c_d*v(i)^2/m - k*(x(i)-L)/m - gamma*v(i)/m;
end
v(i+1) = v(i) + h*accn; % update velocity
end
% Plot position and velocity vs time
plot(t,x,t,v),grid
xlabel('time'),ylabel('Posn and Vel')
legend('Posn','Vel')