Experiment No4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Experiment No.

Variable, Format and Saving


Like most other programming languages, MATLAB provides
mathematical expressions, but unlike most programming
languages, these expressions involve entire matrices. The
building blocks of expressions are:

1- Variable 2- Numbers 3- Operators 4- Functions

3.1 Variable:

MATLAB does not require any type declarations or dimension


statements. When MATLAB encounters a new variable name, it
automatically creates the variable and allocates the appropriate
amount of storage. If the variable already exists, MATLAB changes
its contents and, if necessary, allocates new storage. For
example,

num_students = 25

creates a 1-by-1 matrix named num_students and stores the


value 25 in its single element.

Variable names consist of a letter, followed by any number of


letters, digits, or underscores. MATLAB uses only the first 31
characters of a variable name. MATLAB is case sensitive; it
distinguishes between uppercase and owercase letters. A and a
are not the same variable.

I have forgotten the Variables!


v The who command displays all the variable names you have
used.

3.2 Format:

v By default, MATLAB displays numbers with four decimal


place values. This is known as short format.
v However, if you want more precision, you need to use the
format command.
v The format long command displays 15 digits after decimal.
v The format bank command rounds numbers to two decimal places.
v MATLAB displays large numbers using exponential notation.
v The format short e command allows displaying in exponential form with
four decimal places plus the exponent.
v The format long e command allows displaying in exponential form with 15
decimal places plus the exponent.
v The format rat command gives the closest rational expression resulting
from a calculation

3.3 Arithmetic Operators:

Table (1.1) shows a list of commands used to solve indicated


mathematical equations (a, b, x and y are numbers).
Table (1.1) : Translation of mathematical notation to MATLAB
command

3.4 Saving

v The save command is used for saving all the variables in the workspace, as
a file with .mat extension, in the current directory.
v For example, save myfile
v You can reload the file anytime later using the load command.
v load myfile

3.5 Procedure:

A. Apply the following expression and write the output:

1.

format long

x =7+10/3+5^1.2

2.
format short

x =7+10/3+5^1.2

3.

format bank

daily_wage =177.45;

weekly_wage = daily_wage *6

4.

format short e

4.678*4.9

5.

format long e

x = pi

6.

format rat

4.678*4.9

7.

𝑥 = 1+5

8. Save your application above under Exp4.mat

3.6 Discussion:
1. What is the Semicolon (;) indicates?

2. What is the (Whos) command uses?

3. Write a MATLAB program to calculate the following expression

z = 5x2 + y2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy