Matlab Sheet 4. Conditional Statements and Loops

Similar documents
Matlab Sheet 4 - Solution. Conditional Statements and Loops

Assignment #13: More Logic

Companion. Jeffrey E. Jones

Date: Pd: Unit 4. GSE H Analytic Geometry EOC Review Name: Units Rewrite ( 12 3) 2 in simplest form. 2. Simplify

Physics 201, Midterm Exam 2, Fall Answer Key

PHYS 1111L - Introductory Physics Laboratory I

Matlab Sheet 2. Arrays

MATH 1710 College Algebra Final Exam Review

AP Physics First Nine Weeks Review

UNIVERSITY OF SASKATCHEWAN Department of Physics and Engineering Physics

SPRING 2005 Midterm Exam #1, Part A

Algebra 2 CP. June 2015 Final Exam REVIEW. Exam Date: Time: Room:

UNIVERSITY OF SASKATCHEWAN Department of Physics and Engineering Physics

More applications of quadratic functions

Midterm α, Physics 1P21/1P91

Chapter 3 Kinematics in Two Dimensions; Vectors

ω = k/m x = A cos (ωt + ϕ 0 ) L = I ω a x = ω 2 x P = F v P = de sys J = F dt = p w = m g F G = Gm 1m 2 D = 1 2 CρAv2 a r = v2

Algebra 2 CP Semester 1 PRACTICE Exam January 2015

SHORT ANSWER. Write the word or phrase that best completes each statement or answers the question. x )

UNIVERSITY OF SASKATCHEWAN Department of Physics and Engineering Physics

Practice Questions for Math 131 Exam # 1

Quadratic Applications Name: Block: 3. The product of two consecutive odd integers is equal to 30 more than the first. Find the integers.

Old Exams Questions Ch. 8 T072 Q2.: Q5. Q7.

Faculty of Engineering and Department of Physics Engineering Physics 131 Midterm Examination Monday February 24, 2014; 7:00 pm 8:30 pm

UNIVERSITY OF SASKATCHEWAN Department of Physics and Engineering Physics

3.2 Projectile Motion

MA 125 CALCULUS I SPRING 2007 April 27, 2007 FINAL EXAM. Name (Print last name first):... Student ID Number (last four digits):...

Physics 116A, Section 2, Second Exam A, February 26, Name (Please print)

Physics 116A, Section 2, Second Exam Version B, February 26, Name (Please print)

Final Exam Review: Study Guide Math 3

PHYS 1111L - Introductory Physics Laboratory I

Additional Exercises 10.1 Form I Solving Quadratic Equations by the Square Root Property

Algebra II End of Course Exam Answer Key Segment I. Scientific Calculator Only

University of Guelph. Department of Physics

Physics I Exam 1 Fall 2014 (version A)

UCS Algebra I Semester 1 Exam

Physics 101 Fall 2006: Final Exam Free Response and Instructions

Physics UCSB Winter 2015 First Midterm Exam Tuesday 1/27/2015

The Quadratic Formula. ax 2 bx c 0 where a 0. Deriving the Quadratic Formula. Isolate the constant on the right side of the equation.

THE USE OF A CALCULATOR, CELL PHONE, OR ANY OTHER ELECTRONIC DEVICE IS NOT PERMITTED IN THIS EXAMINATION.

PHY 2130 EXAM 1 B January 25, 2013

PHYS 2211L - Principles of Physics Laboratory I

ADVANCED FUNCTIONS (MHF 4U) FINAL EXAMINATION

Projectile Motion. Chin- Sung Lin STEM GARAGE SCIENCE PHYSICS

For all questions, answer choice E. NOTA" means none of the above answers is correct.

Physics I Exam 1 Fall 2015 (version A)

Physics 115 Mock Midterm Sunday, October 14, 2018 * 1 pm Room 241 Arts Building *

12/14/2010. Directions

1. Adjust your marble launcher to zero degrees. Place your marble launcher on a table or other flat surface or on the ground.

Form #425 Page 1 of 6

MATH Introduction to MATLAB

DO NOT OPEN THIS BOOKLET UNTIL YOU ARE TOLD TO DO SO.

UNIT 3: MODELING AND ANALYZING QUADRATIC FUNCTIONS

Math 1314 Lesson 7 Applications of the Derivative

Math 1314 Test 2 Review Lessons 2 8


Projectile Motion. Figure 1. The system of coordinates for the projectile motion.

2-5 Solving Equations Containing Integers. Warm Up Problem of the Day Lesson Presentation Lesson Quizzes

Elementary Physics October 8, 2007

lim4 4. By the definition of a limit, there is a positive real number such that if 0 x 2. The largest valid value of is

Physics 121, Midterm Exam #3 Tuesday April 20, am 9.30 am. Do not turn the pages of the exam until you are instructed to do so.

Potential Energy & Conservation of Energy

Projectile Motion B D B D A E A E

Important: This test consists of 16 multiple choice problems, each worth 6.25 points.

Math 1325 Final Exam Review

EXAM 3 MECHANICS 40% of the final grade

If you have a conflict, you should have already requested and received permission from Prof. Shapiro to take the make-up exam.

Graphical Vector Addition

UNIVERSITY OF SASKATCHEWAN Department of Physics and Engineering Physics

(A) 10 m (B) 20 m (C) 25 m (D) 30 m (E) 40 m

AP Physics C Summer Assignment Kinematics

Problem: Projectile (CM-1998)

Name & Surname:... No:... Class: 11 /...

Chapter 3 Acceleration

Exam 1 Practice SOLUTIONS Physics 111Q.B

Chapter 3 Acceleration

Announcements. Midterm next week in recitation Ch. 1 6 No quiz in recitation this week. Read Ch. 7 this week. Ch 7 HW not due till NEXT Friday.

1 of 6 10/21/2009 6:33 PM

Unit 1: Mechanical Equilibrium

Concepts in Physics. Friday, October 16th

Spring 2017 Midterm 1 04/26/2017

Objectives 1. Understand and use terminology and notation involved in sequences

PH 1110 Summary Homework 1

Particle Motion Notes Position When an object moves, its position is a function of time. For its position function, we will denote the variable s(t).

ω = k/m x = A cos (ωt + ϕ 0 ) L = I ω a x = ω 2 x P = F v P = de sys J = F dt = p w = m g F G = Gm 1m 2 D = 1 2 CρAv2 a r = v2

2. Linda paid $38 for a jacket that was on sale for 25% of the original price. What was the original price of the jacket?

Unit 1 Test Review Physics Basics, Movement, and Vectors Chapters 2-3

ENGR Spring Exam 2

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 3 x 9 D) 27. y 4 D) -8x 3 y 6.

PHYSICS MIDTERM REVIEW PACKET

What part has zero acceleration? Where is the object stationary? Is there a region of constant acceleration?

Announcements. If you think there was an error in the scoring, fill out a regrade form and had back to ME (not TAs)

Physics 201, Midterm Exam 1, Fall Answer Key

Score on each problem:

Intermediate Algebra Final Exam Review

Math 103 Intermediate Algebra Final Exam Review Practice Problems

PHYSICS 221 SPRING EXAM 1: February 20, 2014; 8:15pm 10:15pm

IB Physics HL Year 1 First Assignment

Antiderivatives. Definition A function, F, is said to be an antiderivative of a function, f, on an interval, I, if. F x f x for all x I.

Problem: Projectile (CM-1998) Justify your answer: Problem: Projectile (CM-1998) 5 10 m/s 3. Show your work: 3 m/s 2

Transcription:

Matlab Sheet 4 Conditional Statements and Loops 1. It is desired to compute the sum of the first 10 terms of the series 14k 20k 2 + 5k. k = 1,2,, Write and run the program to calculate the sum. 2. Create Find the results of the following operations by hand and use MATLAB to check your results. a. z = 6 > + 8 b. z = 6 + > 8 c. z = 4 > (2 + 9) d. z = (4 < 7) + e. z = 4 < 7 + f. z = (4 < 7) 5 g. z = 4 < (7 5) h. z = 2 5 >= 5. Given: a = 2, b =, c = 5. Evaluate the following expressions without using MATLAB. Check the answers with MATLAB. (a) y = a b > a c < b (b) y = 4 < a < 0 (c) y = a c < = b > a + c 4. For the arrays x and y given below, use MATLAB to find all the elements in x that are greater than the corresponding elements in y. x = [ 0 0 2 6 8] y = [ 5 2 0 4 10] 5. The arrays price_a and price_b given below contain the price in dollars of two stocks over 10 days. Use MATLAB to determine how many days the price of stock A was above the price of stock B. price_a = [19 18 22 21 25 19 17 21 27 29] price_b = [22 17 20 19 24 18 16 25 28 27] Dr./ Ahmed Nagib Elmekawy 1 of 7 Matlab Sheet 4

6. Suppose that x = [-, 0, 0, 2, 5, 8] and y = [-5, -2, 0,, 4, 10]. Find the results of the following operations by hand and use MATLAB to check your results. a. z = y < ~x b. z = x & y c. z = x y 7. Evaluate the following expressions without using MATLAB. Check the answers with MATLAB a) -& b) ~5<4&~0> - c) -2&2> 8/ 8. Create The height and speed of a projectile (such as a thrown ball) launched with a speed of at an angle A to the horizontal are given by h(t) = v 0 t sin A 0.5gt 2 v(t) = v 2 0 2v 0 gt sin A + g 2 t 2 where g is the acceleration due to gravity. The projectile will strike the ground when h(t) = 0, which gives the time to hit t hit = 2(v 0 /g) sin A. Suppose that A = 0, v 0 = 40 m/s, and g =9.8 m/s 2. Use the MATLAB relational and logical operators to find the times when a. The height is no less than 15 m. b. The height is no less than 15 m and the speed is simultaneously no greater than 6 m/s. c. The height is less than 5 m or the speed is greater than 5 m/s. 9. Write a program in a script file that finds the smallest even integer that is divisible by 1 and by 16 whose square root is greater than 120. Use a loop in the program. The loop should start from 1 and stop when the number is found. The program prints the message The required number is: and then prints the number. Dr./ Ahmed Nagib Elmekawy 2 of 7 Matlab Sheet 4

10. Write a program in a script file that determines the real roots of a quadratic equation. Name the file quadroots. When the file runs, it asks the user to enter the values of the constants a, b, and c. To calculate the roots of the equation the program calculates the discriminant D, given by: D = b 2 4ac If D > 0, the program displays message The equation has two roots, and the roots are displayed in the next line. If D = 0, the program displays message The equation has one root, and the root is displayed in the next line. If D < 0, the program displays message The equation has no real roots. Run the script file in the Command Window three times to obtain solutions to the following three equations: a. x 2 + 6x + = 0 b. x 2 + 4x 6 = 0 c. x 2 + 7x + 5 = 0 11. The Pythagorean theorem states that a 2 + b 2 = c 2. Write a MATLAB program in a script file that finds all the combinations of triples a, b, and c that are positive integers all smaller or equal to 50 that satisfy the Pythagorean theorem. Display the results in a three-column table in which every row corresponds to one triple. The first three rows of the table are: 4 5 5 12 1 6 8 10 12. Rewrite the following statements to use only one if statement. if x < y if z < 10 w = x y z end end Dr./ Ahmed Nagib Elmekawy of 7 Matlab Sheet 4

1. Figure 1 shows a mass-spring model of the type used to design packaging systems and vehicle suspensions, for example. The springs exert a force that is proportional to their compression, and the proportionality constant is the spring constant k. The two side springs provide additional resistance if the weight W is too heavy for the center spring. When the weight W is gently placed, it moves through a distance x before coming to rest. From statics, the weight force must balance the spring forces at this new position. Thus W = k 1 x if x < d W = k 1 x + 2k 2 (x d) if x d Figure 1. These relations can be used to generate the plot of x versus W. a. Create a function le that computes the distance x, using the input parameters W, k 1, k 2, and d. Test your function for the following two cases, using the values k 1 = 10 4 N/m; k 2 = 1.5 10 4 N/m; d = 0.1 m. W = 500 N W = 2000 N b. Use your program to plot x versus W for 0 W 000 N for the values of k 1, k 2 and d given in part a. Dr./ Ahmed Nagib Elmekawy 4 of 7 Matlab Sheet 4

