Vous êtes sur la page 1sur 6

Session 1

Section A: Finding your way around


Task 1: Identify components of the environment:
1. Command History Window
This window shows all what we have done in the command window in past. From
command history , we can view previously run statements, as well as copy and execute selected
statements. All information are in the sessions of date and time.

2. Command Window
The command window is user to enter variables and to run functions and M-file scripts.
We can enter the matlab functions at the command window prompt. This command window also
displays the result.

3. Start Button and Launch Pad


The MATLAB Start button provides easy access to tools, demos, shortcuts, and
documentation. Click the Start button to see the options and access those options.

4. Help Browser
Use the Help browser Contents for a product to view Functions .in help browser there is
alphabetical list or Functions and also by category, to view more extensive help for a function in
the Help browser run doc functionname.

5. Current Directory Browser


MATLAB file operations use the current directory as reference points. Any file we want
to run must be in the current directory. A quick way to view or change the current directory is by
using the current directory field in the desktop toolbar.

6. Workspace Browser
In work space window all the variables that we use in command window are shown with
their name, value and class.

Task 2: For what purpose the help help command is used


This command will display text in the Command Window explaining what help does and
how to use it. Writing help help in command window will also show doc help which is used to
view more extensive help for all functions. The most common use of the help is to find out what
a certain command or function does in MATLAB.

Section B: The Basics


Task 3: Perform basic mathematical operations.
The simple arithmetic operations can be perform easily in Matlab, simply write following
syntax in command window
>>6+2
ans=
8
>>6/2
ans=
3
The basic mathematics rules of addition, subtraction, multiplication and divison can be
performed through the commands shown above.
Question: What does the semicolon do?
If semicolon is written at the end of statement then the result of that statement will not be
displaced on the command window.

Section C Matrix Manipulation


Task 4: Create an array (also referred to as a vector)
To create an array, vector, write the following syntax on command window:
A=[16 3 2 13]
Second method is given by: b = 1:2:10
In this method there is a fixed interval or increase

TASK 5 Create a matrix:


