Matlab → Basics → Variables → Built-In Functions
Suppose we want to find the square root of 16. It could easily be done in Matlab using the command >> sqrt(16)
. It shows the answer 4 when you press the Enter key. For example,
Command Window
>> sqrt(16)
ans =
4
Matlab has several useful built-in functions. A few of them are listed in the following Table.
Mathematics | Command | Answer |
>> sqrt(81) |
9 9 |
|
>> exp(5) >> exp(-6) |
148.4132 0.0025 |
|
>> abs(-10) |
10 | |
>> factorial(4) >> factorial(5) |
24 (4×3×2×1=24) 120 (5×4×3×2×1=120) |
|
>> log(1000) >> log10(1000) |
6.9078 3 |
|
>> pi |
3.1416 | |
>> sin(pi/6) >> sind(30) |
0.5000 0.5000 |
|
What is the remainder when 33 is divided by 5? What is the remainder when 134 is divided by 9? |
>> rem(33,5) >> rem(134,9) |
3 4 |
Example Suppose $2,000 is invested at a rate of 8% per year compounded continuously. How much will be the amount after:
(a) 4.2 years
(b) 5 years and 2 months
Solution:
(a) When , the Matlab code is
>> P=2000; r=0.08; t=4.2;
>> A=P*exp(r*t)
A =
2798.678
(b) When (because 5 years and 2 months = ), the Matlab code is
>> P=2000; r=0.08; t=5.1667;
>> A=P*exp(r*t)
A =
3023.706