Meteen naar document
Dit is een Premium document. Sommige documenten op Studeersnel zijn Premium. Upgrade naar Premium om toegang te krijgen.

Practicum - Matlab tutorial

Matlab Tutorial
Vak

Computational Intelligence (CSE2530)

44 Documenten
Studenten deelden 44 documenten in dit vak
Studiejaar: 2016/2017
Geüpload door:
0volgers
6Uploads
1upvotes

Reacties

inloggen of registreren om een reactie te plaatsen.

Preview tekst

TI2736-A COMPUTATIONALINTELLIGENCE

MATLAB Tutorial

September 8, 2016

INTRODUCTION

The goal of this tutorial is to get familiar with MATLAB, its layout, syntax and prac- tices. While doing this you will be introduced to the basic principles of neural networks. MATLAB can be obtained from the official MathWorks website by reg- istering as a TU Delft student. You do not have to download the complete MAT- LAB package,only the base MATLAB product + the Neural Network Toolboxare needed for this course.

To assist you with understanding MATLAB, you can consult the following re- sources:

  • ❤tt♣✿✴✴✇✇✇✳♠❛t❤✇♦r❦s✳♥❧✴❤❡❧♣✴♠❛t❧❛❜✴❣❡tt✐♥❣✲ st❛rt❡❞✲ ✇✐t❤✲ ♠❛t❧❛❜✳❤t♠❧
  • ❤tt♣s✿✴✴✇✇✇✳②♦✉t✉❜❡✳❝♦♠✴✉s❡r✴▼❆❚▲❆❇✴♣❧❛②❧✐sts→Getting Started
  • Mathworks Support : Mathworks has an intensive support community. Ask Questions!
  • Google!

1 MATLAB

MATLAB (MATrix LABoratory) is a multi-paradigm numerical computing environment and fourth-generation programming language. It’s a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numerical computation.

The MATLAB desktop consists of the following windows:

  • Current Directory
  • Command Window
  • Workspace

We will now quickly introduce these windows.

CURRENTDIRECTORY

The current folder indicates the working path of MATLAB. The current folder can contain MATLAB script, function and class files, all ending with *.m. You can use this window to get an overview of the current files in scope and to explore your folders.

COMMANDWINDOW

The command window is used to send commands to the MATLAB interpreter. It also dis- plays information as invoked by the user. The console is directly started when you open MATLAB.

1)Type the following calculation into the command window:✷ ✰ ✸

2)Try more calculations like✶ ✴ ✸, ✶✵ ✯ ✶✵ ✯ ✶✵and✺ ✯ ✶❡✲✶✵

3)Clear the command window with❝❧❝(clear console)

OPERATIONS

Now that we know how to create and store arrays, we can start to do some operations with them. To transpose a matrix, use a single quote(’), directly after the variables name.

10)echo the transpose ofato the command window. 11)Create a variablec, which is the product of vectoraandb.

We can access certain elements of an array by indicating the row and column of that element within parentheses. For example, to access the 2nd row of the 3rd column ofA, we type: "A(2,3)", try it!

FUNCTIONS

We’ve already encountered one MATLAB function,♠❛❣✐❝✭s✐③❡✮, which creates a matrix

whose rows and columns all add up to the same number. Other useful functions are:

  • ③❡r♦s✭♥✱♠✮: Creates an array of size n-by-m, filled with zeros.
  • ♦♥❡s✭♥✱♠✮: Creates an array filled with ones.
  • ❡②❡✭♥✱♠✮: Creates an n-by-m identity matrix.

MATLAB has countless of these predefined functions, all of whom can be found on the Math-

Works help site:❤tt♣✿✴✴✇✇✇✳♠❛t❤✇♦r❦s✳♥❧✴❤❡❧♣✴♠❛t❧❛❜✴. If you are ever looking for a

certain function, such as calculating the determinant of a matrix, search for it before you reinvent the wheel. MATLAB is very powerful because it supports almost every math opera- tion you can think of. This allows you to quickly prototype a complex calculation.

If your are ever in doubt on how to use a function or how to interpret its output, you can also

get help straightly from the command window by typing❤❡❧♣followed by the name of the

function. The result will not only give you the function description, but also related functions and a direct link to the MATLAB help pages.

12)Request the documentation of the function✐♥✈in the command window.

SCRIPTS

If we want to write meaningful algorithms, we can’t be writing each command to the inter- preter separately. This is where scripts come in.

13)Open a new script by clicking in the top left corner.

We can now start to type in commands in the newly created script. You can comment certain

lines by using the percentage character (✪). It’s good coding practice to always start a script

with a clear title and a short description of what the algorithm will do. For clarification you

can divide your code into segments by using a double percentage (✪✪), followed by the title

of that segment.

An example of a (pretty useless) script could then look something like this:

