0% found this document useful (0 votes)
106 views

While: Incorrect Use of ' ' Operator. To Assign A Value To A Variable, Use ' '. To Compare Values For Equality, Use ' '

The document contains 6 questions about using MATLAB for numerical computation and modeling. It includes examples of using for loops and while loops to iterate through calculations. Key points covered are: 1) Using for loops to calculate a sum and print squares from 1 to a maximum value. 2) Examples of if/elseif/else conditional statements to assign values based on conditions. 3) A compound interest calculation using a for loop and conditional to change the interest rate in one year.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

While: Incorrect Use of ' ' Operator. To Assign A Value To A Variable, Use ' '. To Compare Values For Equality, Use ' '

The document contains 6 questions about using MATLAB for numerical computation and modeling. It includes examples of using for loops and while loops to iterate through calculations. Key points covered are: 1) Using for loops to calculate a sum and print squares from 1 to a maximum value. 2) Examples of if/elseif/else conditional statements to assign values based on conditions. 3) A compound interest calculation using a for loop and conditional to change the interest rate in one year.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Laforteza, John Vincent B.

CS10-8L – A32 Exercise 6A

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 will print an error:


Incorrect use of '=' operator. To assign a value to a
variable, use '='. To compare values for equality, use '=='.

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 “==”.

> What happens when x equals to zero or x is not zero?


% This code contains the modified version of the if-statement
x = 4;
if x == 0 || x ~= 0
zeroValue = x
end

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

4. Use a for-loop to print out the square of integers from 1 up to %maxValue.


maxValue = 10;
for num = % **increment num from 1 to maxValue**
num % This line simply prints num
square = num^2 % This line prints the square
end

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

5. Use a while-loop to print out the square of integers from 1 up to maxValue.


maxValue = 10;
num = 1
while % **num is not bigger than maxValue**
num % This line simply prints num
square = num^2 % This line prints the square
num = num + 1;
end

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

You might also like