ploting second derivative from second order differential equation using for loop
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi I am trying to solve a dynamics problem. I have tried to simplify my code and have successfully ploted zero and first derivatives of two cases. The code is below. How can i call the function so that I can get second derivative of both events. Right now i am only getting second derivativeof first case
here is my code
clc;
clear all;
g=9.81;
y0=[-0.75 0];
c=6850;
m=450;
k=0;
f=2650;
e=535;
w=4410;
tspan = [0 1];
tstart = tspan(1);
t = tstart;
y = y0;
at=[];
ay=[];
for i=1:100
tstart=(i-1)*0.01;
tend=i*0.01;
[at,ay] = ode45(@(t,y) simulation(t,y), [tstart tend], y(end,:));
t = cat(1, t, at(2:end));
y = cat(1, y, ay(2:end,:));
dy = simulation(t, y.').';
end
plot (t,y(:,1))
figure
plot (t,y(:,2))
figure
plot(t,dy(:,2))
%%%%%%function%%%%%%%%
function fval = simulation( t,y )
x = y(1, :);
v = y(2, :);
c=6850;
m=450;
k=0;
f=2650;
e=535;
g=9.8;
if y(1, :)>=0
F = -c * v - f - e ;
else
F=0;
end
fval(1, :) = v;
fval(2, :) = (F + (m*g) + k * x) / m;
end
5 comentarios
Muhammad Sarmad Zahid
el 20 de Mzo. de 2021
Editada: darova
el 20 de Mzo. de 2021
Jan
el 20 de Mzo. de 2021
These lines of your code are meaningless:
dy = [];
g=9.81;
c=6850;
m=450;
s=[];
f=2650;
e=535;
w=4410;
at=[];
ay=[];
Q1 = [t(end), tend; at(1), at(end); ay(1,:); ay(end,:)]
Filling your program with useless line will increase the confusion level. So I recomment to omit them.
I still have no idea, why you collect the output twice:
% Collect the output
t = cat(1, t, at(2:end));
y = cat(1, y, ay(2:end,:));
% Store the output another time:
tC{end + 1} = at;
yC{end + 1} = ay;
You cannot collect the value of d2y in exactly the same way, because you have used the method to start t and y with their initial value, but d2y has no initial value at startup. So you need this:
d2y = [];
while t(end) < tend
[at,ay] = ode45(fcn, [t(end) tend], y(end,:), options);
% Collect the output
t = cat(1, t, at(2:end));
y = cat(1, y, ay(2:end,:));
dy = fcn(t.', y.').';
if t(end) < tend % Crop last element:
d2y = cat(1, d2y, dy(1:end-1, 2);
else
d2y = cat(1, d2y, dy(:, 2);
end
...
Then plot(t,d2y) will work also.
Respuestas (0)
Ver también
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects 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!