is this code correct?

1 次查看(过去 30 天)
jay jay
jay jay 2022-12-13
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 个评论
John D'Errico
John D'Errico 2022-12-13
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?

请先登录,再进行评论。

回答(2 个)

Jan
Jan 2022-12-13
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 2022-12-13
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.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by