Question

In: Mechanical Engineering

Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...

Can you rewrite this MATLAB code using a for loop instead of a while loop?

%formatting
clc, clear, format compact;

%define variables
k=1;
b=-2;
x=-1;
y=-2;

%while loop initialization
for k <= 3
disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]);
y = x^2 -3;
if y< b
b = y;
end
x = x+1;
k = k+1;
end

Solutions

Expert Solution


Related Solutions

(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
How to rewrite the bin() function below using recursion instead of a for loop in C?...
How to rewrite the bin() function below using recursion instead of a for loop in C? #include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdint.h> char* bin(int x, int i); int main(int argc, char **argv) { char* binstr = bin(10, 0); printf("%s\n", binstr); free(binstr); return 0; } char* bin(int x, int i){ int bits = log10((double) x)/log10(2)+1; char* ret = malloc((bits+1)*sizeof(char)); for(int i = 0; i < bits; i++){ ret[bits-i-1] = (x & 1) ? '1' : '0'; x >>= 1;...
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
Matlab question: By using while loop to solve, Sum up bottles until you are over 75....
Matlab question: By using while loop to solve, Sum up bottles until you are over 75. How many days/loops are needed? Print out cups on the shelf and total until you reach at least 75. Finish the code below, least_num_Cups = randi([75, 300], 1,1); while totalCups = % Change this code to reflect change in totalCups fprintf('%0.0f cups of tea on the shelf, total %0.0f\n',); k = ; % Change code to update k end k_complete = ; % Change...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Rewrite the attached code program using read, write, open and close (System I/O functions) instead of...
Rewrite the attached code program using read, write, open and close (System I/O functions) instead of the standard I/O functions. Also please write comments explaining each line of code clearly and concisely please. #include #include int main(int argc, char *argv[]) { FILE *fd;    char c;    if(argc==1)    fd=stdin;    else         if((fd = fopen(argv[1], "r"))==NULL){        fprintf(stderr, "Error opening %s, exiting\n", argv[1]);            exit(0);    }    while( (c=getc(fd)) != EOF)    putc(c, stdout);   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT