MATLAB Basics


Basic commands and functions


Using the Command Window

>> principal = 2500

principal =

        2500

>> interest = 0.0125, years = 5

interest =

    0.0125


years =

     5

>> principal * (1 + interest) ^ years

ans =

   2.6602e+03

>> future = principal * (1 + interest) ^ years;
>> disp(future)
   2.6602e+03

>> disp(['$', num2str(future)]);
$2660.2054





>> format bank
>> disp(future)
       2660.21

>> format long
>> disp(future)
     2.660205384063720e+03

>> format long g
>> disp(future)
          2660.20538406372
     
   
                     

Scripts

Creating: in simpleScript.m

% Example script
% Calculates the future value of a principal amount of money earning a
% given interest rate (compounded annually) for a given number of years

% initialize variables
principal = 2500;
interest = 0.0125; % interest rate is 1.25%
years = 5;

future = principal * (1 + interest) ^ years;
disp(['Investing $', num2str(principal), ' for ', num2str(years),  ...
      ' years at an annual interest rate of ', num2str(interest * 100), ...
      '% yields $', num2str(future)]);

Running: in the Command Window

>> simpleScript
Investing $2500 for 5 years at an annual interest rate of 1.25% yields $2660.2054
>>

Doing Mathematical Calculations

Arithmetic operators

+   -   *   /   ^   \

Built-in functions