14. Consider the array A. Write a program that computes the array B by computing the natural logarithm of all the elements of A whose value is no less than 1, and adding 20 to each element that is equal to or greater than 1. Do this in two ways: a. By using a for loop with conditional statements. b. By using a logical array as a mask. 15. A company has the choice of producing up to four different products with its machinery, which consists of lathes, grinders, and milling machines. The number of hours on each machine required to produce a product is given in the following table, along with the number of hours available per week on each type of machine. Assume that the company can sell everything it produces. The profit per item for each product appears in the last line of the table. 16. Create Use a while loop to determine how many terms in the series 2 k, k = 1, 2,,..., are required for the sum of the terms to exceed 2000. What is the sum for this number of terms? 17. Compute Use a loop in MATLAB to determine how long it will take to accumulate $1,000,000 in a bank account if you deposit $10,000 initially and $10,000 at the end of each year; the account pays 6 percent annual interest. Dr./ Ahmed Nagib Elmekawy 5 of 7 Matlab Sheet 4

18. One numerical method for calculating the cubic root of a number P, is in iterations. The process starts by choosing a value x 1 as a first estimate of the solution. Using this value, a second, more accurate value x 2 can be calculated with x 2 = (P 2 x 1 + 2x 1 )/, which is then used for calculating a third, still more accurate value x, and so on. The general equation for calculating the value of 2 from the value of x i is x i+1 = (P x i + 2x i )/. Write a MATLAB program that calculates the cubic root of a number. In the program use x 1 = P for the first estimate of the solution. Then, by using the general equation in a loop, calculate new, more accurate values. Stop the looping when the estimated relative error E defined by E = x i+1 x i is smaller than 0.00001. Use the program to calculate: x i a. 100 b. 5701 c. 19.5 19. The overall grade in a course is determined from the grades of 6 quizzes, midterms, and a final exam, using the following scheme: Quizzes: Quizzes are graded on a scale from 0 to 10. The grade of the lowest quiz is dropped and the average of the 5 quizzes with the higher grades constitutes 0% of the course grade. Midterms and final exam: Midterms and final exams are graded on a scale from 0 to 100. If the average of the midterm scores is higher than the score on the final exam, the average of the midterms constitutes 50% of the course grade and the grade of the final exam constitutes 20% of the course grade. If the final grade is higher than the average of the midterms, the average of the midterms constitutes 20% of the course grade and the grade of the final exam constitutes 50% of the course grade. Write a computer program in a script file that determines the course grade for a student. The program first asks the user to enter the six quiz grades (in a vector), the three midterm grades (in a vector), and the grade of the final. Then the program calculates a numerical course grade (a number between 0 and 100). Finally, the program assigns a letter grade according to the following key: A for Grade 90, B for 80 Grade < 90, C for 70 Grade < 80, D for 60 Grade < 70, and E for a grade lower than 60. Execute the program for the following cases: Dr./ Ahmed Nagib Elmekawy 6 of 7 Matlab Sheet 4

(a) Quiz grades: 6, 10, 6, 8, 7, 8. Midterm grades: 82, 95, 89. Final exam: 81. (b) Quiz grades: 9, 5, 8, 8, 7, 6. Midterm grades: 78, 82, 75. Final exam: 81. 20. Given Cam is a mechanical device that transforms rotary motion into linear motion. The shape of the disc is designed to produce a specified displacement profile. A displacement profile is a plot of the displacement of the follower as a function of the angle of rotation of the cam. The motion of a certain cam is given by the following equations: Dr./ Ahmed Nagib Elmekawy 7 of 7 Matlab Sheet 4