Sie sind auf Seite 1von 18

By

J ain B Marshel
Assistant professor/EEE Dept.
St.Xaviers Catholic College of Engineering
FTDP on EMT organized by Department of EEE
St.Xaviers Catholic College of Engineering
16
th
to 22
nd
June, 2014
INTRODUCTION
The name MATLAB stands for matrix laboratory

MATLAB is matrix-oriented, so what would take several
statements in C or Fortran can usually be accomplished in
just a few lines using MATLAB's built-in matrix and vector
operations

It is the tool of choice for high-productivity research,
development, and analysis
ADDITION OF MATRICES IN FORTRAN VS MATLAB

FORTRAN:
real*8 A(10,10), B(10,10), C(10,10)
do i=1,10
do j=1,10
C(i,j) = A(i,j) + B(i,j)
10 continue
20 continue


MATLAB:
C = A + B


Current Directory
View folders and m-files
MATLAB SCREEN
Command Window
type commands
Workspace
View program variables
Double click on a variable
to see it in the Array Editor

Command History
view past commands
save a whole session
using diary

Default values in MATLAB
ans Default variable name for results
pi Value of
eps Smallest incremental number
inf Infinity
nan Not a number e.g. 0/0
i and j square root of -1
realmin The smallest usable positive real number
realmax The largest usable positive real number
1e-3 means 1x10
-3

2.5e3 means 2.5x10
3

core MATLAB files
Script
Data Files
Function
Model
GUI
MATLAB script
Open a blank M-File
Type the program in the newly
opened file
Save the file in a local folder
Run the file using debugRun or F5 button, and output
can be viewed in the command window.
Points to be noted while saving the script file :

The script name should not be in numeric alone.
No space or special character should be there in the file name.
Should not be saved in the name of a built in function.
Shall be saved in alphanumeric or characters alone.

Eg: 1234 eee_sxcce
matrix 5 matrix5
eee@sxcce sum1234
sum

Writing a MATLAB script
Understand the problem clearly.
Form proper algorithm for the problem.
Define suitable logic to implement the algorithm.
Implement the algorithm using suitable commands.
Basic MATLAB commands
who
whos
dir
clc
clear
addpath(path name)
input(write input details for understanding purpose)
Arithmatic Operations:
+ Addition ; - Substraction
* Multiplication ; / Division
.* Element wise Multiplication
./ Element wise Division
**Note : All commands in MATLAB are case sensitive**
A(4)
A(2,3)
zeros(4)
ones(3)
zeros(2,3)
ones(2,3)
eye(3)
det(A)
Basic Matrix Operations :
A=[1 2 4;2 3 6;2 7 9]
B=A
A(:,2)
A(2,:)
A(1:2,:)
sum(A)
mean(A)
size(A)

inv(A)
X=[A B]
X=[A;B]
X=1:10
X=1:0.5:10
length(X)
A^2
A.^2
Simple program in MATLAB
Addition of two numbers using MATLAB:
clc;
clear;
a=input('Enter first number:');
b=input('Enter second number:');
s=a+b

Output:
Enter first number:




7
8
Enter second number:
s =

15
CONDITIONAL STATEMENTS
if-else Statement:
if condition1
statements
elseif condition2
statements
else
statements
end
a = randi(100, 1);
if a < 30
disp('small')
elseif a < 80
disp('medium')
else
disp('large')
end
Switch-case Statement:
switch variable
case value1
statements
case value2
statements
otherwise
statements
end
a = randi(100, 1);
x=rem(a,2);
switch x

case 1
disp(Even Number)
otherwise
disp(Odd Number)
end

LOOP CONTROL STATEMENTS
for loop :
for variable = expr
statement-1,
...,
statement-n
end
x = ones(1,10);
for n = 2:6
x(n) = 2 * x(n-1);
end
while loop :
while expression
statements
end
n = 1;
s=0;
while n <= 5
s=s+n;
n=n+1;
end
Simple exercise
1. Use for loop to print the word MATLAB 5 times. Write another code for the
same using while loop.
2. Write a program to convert the given Cartesian coordinate system to
cylindrical coordinate system .
3. Write a program to convert the given Cartesian coordinate system to spherical
coordinate system .
4. Write a program to convert the given Cartesian coordinate system to
cylindrical or spherical coordinate system whichever required in the same
script.
5. Write a program to find the force of attraction between two point charges
separated by a distance.

Das könnte Ihnen auch gefallen