1 ✪ ✪ ❊ ① ❛ ♠ ♣ ❧ ❡ ❚ ✐ t ❧ ❡ 2 ✪ ❚ ❤ ✐ s s ❝ r ✐ ♣ t ❝ ♦ ✉ ♥ t s ❢ r ♦ ♠ ✶ t ♦ ✶ ✵ ✳ 3 ✪ ❋ ✐ ♥ ❛ ❧ ❧ ② ✐ t ❞ ✐ s ♣ ❧ ❛ ② s t ❤ ❡ ❝ ❧ ❛ s s ✐ ❝ ❝ ♦ ❞ ✐ ♥ ❣ ❡ ① ❛ ♠ ♣ ❧ ❡ t ♦ t ❤ ❡ ❝ ♦ ♠ ♠ ❛♥ ❞ ←- ✇ ✐ ♥ ❞ ♦ ✇ ✳ 4 ✪ ❈ ♦ ❞ ❡ ❜ ② ◆ ❆ ▼ ❊ ● ❖ ❊ ❙ ❍ ❊ ❘ ❊ 5 ✪ ▲ ❛ s t ❝ ❤ ❛ ♥ ❣ ❡ ❞ ✿ ✷ ✲ ✾ ✲ ✷ ✵ ✶ ✹ 6 ❝ ❧ ❝ 7 ❝ ❧ ❡ ❛ r ❛ ❧ ❧ 8 9 ✪ ✪ ❱ ❛ r ✐ ❛ ❜ ❧ ❡ s 10 ❝ ♦ ✉ ♥ t ❂ ✵ ❀ ✪ ■ ♥ ✐ t ✐ ❛ ❧ ✐ ③ ❡ ❝ ♦ ✉ ♥ t ✈ ❛ r ✐ ❛ ❜ ❧ ❡ t ♦ ③ ❡ r ♦ 11 12 ✪ ✪ ❈ ♦ ✉ ♥ t ❡ r 13 ❢ ♦ r ✐ ❂ ✶ ✿ ✶ ✿ ✶ ✵ ✪ ■ t ❡ r ❛ t ❡ ❢ r ♦ ♠ ✶ t ♦ ✶ ✵ ✐ ♥ s t ❡ ♣ s ♦ ❢ ✶ 14 ❝ ♦ ✉ ♥ t ❂ ❝ ♦ ✉ ♥ t ✰ ✶ 15 ❡ ♥ ❞ 16 17 ✪ ✪ ❊ ❝ ❤ ♦ s t r ✐ ♥ ❣ t ♦ ❝ ♦ ♥ s ♦ ❧ ❡ 18 ❞ ✐ s ♣✭✬ ❍ ❡ ❧ ❧ ♦ ❲ ♦ r ❧ ❞ ✬✮ ❀

CONDITIONALSTATEMENTS

MATLAB has a rather unusual syntax when it comes to conditional statements. The following example demonstrates the if-else clause, the while- and the for-loop.

1 ❞ ♦ ♥ ❡ ❂ ✵ ❀ 2 3 ✇ ❤ ✐ ❧ ❡ ⑦ ❞ ♦ ♥ ❡ ✪ ✇ ❤ ✐ ❧ ❡ ◆ ❖ ❚ ❞ ♦ ♥ ❡ 4 ❢ ♦ r ✐ ❂ ✶ ✿ ✶ ✵ ✵ ✪ ❧ ♦ ♦ ♣ ✶ ✵ ✵ t ✐ ♠ ❡ s 5 ① ❂ r ❛ ♥ ❞❀ ✪ s ❡ t ① t ♦ ❛ r ❛ ♥ ❞ ♦ ♠ ♥ ✉ ♠ ❜ ❡ r 6 ✐ ❢ ① ❁ ✵ ✳ ✵ ✵ ✶ ✪ ✐ ❢ ① ✐ s s ♠ ❛ ❧ ❧ ❡ r t ❤ ❛ ♥ ✵ ✳ ✵ ✵ ✶ 7 ❞ ♦ ♥ ❡ ❂ ✶ ❀ ✪ s ❡ t ❞ ♦ ♥ ❡ t ♦ ✶ ✭ t r ✉ ❡ ✮ 8 ❜ r ❡ ❛ ❦ ✪ ❜ r ❡ ❛ ❦ t ❤ ❡ ❢ ♦ r ❧ ♦ ♦ ♣ 9 ❡ ♥ ❞ ✪ ❡ ♥ ❞ ✐ ❢ ❝ ♦ ♥ ❞ ✐ t ✐ ♦ ♥ 10 ❡ ♥ ❞ ✪ ❡ ♥ ❞ ❢ ♦ r ❧ ♦ ♦ ♣ 11 ❡ ♥ ❞ ✪ ❡ ♥ ❞ ✇ ❤ ✐ ❧ ❡ ❧ ♦ ♦ ♣

Inputs Output x 1 x 2 Y 0 0 1 0 0 1 1 1

Do you recognize the logic? In this configuration the perceptron acts as an OR operator, which outputs 1 when at least one input is 1. It is also possible to make the perceptron behave as an AND operator, where the output value is only 1 when both inputs are 1.

17)Try to guess the values for the weights and threshold to obtain an AND operator. Modify the script to validate your answer. How many trials did you need to get the right values?

HINT:

  1. Change the values for the weights
  2. Check whether their perceptron works as an AND, given all possible input combina- tions
  3. If not, go to back to 1. 18)Instead of working with individual variables, put the weights and inputs in a vector.

The weighted sum can then be calculated with the dot product:❳❂❞♦t✭①✱✇✮or❳❂①✯

✇✬.

GETTING YOUR PERCEPTRON TO LEARN ANAND

We can set the weights by hand to make the perceptron perform the desired operation, but we do not always know the exact function involved. A raw perceptron can be trained to perform a simple operation like AND based on a set of inputs and outputs. Initially the weights are randomly assigned, and through iteration the values are updated. This is done by making small adjustments in the weights to reduce the output error. The output error of an iteration pis shown in equation??.

e(p)=Ydesired(p)−Yactual(p) (2)

You will modify your script to train a perceptron to behave like an AND operator. First, it is important to calculate the output error of each input pattern.

19)Use the random number generator (rand()to set the weights to a starting value be- tween -1 and 1. With a threshold of 1, calculate the output error for each set of inputs. Your script should generate and evaluate all 4 input patterns sequentially.

After each evaluation, the output error can be used to update the perceptron. Equation?? shows theperceptron learning rulewhich can be used to calculate the new weight value at iterationp+1.

wi(p+1)=wi(p)+α·xi(p)·e(p) (2)

αis the learning rate and can be assumed to be 0. The learning rule can be used each time an output is calculated to update weightsw 1 andw 2. A fullepochis completed when all input patterns are evaluated. Typically, the perceptron will need around 10-20epochsto converge, depending on the configuration.

20)Use a for-loop to train the perceptron according to the above explained method. Use vector indexing to update the weights independently. How many epochs do you need until the perceptron can perform the AND operation flawlessly?

It is important to monitor the training progress of the perceptron, as it can give information about the rate of convergence. A good candidate for this is the sum of squared errors of each iteration within an epoch, thesum squared error:E=

pe(p) 2 21)Use a vector to store these values and calculate the sum squared error with the MAT-

LABs✉♠sqrfunction. At the end of your script, plot the sum squared error of all epochs. How

does the learning rate valueαinfluence the learning curve? What happens with the weight and threshold values withα=100?

LIMITATIONS OF THE PERCEPTRON

22)Modify your script to train the perceptron to function like an XOR operator. What happens with the sum squared error?

23)What would be the weight and threshold values for a single-input perceptron that performs the NOT operation?

During the upcoming lectures, you will learn a lot more about the perceptron and it’s limi- tations. More importantly, you will learn how to group perceptrons together to perform very powerful calculations.

Was dit document nuttig?
Dit is een Premium document. Sommige documenten op Studeersnel zijn Premium. Upgrade naar Premium om toegang te krijgen.

Practicum - Matlab tutorial

Vak: Computational Intelligence (CSE2530)

44 Documenten
Studenten deelden 44 documenten in dit vak
Was dit document nuttig?

Dit is een preview

Wil je onbeperkt toegang? Word Premium en krijg toegang tot alle 8 pagina's
  • Toegang tot alle documenten

  • Onbeperkt downloaden

  • Hogere cijfers halen

Uploaden

Deel jouw documenten voor gratis toegang

Ben je al Premium?
TI2736-A COMPUTATIONAL INTELLIGENCE
MATLAB Tutorial
September 8, 2016
INTRODUCTION
The goal of this tutorial is to get familiar with MATLAB, its layout, syntax and prac-
tices. While doing this you will be introduced to the basic principles of neural
networks. MATLAB can be obtained from the official MathWorks website by reg-
istering as a TU Delft student. You do not have to download the complete MAT-
LAB package, only the base MATLAB product + the Neural Network Toolbox are
needed for this course.
To assist you with understanding MATLAB, you can consult the following re-
sources:
❤tt♣✿✴✴✇✇✇✳♠❛t❤✇♦r❦s✳♥❧✴❤❡❧♣✴♠❛t❧❛❜✴❣❡tt✐♥❣✲st❛rt❡❞✲✇✐t❤✲♠❛t❧❛❜✳❤t♠❧
❤tt♣s✿✴✴✇✇✇✳②♦✉t✉❜❡✳❝♦♠✴✉s❡r✴▼❆❚▲❆❇✴♣❧❛②❧✐sts
Getting Started
Mathworks Support : Mathworks has an intensive support community. Ask Questions!
Google!
1 MATLAB
MATLAB (MATrix LABoratory) is a multi-paradigm numerical computing environment and
fourth-generation programming language. Its a high-level technical computing language
and interactive environment for algorithm development, data visualization, data analysis,
and numerical computation.
Delft University of Technology 1

Waarom is deze pagina onscherp?

Dit is een Premium document. Word Premium om het volledige document te kunnen lezen.

Waarom is deze pagina onscherp?

Dit is een Premium document. Word Premium om het volledige document te kunnen lezen.