> Financial Mathematics |
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 10 | w = 9 - z |
10 or greater than 10, to 100 | w = square root of z |
Greater than 100 | w = z |
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 Less | 10% of the income |
Greater than 10,000 to 50,000 | 1,000 + 20% of the income |
Greater than 50,000 | 2,000 + 30% of the 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)])