ploting second derivative from second order differential equationn

11 vues (au cours des 30 derniers jours)
Muhammad Sarmad Zahid
Muhammad Sarmad Zahid le 18 Mar 2021
Commenté : Jan le 20 Mar 2021
Hi, Beloow is my code and i am solving second order differential equation numericaly and i am getting plot of first and zeroth derivation from ode45. is there any way to plot the second derivative as well by just calling ode45 ?
function fval = simulation( t,y )
x=y(1);
v=y(2);
c=6850;
m=450;
k=0;
f=2650;
e=535;
w=4410;
fval(1,1)=v;
%fval(2,1)= -(c*v+k*x)/m
fval(2,1)=(-c*v-f-e+w+k*x)/m;
end
%%%%main file%%%%%%
c=6850;
m=450;
k=0;
f=2650;
e=535;
w=4410;
Y0= [0;3.86];
tspan= [0 1];
options= odeset ('Events', @event);
[tsol,ysol] = ode45(@(t,y) simulation(t,y), tspan, Y0,options);
figure
plot(tsol,ysol(:,1), 'r-');
hold on;
figure
plot(tsol,ysol(:,2), 'g-');
hold on;
figure

Réponse acceptée

Jan
Jan le 18 Mar 2021
The easiest solution is to let the function to be integrated accept vectors as input and call it with the time steps and positions replied by ODE45:
[tsol, ysol] = ode45(@(t,y) simulation(t,y), tspan, Y0,options);
dy = simulation(tsol, ysol.').';
d2y = dy(:, 2);
function fval = simulation(t, y)
x = y(1, :);
v = y(2, :);
c = 6850;
m = 450;
k = 0;
f = 2650;
e = 535;
w = 4410;
fval(1, :) = v;
fval(2, :) = (-c * v - f - e + w + k * x) / m;
end
  12 commentaires
Jan
Jan le 20 Mar 2021
But this is the same question again?

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by