Matlab and Boolean Expressions In a previous lab this semester we saw how C++ ha
ID: 3621590 • Letter: M
Question
Matlab and Boolean ExpressionsIn a previous lab this semester we saw how C++ handles boolean expressions on a single variable. Now we want to look at Matlab and see how dealing with booleans and vectors can result in a powerful programming tool.
Note that all of the logical expressions that were allowed in C++ are allowed in Matlab, examples include <, >, >=, ~= (not equal). But what happens if we do a < 10 when a is a vector (or matrix) of information. It turns out that the result is then a vector of boolean results. So if a has ten items in its vector, the result then has 10 items in its vector, where each location is the true or false for the particular condition.
We can then tell matlab to only operate on those areas of the vector where a particular condition was true or false. If we had the following:
if (a < 10) then
b = b + 1
else
b = b + 10
We could implement this in matlab as:
x = a < 10
and then operate on b using that information
b = b .+ 1;
b(x) = b(x) .+ 9;
(why +9 and not + 10?????)
(Note the difference between the two expressions....)
A few other issues
Matlab can display text (using the disp('string') command, refer to lab 1), and it gets input (with the input command).
So disp('some text') will display some text, while value=input('Enter a value') will display Enter a value and then read in whatever the user types.
Lab Assignment
Due: By your Lab Time during the week of 11/8).
Create a Matlab script file that will calculate the income taxes owed for a vector of salaries based on the following table.
Income Range Income Tax Amount
$0 - $2,000 Nothing
$2,001 - $20,000 4% * Each dollar earned over $2,000
$20,001 - $40,000 $720 + 8% * Each dollar earned over $20,000
$40,001 - $80,000 $2,320 + 16% * Each dollar earned over $40,000
$80,001 - $120,000 $8,720 + 24% * Each dollar earned over $80,000
$120,001 + $18,320 + 32% * Each dollar earned over $120,000
Your program should ask the user for 10 salaries and then calculate and output the amount of income tax owed for each individual salary. Be sure to output your information in some readable style.