is this code correct?
1 view (last 30 days)
Show older comments
finding y(t) for
seconds in steps of 0.1 seconds.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1229537/image.png)
given:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1229542/image.png)
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
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?
Answers (2)
Jan
on 13 Dec 2022
The equation of an underdamped mass-spring-dashpot system is:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1229557/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1229562/image.png)
So my bold guessing is that your code is not correct.
0 Comments
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))
Do you see a problem there? I do. It points out a mistake you made twice, in that
c/2*m
is NOT the same thing as
c/(2*m)
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.
0 Comments
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!