Borrar filtros
Borrar filtros

Why is my code not working?

1 visualización (últimos 30 días)
Derek Gabaldon
Derek Gabaldon el 22 de Sept. de 2019
Comentada: Ankit el 23 de Sept. de 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 comentarios
David Hill
David Hill el 23 de Sept. de 2019
Editada: Walter Roberson el 23 de Sept. de 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 el 23 de Sept. de 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

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by