is this code correct?

1 view (last 30 days)
jay jay
jay jay on 13 Dec 2022
Commented: John D'Errico on 13 Dec 2022
finding y(t) for seconds in steps of 0.1 seconds.
given:
m = 25.0 kg, c is the damping factor = 5.0 N-s/m, k is the spring constant = 200.0 N/m, A = 5.0 m, B = 0.25 m.
code:
m=25;
c=5;
k=200;
A=5;
B=0.25;
t=0:0.1:10;
y = exp((-c/(2*m))*t).*(A*sin(sqrt((k/m)-((c/2*m)^2))*t)+cos(sqrt((k/m)-((c/2*m)^2))*t))
  1 Comment
John D'Errico
John D'Errico on 13 Dec 2022
There was no need to post the same question twice, after only a few minutes of waiting. Are you that impatient to have an answer?

Sign in to comment.

Answers (2)

Jan
Jan on 13 Dec 2022
The equation of an underdamped mass-spring-dashpot system is:
This does neither match
y = exp((-c/(2*m))*t).*(A*sin(sqrt((k/m)-((c/2*m)^2))*t)+cos(sqrt((k/m)-((c/2*m)^2))*t))
nor
So my bold guessing is that your code is not correct.

John D'Errico
John D'Errico on 13 Dec 2022
Why do you think something is wrong? :)
I really like the way Answers shows a symbolic expression in a nice viewable form. Often it can help to see if you have made a mistake in an expression. But a live script should do the same for you in MATLAB proper. Anyway, take a look at the formula below, and see if you notice anything strange. I did. I copied exactly the expression you wrote into the line below, though I did not assign the other parameters the values you have assigned, as that would make it difficult to know if you had made a mistake.
syms m c k A B t
y = exp((-c/(2*m))*t).*(A*sin(sqrt((k/m)-((c/2*m)^2))*t)+cos(sqrt((k/m)-((c/2*m)^2))*t))
y = 
Do you see a problem there? I do. It points out a mistake you made twice, in that
c/2*m
ans = 
is NOT the same thing as
c/(2*m)
ans = 
An easy mistake to make perhaps, especially in a long messy expression. Sometimes you can read a line of code over and over again, not seeing the obvious, because a missing pair of parens is easy to miss in a long line like that.
Next, look carefully. Is there a term with B in it? Is B begone? And then look more carefully yet, because A has been multiplied by the cosine term, not the sine term. So there were actually two more mistakes.
Again, the pretty display of an expression is a useful tool in problems like this.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!