Azzera filtri
Azzera filtri

Why is my code not working?

1 visualizzazione (ultimi 30 giorni)
Derek Gabaldon
Derek Gabaldon il 22 Set 2019
Commentato: Ankit il 23 Set 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 Commenti
David Hill
David Hill il 23 Set 2019
Modificato: Walter Roberson il 23 Set 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 il 23 Set 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

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by