Question

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

Can you rewrite this MATLAB code using a while loop instead of a for 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

Homework Answers

Answer #1

%formatting
clc, clear, format compact;

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

%while loop initialization
while 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

Output is attached below

Know the answer?
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
Similar Questions
Using for loop and if statement, write a MATLAB code to plot a graph for x(t)...
Using for loop and if statement, write a MATLAB code to plot a graph for x(t) as a function of time t in the range 0 to 12 in increment of 0.01 ?(?) = 1: 0 ≤ ? ≤ 1 2? − 1 1 ≤ ? ≤ 2 3 2 ≤ ? ≤ 3 −2.5? + 10.5 3 ≤ ? ≤ 5 −2 5 ≤ ? ≤ 6 4/3 ? − 10 6 ≤ ? ≤ 9 2 9 ≤...
Problem 3 you can use Matlab and also i give u the Problem 1 code its...
Problem 3 you can use Matlab and also i give u the Problem 1 code its on Matlab Using the same initial code fragment as in Problem 1, add code that calculates and plays y (n)=h(n)?x (n) where h(n) is the impulse response of an IIR bandpass filter with band edge frequencies 750 Hz and 850 Hz and based on a 4th order Butterworth prototype. Name your program p3.sce this is the Problem 1 code and the solutin clear; clc;...
(java) a. The following code has compilation AND logic errors. Rewrite the code (using the while...
(java) a. The following code has compilation AND logic errors. Rewrite the code (using the while loop) so it will compile correctly and print all the even numbers from 0 to 10 on one line separated by a space: int j = 0; while j > 10 ; j + 2; System.println(j, “ “); b.You want to print the values of integers a and b to an external file “test123.txt”, assigned to the PrintWriter variable output. Correct the following statements:...
USING MATLAB Code for pseudo Random Input N M=0 For i=1 to N X=rand# Y=rand# If...
USING MATLAB Code for pseudo Random Input N M=0 For i=1 to N X=rand# Y=rand# If X^2+ y^2 <= 1, then M=M+1 End the loop Print M/N (This is the probability) Print M/N (this is approximately = Py
This is the code I am working on for a project using MATLAB: clc,clear G=27.7*10.^9; b=0.286*10.^-9;...
This is the code I am working on for a project using MATLAB: clc,clear G=27.7*10.^9; b=0.286*10.^-9; v=0.334; x=(-5*10.^-9:0.1:5*10.^-9); y=(-5*10.^-9:0.1:-1*10.^-9); [xx,yy]=meshgrid(x,y); stress=(-G*b)*yy*(3^xx+yy^2)/(2*pi.*(1-v))*(xx^2+yy^2).^2; figure mesh(xx,yy,stress); grid on xlabel('X') ylabel('Y') zlabel('Stress') Whenever I try to run this I keep getting the "Error using mesh" line, and then an error for "mesh(xx,yy,stress)". I know I am not catching the issue but how do I fix this? If you could explain as well that be extremely helpful!
Code in Matlab. I need to make a diamond shape with * I get part of...
Code in Matlab. I need to make a diamond shape with * I get part of it with the code below. Instructions are: " Given the value of N print diamond of N + (N-1) rows. For example, if N = 5 it should print a diamond." clc clear n = input ('number of rows \n') o = input ('number of inverted rows \n') t = (o-1) for i = 1:n for k = 1:n-i fprintf (' '); end for...
using matlab please present code for the following (ALL PARTS PLEASE AND THANK YOU) 1. No...
using matlab please present code for the following (ALL PARTS PLEASE AND THANK YOU) 1. No Input/No Output Write a function that has no input and no outputs. This function will simply display some text when it is called. (this is my code, are there suggestions for improvements?) function Display() disp('some text') end 2. 1 Input/No Outputs Write a function with one input and no outputs. This function will simply display a variable that is provided as an input argument....
This is LU deposition code of matlab. What's wrong with this code.... Thank you n and...
This is LU deposition code of matlab. What's wrong with this code.... Thank you n and B was given from above, what is for i = 1:n U(1,i) = A(1,i); L(i,1) = A(i,1)/U(1,1); %u(1,1)=1 end for j = 2:n for k = j:n U(j,k) = A(j,k)-L(j,1:j-1)*U(1:j-1,k); L(k,j) = (A(k,j)-L(k,1:j-1)*U(1:j-1,j))/U(j,j); end end for i = 1:n x(i) = B(i) / L(i,i:n)*U(i:n,i); end fprintf("Ans x = \n"); fprintf("%f \n", x);
Why it cannot display flop? you need to explain rather than just show me another code...
Why it cannot display flop? you need to explain rather than just show me another code MatLab function b=Matrixvector(A,x) n=length(x); b=zeros(n,1); flop=0; for i=1:n for j=1:n b(i)=b(i)+A(i,j)*x(j); flop=flop+2; end end disp(b); disp(flop);
Problem 1....... you can use Matlab The following Scilab code generates a 10-second “chirp” with discrete...
Problem 1....... you can use Matlab The following Scilab code generates a 10-second “chirp” with discrete frequencies ranging from 0 to 0.2 with a sampling frequency of 8 kHz. clear; Fs = 8000; Nbits = 16; tMax = 10; N = Fs*tMax+1; f = linspace(0.0,0.2,N); x = zeros(f); phi = 0; for n=0:N-1 x(n+1) = 0.8*sin(phi); phi = phi+2*%pi*f(n+1); end sound(x,Fs,Nbits); sleep(10000); //allows full sound to play Add code that calculates and plays y (n)=h(n)?x (n) where h(n) is the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT