PREDICT THE OUTPUT FROM EACH OF THE FOLLOWING MATLAB PROGRAMS In each of the bel
ID: 3699913 • Letter: P
Question
PREDICT THE OUTPUT FROM EACH OF THE FOLLOWING MATLAB PROGRAMS In each of the below syntax exercises, determine the output that results from running the specified MATLAB program. If there is no output because an error or an infinite loop results, write ERROR" or "INFINITE LOOP" (whichever causes the problem) and then explain WHY the error or infinite loop occurs. If no error or infinite loop occurs, then write the output that results from running the program. You will need to show your work, meaning, demonstrate how you arrived at what you claim is the output from these programs. Remember: statements terminated with a semi-ccon do not produce output! Also... it will be beneficial to READ THE PROBLEM FIRST, to understand what's going on, rather than just jump right in. Finally... DO NOT USE MATLAB to work out the answers to these exercises!Explanation / Answer
Let us understand each of the program line by line:
program 1(example 2)
first line clears the variables and screen
2nd and 3rd lines assign value 1 and -1 to a and b respectively
4th line assign an array [7 5 3 1] to the c
5th line set the value of counter to 3
6th line start a loop that will run untill the product of a and b is less then c(counter) i.e value at index counter of the c array
1 iteration: a=1, b=-1, c(counter) = c(3) = 3 ----> 1*-1 <3, loop will run
2 iteration: a=2, b=-2, c(counter) = c(2) = 5 ----> 2*-2 <5, loop will run
3 iteration: a=3, b=-3, c(counter) = c(1) = 3 ----> 3*-3 <7, loop will run
4 iteration: a=4, b=-4, c(counter) = c(0) ==>error, c(0) is not a valid index access
So, first program will produce an error
Program 2(example 3)
first line clears the variables and screen
2nd, 3rd and 4th lines assign value 2 and -2 and 5 to a, b and counter respectively
5th line assign 1 to n
6th line start a loop that will run untill the product of a and b is less or equal to then counter
1st iteration: a=2, b=-2, counter = 5 ----> 2*-2 <5, loop will run
2nd iteration: a=2, b=-2, counter = 11 ----> 2*-2 <11, loop will run
3rd iteration: a=-2, b=2, counter = 23 ----> -2*2 <23, loop will run
it will continue for infinity as value of a will swap in every iteration and hence product will always remain -4 and value of counter will increase in every iteration. So, loop conditio will always be true. Hence the program will fall in infinite loop
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.