Euler integration pendulum approximation
Afficher commentaires plus anciens
I am trying to solve these equations dx/dt=y dy/dt=θ¨=−gamma*y−ω^2*x for the simple pendulum with euler integration method - xn+1=xn+(dx/dt)n*dt. In matlab I wrote x(i+1)=x(i)+(y(i).*dt); in the for loop. But I have to use this expression (second order aprox.) xn+1=xn+(1.5(dx/dt)n −0.5(dx/dt)n−1)*dt instead for xn+1=xn+(dx/dt)n*dt. I wrote this in the for loop, x(i+1)=x(i)+(1.5*y(i)-0.5*y(i-1))*dt; but it dosn't work. How do I solve the problem? Thanks
My code below:
clear
g=1;
l=1;
omega=sqrt(g/l);
gamma=0;
n=400; %time steps
time=2*pi;
dt=time/n; % time step in seconds
x=zeros(n,1);
y=zeros(n,1);
x(1)=1;
y(1)=0;
for i=1:n
x(i+1)=x(i)+(y(i).*dt); % which I have to replace with xn+1=xn+(1.5(dx/dt)n −0.5(dx/dt)n−1)*dt
y(i+1)=y(i)-gamma*y(i)-((omega^2)*x(i));
end
Réponses (1)
Torsten
le 22 Oct 2015
For the Euler method, it must read:
y(i+1)=y(i)+dt*(-gamma*y(i)-omega^2*x(i));
instead of
y(i+1)=y(i)-gamma*y(i)-omega^2*x(i);
in your code.
Concerning your second question:
Make a starting step with the Euler method and then start your for loop with i=2 instead of i=1.
Best wishes
Torsten.
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!