Question

MATLAB You've recently looked at 'while' loops in Matlab. Break statements can be contained within 'while'...

MATLAB

You've recently looked at 'while' loops in Matlab. Break statements can be contained within 'while' loops to exit the loop early. Look at the following example code:

x=-10;

while x<0

      x=x+2;

     if x == -2

            break;

      end

end

Without the break statement, the while loop would usually add 2 to x=-10 until x=0, and then produce a final answer of x=0 at the end of the loop. Now, the loop ends prematurely when the value of -2 is given to x.

Following this example. come up with a code while loop that calculates multiple powers of 2, i.e. (2^1, 2^2, 2^3...). Your while loop should calculate up to 2^30

Provide a break statement that stops the code at the value 1048576, which is equal to 2^20  

To complete this quest, answer the following questions in the submission box below.

  • Upload a word document of your code that contains a while statement calculating 2^30, with a break statement that exits the while loop at 2^20.
  • Keep in mind that the screenshot must also include a timestamp showing when you completed the quest.

(For more information see the "Engineering Tools - MATLAB" chapter from Pathfinder)

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

Code:

x = 2;

while x ~= 2^30

    x = x*2;
    if x == 2^20
        break;

    end
end
 %to verify our code
disp(x);

Output:

1048576

Add a comment
Know the answer?
Add Answer to:
MATLAB You've recently looked at 'while' loops in Matlab. Break statements can be contained within 'while'...
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
  • MATLAB ONLY!! PLEASE WRITE IN COMPUTER SO I CAN COPY PASTE!!! ANSWER COMPLETELY, USE FOR LOOPS....

    MATLAB ONLY!! PLEASE WRITE IN COMPUTER SO I CAN COPY PASTE!!! ANSWER COMPLETELY, USE FOR LOOPS. THE PROGRAM TO BE MODIFIED IS THE NEXT ONE: clc clear % Approximate the value of e ^ x using the Taylor Series x = input( 'Enter the value of x for e^x. x = ' ); t = input( 'Enter the amount of desired terms. t = ' ); i = 1; e_taylor = 0; % initializing the variable for the accumulator while...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • Matlab Question: Important! Read: No for or while loops may be used; must use the disp...

    Matlab Question: Important! Read: No for or while loops may be used; must use the disp function The top speeds of sportscars, given in miles per hour, are: 155 mph BMW M5 217 mph Lamborghini Aventador Spyder 205 mph Ferrari 488 205 mph Nissan GTR 197 mph Chevrolet Corvette Stingray ZR1 258 mph Bugatti Veyron Supersport 195 mph Dodge Viper 270 mph Hennessey Venom 155 mph BMW M3 195 mph Mercedes SL Write a function selectCars to identify cars with...

  • Creates MATLAB program that acts like an user-machine interface which will help the user to know...

    Creates MATLAB program that acts like an user-machine interface which will help the user to know about any updates about COVID-19 only in the particular states in the USA including the areas affected and the number of registered positive cases along with number of deaths. State = ["WASHINGTON", "CALIFORNIA", "VIRGINIA", "TEXAS", "MARYLAND", "COLORADO", "OKLAHOMA", "KANSAS", "LOUISIANA", "INDIANA", "PENNSYLVANIA", "NEW JERSEY", "NEW YORK", "MAINE", "NEW MEXICO", "ARIZONA"]; Cases = [1376, 1030, 105, 307, 107, 277, 46, 35, 392, 60, 206, 742,...

  • matlap Question 1 1. Consider the following Matlab program 5 - (2 < 3) & (C2...

    matlap Question 1 1. Consider the following Matlab program 5 - (2 < 3) & (C2 > 3) 1 (1 - 0)) What is the final value of s? A. True B. *1 CO D. false 2- Which expression tests whether variable x is between (but not the same as) the values 5 and 10 (a) 5 <x< 10 (b) 5 <= x <= 10 (c) 5 < X & X > 10 (d) x < 10 & 5 <...

  • Matlab-How can i exit the for loop when counts exceeds the max value, then continue to...

    Matlab-How can i exit the for loop when counts exceeds the max value, then continue to the last loop and plot accepted values. counts = 0; for k = 2:length(partDiameter) overlaps = 1; while overlaps == 1 counts = counts + 1; if counts >= 10000 disp('Max particles packed') inCoord = [inCoord;[x,y]]; return; end x = (partDiameter(k)-L)*rand(1,1) + (L-partDiameter(k)/2); y = (partDiameter(k)-L)*rand(1,1) + (L-partDiameter(k)/2); % check if this x,y postion overlaps with previously packed % partices for m = 1:length(inCoord(:,1))...

  • 5.1.2 Open Methods - Newton-Raphson Method Xi+1= xi – FOTO Matlab Code Example:4 function mynewtraph (f,...

    5.1.2 Open Methods - Newton-Raphson Method Xi+1= xi – FOTO Matlab Code Example:4 function mynewtraph (f, f1,x0,n) Xx0; for ilin x = x - f(x)/f1(x); disp (li if f(x) <0.01 f(x))) break end end end Matlab Code from Chapra function [root, ea, iter)=newtraph (func,dfunc, xr, es,maxit,varargin) newtraph: Newton-Raphson root location zeroes 8 [root, ea, iter)-newtraph (func, dfunc, xr, es,maxit,pl,p2, ...): $uses Newton-Raphson method to find the root of fune input: func- name of function 8dfunc = name of derivative of...

  • Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the...

    Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to display a warning if the number is less than 2 or greater than 10 and prompt the user to enter the data again until they enter a valid number Put a...

  • How can I get my while loop to run until my condition in my if statement...

    How can I get my while loop to run until my condition in my if statement is met? (In MATLAB) clc%clears screen clear all%clears history close all%closes all files format long %Euler's Method %Initial conditions and setup %Projectile motion x=3;%randi(3); y=3;%randi(3); figure('color','white'); plot(x,y,'bo'); hold on wind = (-10 + 20*rand(1)); fprintf('The wind speed is %2.2i\n',wind); v0= input('Initial velocity(m/s): '); a= input('Angle of projection(in degrees): '); a=a*pi/180; h=.01; cd=.1; %cdy=.1; %cdx=(rand*2-1)/2; %cdy=(rand*2-1)/2; m=1.5; g=9.8; %acceleration due to gravity in m/s^2 %td=2*v0*sin(a)/g;...

  • Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script...

    Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script that evaluates this expression, ignoring all terms that are strictly smaller than .000001. Your script should display the number of terms summed and the sum. Use a while-loop. Think about the initialization of your variables and the order of computation carefully! In the loop body you need to calculate the value of a term only once. We use the above series for approximating (π^2)/8...

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
ADVERTISEMENT