Matlab → Basics → Variables in Matlab
Like all programming languages, we can declare variables in Matlab and can assign values to them. When a variable is given a value, that value is actually placed in the memory space assigned to the variable. Matlab shows all the declared variables and their values in the right column. For example, assign value 5 to variable x, 7 to variable y, and 12 to variable z and press Enter.
Figure: Assigning Values to Variables in Matlab
Now, suppose we have to find the value of when where , and . We can find it in the following way.
We can also write the above code in the following way.
>> x=20; y=11; z=-3;
Notice that if a semicolon is typed at the end of the command and Enter key is pressed, the Matlab stored the variable in the memory but does not display it. Besides, it is a good practice to declare all the variables on the same line.
Example Suppose $1,000 is deposited in a savings account that earns compounded interest at a rate of 10% per year. How much will be in the account after 2 years if interest is compounded:
(a) When interest is compounded monthly, the Matlab code is
>> P=1000; r=0.1; t=2; n=12;
(b) When interest is compounded weekly, the Matlab code is
>> P=1000; r=0.1; t=2; n=52;
NOTE: Matlab is case-sensitive, that means a≠A. Consider the following example,
>> a=10; A=15; b=20; B=25;