Effacer les filtres
Effacer les filtres

Why is my code not working?

1 vue (au cours des 30 derniers jours)
Derek Gabaldon
Derek Gabaldon le 22 Sep 2019
Commenté : Ankit le 23 Sep 2019
I'm working on a project dealing with a function for a damped harmonic oscillator. I've found the derivate of this function to plot the velocities. I'm being asked to "Superimpose the respective velocities estimating them using a numerical approach." I'm not sure why the code I'm using won't run. I get no errors; the graph just never appears. I've linked the original m file. Any help is much appreciated!
  2 commentaires
David Hill
David Hill le 23 Sep 2019
Modifié(e) : Walter Roberson le 23 Sep 2019
Everything is working except question #5. What are you trying to do with question #5 (it does not make sense to me)? n(i) would be a scalar not a vector and most of your equations can be moved outside the loop. I'm not sure if this is what you wanted or not.
A=1;
g= c/(2*m); %Damping coefficient
wn=sqrt(k/m); %Natural frequency
wd=sqrt(((wn).^2)-(g).^2); %Damped natural frequency
t=linspace(0,20,1000); %Time value
x=A*exp(-g*t).*cos(wd*t); %x(t)
for i = 1:(length(t) -1)
n(i)= (x(i+1)-x(i))/(t(i+1)-t(i));
end
plot (t(1:end-1),n);
hold on
grid on
pause(0.1);
Ankit
Ankit le 23 Sep 2019
Question 4 and Question 5 should have same output:
as in question 4
v = dx/dt, differentiation of displacement using differential calculus
and in question 5
v is approximated as change in x/change in t.
David code is giving also same results

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by