Clear Filters
Clear Filters

Why is my code not working?

1 view (last 30 days)
Derek Gabaldon
Derek Gabaldon on 22 Sep 2019
Commented: Ankit on 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 Comments
David Hill
David Hill on 23 Sep 2019
Edited: Walter Roberson on 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 on 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

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!