Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please change the code below according to the question above (has fixed numbers instead of user input and also uses functions instead of while loop)--

image text in transcribed

Please change the code below according to the question above

(has fixed numbers instead of user input and also uses functions instead of while loop)-- Main difference that needs to be changed

CODE:

clear % removes items from the workspace

%----given information---

% using ; to suppress output m = [240 120 360]; % array of masses in grams v0 = [30 15 -45]; % array of velocities in cm/s [vA,vB,vC,vD] = deal(zeros(1,3)); % array of final velocities in % different collisions

% Collision A vA(1) = v0(1); [vA(2),vA(3)] = final_velocities(m(2),v0(2),m(3),v0(3)); vA % display new velocities [checkP_A,checkE_A] = check_P_E_total(m,v0,vA)

% Collision B vB(3) = vA(3); [vB(1),vB(2)] = final_velocities(m(1),vA(1),m(2),vA(2)); vB % display new velocities [checkP_B,checkE_B] = check_P_E_total(m,vA,vB)

% Collision C vC(1) = vB(1); [vC(2),vC(3)] = final_velocities(m(2),vB(2),m(3),vB(3)); vC % display new velocities [checkP_C,checkE_C] = check_P_E_total(m,vB,vC)

% Collision D vD(3) = vC(3); [vD(1),vD(2)] = final_velocities(m(1),vC(1),m(2),vC(2)); vD % display new velocities [checkP_D,checkE_D] = check_P_E_total(m,vC,vD)

% function to calculate intial momentum and kinetic energy function [P,E] = P_E_total(m,v) P = sum(m.*v); E = sum(0.5*m.*v.^2); end

% function to check the inital and final momentum and kinetic energy function [checkP,checkE] = check_P_E_total(m,vi,vf) [Pi,Ei] = P_E_total(m,vi); [Pf,Ef] = P_E_total(m,vf); checkP = Pf-Pi; checkE = Ef-Ei; end

% function to calculate final velocities of the carts after a collision function [v1f,v2f] = final_velocities(m1,v1i,m2,v2i) M = m1+m2; dM = m1-m2; v1f = (2*m2*v2i + v1i*dM)/M; v2f = (2*m1*v1i - v2i*dM)/M; end

Exercise M6 Start with your script for M3 (three carts colliding) and update it to include user inputs and user- defined functions, so that it can solve any set of three masses and three initial velocities. ADDITIONAL DESIGN SPECIFICATIONS 9. Build a script with a user-defined function to compute the final velocities of two carts, for any set of three initial velocities and masses of the carts input by the user. If there is ambiguity about which two carts collide first, ask the user to specify which collision is first. 10. Use a WHILE statement to keep computing the result of each collision until there are no more collisions. Output the final velocity array after each collision. At the end, display something like, "There are no more collisions." 11. Check that total energy and total momentum are both conserved after each collision. Output the results only if there is a problem. If the checks are inside the user-defined function, use global variables for the initial values of total momentum and total energy. 12. Use a counter to keep track of the number of collisions and to make the output more meaningful. Output a statement that includes the collision number associated with each set of "final" velocities. 13. Arrays and dot operations should be used to maximize efficiency. 14. Test your script using the values from M3. Submit the output (as a PDF) and your M file (.m). Exercise M6 Start with your script for M3 (three carts colliding) and update it to include user inputs and user- defined functions, so that it can solve any set of three masses and three initial velocities. ADDITIONAL DESIGN SPECIFICATIONS 9. Build a script with a user-defined function to compute the final velocities of two carts, for any set of three initial velocities and masses of the carts input by the user. If there is ambiguity about which two carts collide first, ask the user to specify which collision is first. 10. Use a WHILE statement to keep computing the result of each collision until there are no more collisions. Output the final velocity array after each collision. At the end, display something like, "There are no more collisions." 11. Check that total energy and total momentum are both conserved after each collision. Output the results only if there is a problem. If the checks are inside the user-defined function, use global variables for the initial values of total momentum and total energy. 12. Use a counter to keep track of the number of collisions and to make the output more meaningful. Output a statement that includes the collision number associated with each set of "final" velocities. 13. Arrays and dot operations should be used to maximize efficiency. 14. Test your script using the values from M3. Submit the output (as a PDF) and your M file (.m)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions

Question

1. Describe the power of nonverbal communication

Answered: 1 week ago