While: Incorrect Use of ' ' Operator. To Assign A Value To A Variable, Use ' '. To Compare Values For Equality, Use ' '
While: Incorrect Use of ' ' Operator. To Assign A Value To A Variable, Use ' '. To Compare Values For Equality, Use ' '
Do the following problems. Save you answer in word document and attached to EXERCISE 6A in
Blackboard.
1. Type the below for-loop into MATLAB. How many times will the starred line of the previous
example be executed? What are starting point, increment (step) and ending point of this loop?
Start: 1
Step: 1
End: 8
Total number of loops: 8
Calculate the sum S of elements a i =√2i-1, i=1, 2, ..., until the sum will exceed 20. Type in the
following code and examine the output.
S = 0; % Initial assignment for sum to be able to check condition
i = 1; % First value for i is 1
while S < 20 % Condition to check
S = S + sqrt(2*i-1); %Recurrent formula for S
i = i + 1; % Next i
end
number_of_loops = i - 1 %Do you know why i-1?
S % Shows the final value
Command Window:
number_of_loops =
8
S =
21.4090
2. What is wrong with the following code? Type it in MATLAB to check the error message.
% This code assigns the value of x to zero
x = 4;
if x = 0
zeroValue = x
end
MATLAB tells us that the “=” sign is incorrectly used because it is only used to assign a value
to a variable, and that the conditional statement intended to compare values instead. To
correct this mistake, use double equals sign “==”.
Command Window:
zeroValue =
4
The command window will produce the value for zeroValue, which is 4. This was possible due
to the presence of the “or” logical operator, which checks if either one of the statements is true.
3. Verify that the code below is correct by checking with a positive, negative and zero value of x.
It will be faster to run the code from a script M-file. Why don’t we need to check if x is actually
zero when assigning it to zeroValue?
% If x is negative, assign value of x to negativeValue
% If x is positive, assign value of x to positiveValue
% If x is zero, assign value of x to zeroValue
x = 4;
if x < 0
negativeValue = x
elseif x > 0
positiveValue = x
else
zeroValue = x
end
Command Window:
positiveValue =
4
Script file:
maxValue = 10;
for num = 1:maxValue % **increment num from 1 to maxValue**
num % This line simply prints num
square = num^2 % This line prints the square
end
Command Window:
num =
1
square =
1
num =
2
square =
4
num =
3
square =
9
num =
4
square =
16
num =
5
square =
25
num =
6
square =
36
num =
7
square =
49
num =
8
square =
64
num =
9
square =
81
num =
10
square =
100
Script file:
maxValue = 10;
num = 1
while num <= maxValue % **num is not bigger than maxValue**
num % This line simply prints num
square = num^2 % This line prints the square
num = num + 1;
end
Command Window:
num =
1
square =
1
num =
2
square =
4
num =
3
square =
9
num =
4
square =
16
num =
5
square =
25
num =
6
square =
36
num =
7
square =
49
num =
8
square =
64
num =
9
square =
81
num =
10
square =
100
6. Question:
% Sam gets paid compound interest at a rate defined at 5% per annum.
% Calculate his resulting investment each year and after 10 years.
% Change the rate on the 8th year to 5.75%
rate = 0.05;
investment = 5000;
for year = % **increment the year from 1 to 10**
if % **year is 8**
rate = 0.0575
end
year %this line simply prints the year
investment = (1+rate)*investment % compound function
end
Script file:
% Sam gets paid compound interest at a rate defined at 5% per annum.
% Calculate his resulting investment each year and after 10 years.
% Change the rate on the 8th year to 5.75%
rate = 0.05;
investment = 5000;
for year = 1:10 % **increment the year from 1 to 10**
if year == 8 % **year is 8**
rate = 0.0575
end
year %this line simply prints the year
investment = (1+rate)*investment % compound function
end
Command Window:
year =
1
investment =
5250
year =
2
investment =
5.5125e+03
year =
3
investment =
5.7881e+03
year =
4
investment =
6.0775e+03
year =
5
investment =
6.3814e+03
year =
6
investment =
6.7005e+03
year =
7
investment =
7.0355e+03
rate =
0.0575
year =
8
investment =
7.4400e+03
year =
9
investment =
7.8678e+03
year =
10
investment =
8.3202e+03