How can i correct the function below?
Show older comments
Write a function that is called like this: amag = accelerate(F1,F2,m). F1 and F2 are three-element column vectors that represent two forces applied to a single object. The argument m equals the mass of the object in units of kilograms. The three elements of each force equal the x, y, and z components of the force in Newtons. The output variable amag is a scalar that is equal to the magnitude of the object’s acceleration. The function calculates the object’s acceleration vector a by using Newton’s law: F = ma, where F is the sum of F1 and F2. Then it returns the magnitude of a. Hint: we are talking about physical vectors here, so the Pythagorean theorem will come in handy.
this was my function:
function a = accelerate(F1,F2,m)
Fa = sqrt(((F1(1,1))^2)+((F1(2,1))^2)+((F1(3,1))^2));
Fb = sqrt(((F2(1,1))^2)+((F2(2,1))^2)+((F2(3,1))^2));
Ft = Fa + Fb;
a = Ft/m;
end
Upon using the function checker it gives the feedback:
Feedback: Your function performed correctly for argument(s) [1;0;0], [0;0;0], 1
Feedback: Your function performed correctly for argument(s) [1;0;0], [1;0;0], 1
Feedback: Your function performed correctly for argument(s) [0;1;0], [0;1;0], 1
Feedback: Your function performed correctly for argument(s) [0;0;1], [0;0;1], 1
Feedback: Your function made an error for argument(s) [1;0;0], [0;1;0], 1
Your solution is _not_ correct.
Help.
2 Comments
Tip: rather then writing this (with confusing parentheses):
sqrt(((F1(1,1))^2)+((F1(2,1))^2)+((F1(3,1))^2))
it is much simpler to just do this:
sqrt(sum(F1.^2))
Note: your code does not sum the vector forces properly. As this is homework I will not solve it for you, but you can find plenty of tutorials online that show how to sum vector forces.
Aakash Deep
on 4 Jun 2018
It is working fine. I checked it, it is not giving any error.
Answers (1)
Torsten
on 4 Jun 2018
Shouldn't it be
function a = accelerate(F1,F2,m)
F = F1 + F2;
a = norm(F)/m;
end
?
Best wishes
Torsten.
Categories
Find more on Power and Energy Systems 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!