To create a matrix write the following command:
A=[16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
The semi colon in above command shows the end of rows.
Fuctiions of Matrix
sum(A) This is a row vector of the sums of the columns of A that is it will sum the each
columns and shown in row vector form
A’ This is used to take the transpose of A
A(1,4) This will display the wlwment of A matrix locating at row 1 and column 4.
Section D: Array and Matrix Operators
Task 6: Solves the set of matrix equations Wx = b.
W = rand(4);
b = rand(4,1)
x = W\b;
b=
0.9355
0.9169
0.4103
0.8936
>> W*x
ans =
0.9355
0.9169
0.4103
0.8936
So W*x=b Solved
Task 7: Perform different Array and Matrix Arithmetic Operators:
The simple + - * / are used for matrix multiplication and placing dot ’.’ before operator is
used for array multiplication. For Example
>> c = 1:5
c=
1 2 3 4 5
>> c*c
??? Error because simple operators are used for matrix operations and c is an array..
>> c.*c
ans =
1 4 9 16 25

Section E: Functions
Task 8: Use built in functions
Rand is a built in function which takes one or more inputs e.g:
rand(3,4)
It should output a 3×4 matrix of random numbers.

TASK 9 The function that can return two outputs


The eig function is an example of a function that can return two outputs. It outputs the
eigen values and eigenvectors of the input matrix.
X=rand(3);
[V,D]=eig(X)
V=
-0.1940 -0.3436 0.2351
-0.7423 -0.6389 -0.9716
-0.6414 0.6883 0.0248
D=
0.7528 0 0
0 -0.3219 0
0 0 0.0381
TASK 10 Use Mat lab’s help command to find out about each of the other built in functions.

Section F Using Files


TASK 11
Outside of MATLAB, use a text editor (such as Notepad ) to create a file called fred.dat
containing a data array looking like:
1234
5678
8765
4321
Save the file in the MATLAB work directory and then enter in the MATLAB Command
Window: load fred.dat
Look at the variable fred. There are many commands and functions for saving and retrieving
data such as fread, fprintf, save, etc.
TASK 12 Use M file
Typing commands in Command Window is not a good way to realise larger programs.
Use M file to perform tasks, which is started from File-> New-> M-File and write all previous
commands which we had used earlier.

TASK 13 Write your own functions


function [y, z] = funcname(x, i)
The word function is a MATLAB keyword that declares a function. The variables in
square brackets [ ] are the variables returned by the function. Brackets can be omitted if there is
only one return variable. The variables in round brackets ( ) are input argument variables passed
to the function and funcname is the function’s name used to call it,
Differences between scripts and functions:
1. Scripts are lines of code that you would type into the Command Window, so variables are
created in your workspace.
2. Functions have local variables (unless otherwise declared global by global), so they are
destroyed once the function is returned.
Section G Plotting Graphs
TASK 14 Creat a plot
Use the colon operator to make a vector having values in
the range (0, 2p) and plot the sine of this vector.
x = 0:pi/100:2*pi
y = sin(x);
plot(x,y);
xlabel(‟x = 0:2\pi‟);
ylabel(‟y = sin(x)‟);
Remember the semicolons stop results printing to screen
which makes the program run faster.

TASK 15 Multiple data sets on one graph.


Create another two functions dependent on x
and put them in the plot function:
y2 = sin(x-0.25);
y3 = sin(x-0.5);
plot(x,y,x,y2,x,y3);
To make it easier to identify the different
lines use a legend:
legend(‟sin(x)‟,‟sin(x-0.25)‟,‟sin(x-0.5)‟);

TASK 16 Specify line colours and styles 1

0.8
The plot function allows for many customisations 0.6
such as line colours, line styles, etc. 0.4

x1 = 0:pi/100:2*pi; 0.2

x2 = 0:pi/10:2*pi; 0

plot(x1,sin(x1),‟r:‟,x2,sin(x2),‟b+‟); -0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
TASK 17 Imaginary and complex data 1

0.8

0.6
When plot is used on complex data the imaginary part is 0.4

ignored, except when plot is given a single complex argument. 0.2


For this case the real part is plotted versus the 0

imaginary part. So plot(Z) is equivalent to plot(real(Z),imag(Z)).


-0.2

t = 0:pi/10:2*pi; -0.4

plot(exp(j*t),’-o’); -0.6

-0.8

-1
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
3

TASK 18 Adding multiple 2

By using the hold on command you can add more graphs 1

[x,y,z] = peaks;
pcolor(x,y,z) 0

shading interp
hold on -1

contour(x, y, z, 20,'k')
hold off -2

-3

TASK 19 Figure windows


-3 -2 -1 0 1 2 3

x1 = 0:pi/100:2*pi
x2 = 0:pi/10:2*pi
figure(1)
plot(x1,sin(x1),‟r:‟);
figure(2)
plot(x2, sin(x2),‟b+‟);
Using above code the two figures are shown in two different windows

TASK 20 Multiple plots in one figure


t=0:pi/10:2*pi; 5 5

[X,Y,Z] = cylinder(4*cos(t)); 0 0
subplot(2,2,1);
mesh(X); -5
40
-5
40
40 40
subplot(2,2,2); 20
0 0
20 20
0 0
20

mesh(Y);
subplot(2,2,3);
mesh(Z); 1 1

subplot(2,2,4); 0.5 0.5

mesh(X,Y,Z); 0 0
grid lines can be set using: 40
20
40
5
0
5

grid on 0 0
20
-5 -5
0

grid off
The command subplot is used to have more than one figures in one or same window. The
command mesh is used to show the figure in three dimension.
Section G: Flow Control
Task 21: Use of input command
Input command is used to take the input from user. E.g.
z = input(‟enter a value for z‟)
Task 22: Use of if command
If command is used to select different alternatives based on different conditions for example:
temperature = input(‟enter a temperature ‟)
if temperature >= 90
disp(‟It’s getting hot‟);
elseif temperature < 90 & temperature > 50
disp(‟This is just right‟);
else
disp(‟I think I will get my coat‟);
end
Output:
enter a temperature 101
It’s getting hot
Task 23: Use of switch command
Switch statement is used instead of if statement and have same functionality as if
statement has.

TASK 24 The for loop


This example repeats a statement a predetermined number of times:
function factx = fact(x)
a=1;
for k=1:x
a=a*k;
end
factx=a;
TASK 25 The for loop
The while loop repeats a group of statements an indefinite number of times under the
control of a logical condition. For example:
a=0; fa=-Inf;
b=3;fb=Inf;
while b-a > eps*b
x=(a+b)/2;
fx=xˆ3-2*x-5;
if(sign(fx) == sign(fa))
a=x; fa=fx;
else
b=x;fb=fx;
end
end
x

Vous aimerez peut-être aussi