Professional Documents
Culture Documents
Al-Amer 2006
Objectives
To be able to create MATLAB m-files To understands the basics of MATLAB files Basic graphics
Al-Amer 2006
Creating M-files
Select FILE OPEN NEW M-files
Al-Amer 2006
MATLAB shortcuts
Create a New file Open an existing files
Al-Amer 2006
Programming in MATLAB
Al-Amer 2006
Al-Amer 2006
Programming in MATLAB
Script files
Use script file when you have a long sequence of statements to solve a problem Run the program by
typing its name in the command window from tools in the editor window
Al-Amer 2006
Example 1
Write a function file to compute the factorial of a number. Input: N Output :NF Function name: factorial
Al-Amer 2006
A solution
output First statement must start with function Function name input
A Better one
These comments will be displayed when
help factorial
function [FC]=factorial(N) % [FC]=factorial(N) % program to calculate the factorial of a number % input N : an integer % if N is not an integer the program obtains the % factorial of the integer part of N % output FC : the factorial of N % FC=1; for i=1:N FC=FC*i; end
% initial value of FC
is typed
% n! =(n-1)!*n
Comments are used to explain MATLAB statements
Al-Amer 2006 11
Al-Amer 2006
13
Graphics on MATLAB
Simple 1D graphics
2D graphics
Al-Amer 2006
14
Example
time=[0:0.01:6] Y=sin(time)
Generating data Plot Y verses time xaxis is time y- axis is Y Add a label to the xaxis Add a label to the yaxis Add a title Add grid lines
Al-Amer 2006 15
Al-Amer 2006
16
Example
time=[0:0.01:6] Y=sin(time)
Generating data
plot(time,Y)
You can add a label to the x- axis a label to the x- axis Title And others on the graph directly
(click insert)
Al-Amer 2006 17
Example
time=[0:0.01:6] Y=sin(time)
Generating data
plot(Y)
Al-Amer 2006
18
Example
time=[0:0.01:6] Y=sin(time)
Generating data
semilogx(time,Y) semilogy(t,Y)
Plot Y verses time x- axis is time (log scale) y- axis is Y (linear scale) Plot Y verses v x- axis is v (linear scale) y- axis is Y (log scale) Plot Y verses v x- axis is v (log scale) y- axis is Y (log scale)
loglog(t,Y)
You can modify the scales directly on the figure Click Edit- axis properties
Al-Amer 2006 19