> Financial Mathematics
>> MATLAB Programming
>>> If/else programs

If/else Programs

Consider the following basic Matlab if/else programs.

Q. Write a program in Matlab Script Editor that takes the value of 'z' from the user, and calculate the value of 'w' according to the following given conditions.

Value of z

Value of w

Less than 5 w = 2*z
5 or greater than 5, and Less than 10w = 9 - z
10 or greater than 10, to 100w = square root of z
Greater than 100 w = z

z = input ('Please insert a number: ');
if z < 5
w = 2*z;
elseif z >= 5 && z < 10
w = 9-z;
elseif z >= 10 && z <= 100
w=sqrt(z);
else
w=z;
end
disp (['The value of w is ',num2str(w)])



Q. Write a program in Matlab Script Editor that takes the value of 'income' from the user, and calculate the payable 'tax' according to the following given conditions.

Income

Payable Tax

10,000 or Less10% of the income
Greater than 10,000 to 50,0001,000 + 20% of the income
Greater than 50,0002,000 + 30% of the income

income = input ('Please insert your income: ');
if income <= 10000
tax = 0.1*income;
elseif income >10000 && income <= 50000
tax = 1000 + 0.2*income;
else
tax = 2000 + 0.3*income;
end
disp(['Your tax is: ',num2str(tax)])

>> Next: Matlab if/else programs