Professional Documents
Culture Documents
Introduction to MATLAB
An Example Session
>> 8/10
ans=
0.8000
>> 5*ans
ans=
4
>> r=8/10
ans=
0.8000
>>r
r=
0.8000
18-22 May, 2014 © 2014 UPES
2.3 Order of Precedence
>> s=3+7i;w=5-9i;
x 1 2 3 5 7 7.5 8 10
y 2 6.5 7 7 5.5 4 6 8
plot(x,y,‘line specifiers’)
plot(x,y,‘line specifiers’)
plot(x,y,’*’) The points are marked with * (no line between the
points.)
fplot(‘function’,limits)
The limits is a vector with the domain of x, and optionally with limits
of the y axis:
[xmin,xmax] or [xmin,xmax,ymin,ymax]
Line specifiers can be added.
Example:
>> t=0:.1:10
>>plot(t,sin(t));
>>hold on;
>>plot(t,cos(t));
plot(x,yd,'--r')
plot(x,ydd,':k') Two more graphs are created.
hold off
1. Formatting commands.
In this method commands, that make changes or
additions to the plot, are entered after the plot()
command. This can be done in the Command
Window, or as part of a program in a script file.
title(‘string’)
Adds the string as a title at the top of the plot.
xlabel(‘string’)
Adds the string as a label to the x-axis.
ylabel(‘string’)
Adds the string as a label to the y-axis.
text(x,y,’string’)
Places the string (text) on the plot at coordinate x,y relative to
the plot axes.
gtext(‘string’)
Places the string (text) on the plot. When the command
executes the figure window pops and the text location is clicked
with the mouse.
Creating a legend.
The name of a script file must begin with a letter, and may
include digits and the underscore character, up to 63
characters.
1. Comments section
a. The name of the program and any key words in the first
line.
b. The date created, and the creators' names in the second
line.
c. The definitions of the variable names for every input and
output variable. Include definitions of variables used in the
calculations and units of measurement for all input and all
output variables!
2. Input section: Include input data and /or the input functions
and comments for documentation.
Problem:
% Program falling_speed.m:
% Plots speed of a falling object.
% Created on May 18, 2014
%
% Input Variable:
% tfinal = final time (in seconds)
%
% Output Variables:
% t = array of times at which speed is
% computed (in seconds)
% v = array of speeds (meters/second)
%
18-22 May, 2014 © 2014 UPES
4.11 Example of a script file Contd…
% Parameter Value:
g = 9.81; % Acceleration in SI units
%
% Input section:
tfinal = input(’Enter final time in seconds:’);
%
% Calculation section:
dt = tfinal/500;
% Create an array of 501 time values.
t = 0:dt:tfinal;
% Compute speed values.
v = g*t;
%
% Output section:
Plot(t,v),xlabel(’t (s)’),ylabel(’v m/s)’)
function z = fun(x,y)
u = 3*x;
z = u + 6*y.^2;
Note the use of a semicolon at the end of the
lines. This prevents the values of u and z from
being displayed.
Note also the use of the array exponentiation
operator (.^). This enables the function to
accept y as an array.
18-22 May, 2014 © 2014 UPES
5.4 User defined Functions: Example contd..
function show_date
clear
clc
today = date
function y = f1(x)
y = x + 2*exp(-x) - 3;
To create a handle to this function and name the handle fh1, you
type fh1=@f1
>> p = [1 2 3 4 5]
Let
p ( x) 6 x 7 x 8
3
>> p= [6 0 7 8]
r 6 x 19 x 40 x 61x 82 x 67 x 40
6 5 4 3 2
Therefore s= r/p =q
i.e., >> s = deconv(r,p)
s=
678
s 1 0.5 0.5
s( s 2) s 2 s
If f(s)=(s^2+1)/(s^2(s+2))
>>n=[1 0 1];d=poly([0 0 -2]);
>>[r p k]=residue(n,d)
r= p= k=
1.25 -2 [] Empty
-0.25 0
0.5 0
Advanced Plotting
The mesh plot is made of lines that connect the points. In the surface plot, areas
within the mesh lines are colored.
Symbolic Computation
>> sol=solve('3*x-5*y=30','x+5*y=50')
sol =
x: [1x1 sym]
y: [1x1 sym]
>> sol.x=20
>>sol.y=6
>> R=5*y^2*cos(3*t);
>> diff(R)
ans =
10*y*cos(3*t)
Programming in MATLAB
Operator Meaning
>>z = (x < y)
z =
1 0 0
>>z = (x ~= y)
z =
1 1 0
>>z = (x > 8)
z =
0 0 1
z = x(x<y)
z = 5 > 2 + 7
is equivalent to
z = 5 >(2+7)
if logical expression
statements
end
if logical expression
statement group 1
else
statement group 2
end
x = [4,-9,25];
if x < 0
disp(’Some of the elements of x are negative.’)
else
y = sqrt(x)
end
y =
2 0 + 3.000i 5
if logical expression 1
statement group 1
elseif logical expression 2
statement group 2
else
statement group 3
end
if x > 10
y = log(x)
elseif x >= 0
y = sqrt(x)
else
y = exp(x) - 1
end
for k = 5:10:35
x = k^2
end
x = [10,1000,-10,100];
y = NaN*x;
for k = 1:length(x)
if x(k) < 0
continue
end
y(k) = log10(x(k));
end
y
The result is y = 1, 3, NaN, 2.
x = 5;
while x < 25
disp(x)
x = 2*x - 1;
end
switch angle
case 45
disp(’Northeast’)
case 135
disp(’Southeast’)
case 225
disp(’Southwest’)
case 315
disp(’Northwest’)
otherwise
disp(’Direction Unknown’)
end
18-22 May, 2014 © 2014 UPES
10.20 The break command
When inside a loop the break command terminates the
execution of the loop.