Question

For each of the below codes, determine if the following MATLAB codes produces an error. If...

For each of the below codes, determine if the following MATLAB codes produces an error.

  • If the code does NOT produce an error, predict the output.
  • If the code DOES produce an error: (1) state what line the FIRST error is found and (2) what the error is

clear;clc
%ICE08B-1
A = [1,5,2,9,4,9];
B(1,5) = 0;
counter = 1;
for i = 1:length(A)
    B(i) = A(i) * counter;
    B(i) = B(i)-2;
    counter = counter + 1;
end

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the solution:

The above code does not give the error and simply executed with modified value using the array A value.

for i = 1:length(A)

The for loop runs the array A length times that is A array length is 6

for the very first time in loop

B(i) = A(i) * counter; %i value B(i) is 1*1 = 1
B(i) = B(i)-2; %i value B(i) is 1-2 = -1
counter = counter + 1; %increment the counter

second time in loop

B(i) = A(i) * counter; %i value B(i) = 10
B(i) = B(i)-2; %i value B(i) = 8
counter = counter + 1; %increment the counter

third time in loop

B(i) = A(i) * counter; %i value B(i) = 6
B(i) = B(i)-2; %i value B(i) = 4
counter = counter + 1; %increment the counter

fourth time in loop

B(i) = A(i) * counter; %i value B(i) = 36
B(i) = B(i)-2; %i value B(i) = 34
counter = counter + 1; %increment the counter

fifth time in loop

B(i) = A(i) * counter; %i value B(i) = 20
B(i) = B(i)-2; %i value B(i) = 18
counter = counter + 1; %increment the counter

sixth time in loop

B(i) = A(i) * counter; %i value B(i) = 54
B(i) = B(i)-2; %i value B(i) = 52
counter = counter + 1; %increment the counter

The final B array value is :

-1
8
4
34
18
52

That is output of the B array

Add a comment
Know the answer?
Add Answer to:
For each of the below codes, determine if the following MATLAB codes produces an error. If...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For each of the below codes, determine if the following MATLAB codes produces an error. •...

    For each of the below codes, determine if the following MATLAB codes produces an error. • If the code does NOT produce an error, predict the output. • If the code DOES produce an error: (1) state what line the FIRST error is found and what the error is ICE08B-1: clear;clc SICE08B-1 A = [1,5,2,9,4,9]; B(1,5) = 0; counter = 1; for i = 1:length(A) B(1) = A(i) * counter; B(1) = B(i)-2; counter = counter + 1; end ICE08B-2:...

  • Exercise 11 (2 pts.): When the following Matlab program is run, it produces an error. WHERE...

    Exercise 11 (2 pts.): When the following Matlab program is run, it produces an error. WHERE in the Matlab program does the error occur (underline the specific line of Matlab code that produces the error), and WHY does it occur? cleariclc A[-1, 0, -2, 3, 4, 61 Exercise 12 (2 pts.): When the following Matlab program is run, it produces an error. WHERE in the Matlab program does the error occur (underline the specific line of Matlab code that produces...

  • In each of the below syntax exercises, determine the output that results from running the specified...

    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 results, write “ERROR” and then explain why the error occurs. clear; clc a = 3; b = 2; c = 1; for m = 1:3 for n = 1:3 if n < m      a = b - c elseif n == m b = a + 1; else c = 2*b + a; end end...

  • I want to see the work written out because I do not understand how a for...

    I want to see the work written out because I do not understand how a for loop works without running it on MatLab Exercise 13 (2 pts.): Predict the output that appears in Matlab's command window when the following Matlab program is run (REMEMBER to SHOW SOME WORK!) clear;clc a = 3; c=2; d = zeros (1,5) -2; for m - 1:5 x = (m-1)"c; y = (-1)^n; z=x-y; d(m) = z + 1; end

  • Identify and correct the error in the following codes, and state what type of error you...

    Identify and correct the error in the following codes, and state what type of error you will get? public class Assignmet2{      public static void main(String []args){        int x=2;        for(int year=1; year>=0; year--)          for (int day=2; day!=3; year++)            {if ( x>=1) System.out.println("Yes");            else System.out.println("No");            x--;} }} int[] grade = new int[20]; for(int counter=0; counter<=grade.length; i++) grade[counter]=20;

  • Write the output of the two codes below, unless there is a compiler error or a...

    Write the output of the two codes below, unless there is a compiler error or a runtime error. In those cases, write ​CE​ or ​RE​, respectively, and explain why those errors occur. If the code produces an infinite loop, write “​LE:​ ​infinite loop​”. public class ForLoopsDemo{ public static void main(String[] args){ int x = 4; fint x = 4; for( int i = 0; i < x; i++ ){ } } for( int j = 0; j < 2*i+1; j++)}...

  • Complete the Logic section of the below MATLAB code to detect the pattern shown where a...

    Complete the Logic section of the below MATLAB code to detect the pattern shown where a single pixel/element is EITHER RED or GREEN question you will need to fill in the two upper limits of the for loops and write out the IF statement RED or GREEN CDS-130 Final Exam clear: clc counter = 0; Lor X = 1: 15 for y - 1:15 A (x,y) = randi (4)-1; end end mycolormap = [ 1; 1 ; 10 0; 1...

  • For each of the following Matlab codes, fill in each cell of the empty template with TWO VALUES: ...

    For each of the following Matlab codes, fill in each cell of the empty template with TWO VALUES: (1) A NUMERICAL VALUE that appears in that cell of the matrix; and (2) A WORD identifying the COLOR that appears in that cell, as a result of the matrix being interpreted as an image using Matlab's IMAGESC command. Examples: "Red", "Green", "Cyan", etc. DO NOT PUT THE MATLAB RGB COLOR CODE IN THE CELL! That is, instead of the word "Red",...

  • All I need help with is the matlab codes for B) and C) Photos of answers are below. Rate will be ...

    All I need help with is the matlab codes for B) and C) Photos of answers are below. Rate will be given for correct matlab code! DP6.3 A unity negative feedback system with K(s + 2) has two parameters to be selected. (a) Determine and plot the regions of stability for this system. (b) Select r and K so that the steady-state error to a ramp input is less than or equal to 25% of the input magnitude. (c) Determine...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT