(matlab..a walk through) ai (1... · algorithms, enormous data handling abilities, and powerful...

19
(MATLAB..A WALK (MATLAB..A WALK THROUGH) THROUGH) Siti Siti Zaiton Zaiton Mohd Mohd Hashim Hashim, PhD , PhD www.kpnet.fsksm.utm.my/sitizaiton www.kpnet.fsksm.utm.my/sitizaiton [email protected] [email protected]

Upload: others

Post on 01-Nov-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

(MATLAB..A WALK (MATLAB..A WALK THROUGH)THROUGH)

SitiSiti ZaitonZaiton MohdMohd HashimHashim, PhD, PhDwww.kpnet.fsksm.utm.my/sitizaitonwww.kpnet.fsksm.utm.my/sitizaiton

[email protected]@utm.my

Page 2: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

What Is MATLAB?What Is MATLAB?

MATLAB is a highMATLAB is a high--performance language for performance language for technical computing. technical computing.

It integrates It integrates ComputationComputation VisualizationVisualization programming programming in an easyin an easy--toto--use environment where problems use environment where problems and solutions are expressed in familiar mathematicaland solutions are expressed in familiar mathematicalnotation. notation.

Page 3: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

What Is MATLAB?What Is MATLAB?

Matlab's strengths include cuttingMatlab's strengths include cutting--edge edge algorithms, enormous data handling abilities, algorithms, enormous data handling abilities, and powerful programming tools.and powerful programming tools.

The Matlab is not designed for symbolic The Matlab is not designed for symbolic computation, but does include a Maple kernel computation, but does include a Maple kernel for such computations. The interface is mostly for such computations. The interface is mostly texttext--based.based.

Matlab is packaged as a core program with Matlab is packaged as a core program with several "toolboxes, sold separately. several "toolboxes, sold separately.

Page 4: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

What Is MATLAB?What Is MATLAB?

Typical uses includeTypical uses include Math and computationMath and computation Algorithm developmentAlgorithm development Data acquisitionData acquisition Modeling, simulation, and prototypingModeling, simulation, and prototyping Data analysis, exploration, and visualizationData analysis, exploration, and visualization Scientific and engineering graphicsScientific and engineering graphics Application development, including graphical user Application development, including graphical user

interface buildinginterface building

Page 5: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include
Page 6: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

MatricesMatrices

In Matlab, a matrix is a rectangular array of In Matlab, a matrix is a rectangular array of numbernumber

Use a semicolon ; to indicate the end of each Use a semicolon ; to indicate the end of each rowrow

Not ending with ; will display the output in Not ending with ; will display the output in Command WindowCommand Window

>> A =[16 3 2 13; 5 10 11 8]>> A =[16 3 2 13; 5 10 11 8]

Page 7: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

MatricesMatrices

Nom=ones(3)Nom=ones(3) NomNom Nom’Nom’ Sum(Nom)Sum(Nom) Nom(2)Nom(2) Nom(2)=14Nom(2)=14

Page 8: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

The Colon OperatorThe Colon Operator

The colon : is one of the most important The colon : is one of the most important MATLAB operators. It occurs in several MATLAB operators. It occurs in several different forms. The expression different forms. The expression

>>1:10>>1:10is a row vector containing the integers from 1 to is a row vector containing the integers from 1 to

1010>> 100:>> 100:--7:50 7:50 specify an increment/decrementspecify an increment/decrement

Page 9: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Matrices ..moreMatrices ..more

A(3,2)=0A(3,2)=0Subscript expressions involving colons refer to Subscript expressions involving colons refer to

portions of a matrix:portions of a matrix: A(1:k,j)A(1:k,j) sum(A(1:4,4))sum(A(1:4,4)) sum(A(:,end))sum(A(:,end))

Page 10: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Creating a PlotCreating a Plot The plot function has different forms, depending on the input The plot function has different forms, depending on the input

arguments. If you specify two vectors as argumentsarguments. If you specify two vectors as arguments

plot(x,y) produces a graph of y versus x.plot(x,y) produces a graph of y versus x.

Eg. create a vector of x values ranging from 0 to 2π, compute the Eg. create a vector of x values ranging from 0 to 2π, compute the sine of these values, and plot the result:sine of these values, and plot the result:

>> x = 0:pi/100:2*pi>> x = 0:pi/100:2*pi>> y = sin(x);>> y = sin(x);>>plot(x,y)>>plot(x,y)

Now label the axes and add a title. Now label the axes and add a title. >>xlabel('x = 0:2*pi')>>xlabel('x = 0:2*pi')>>ylabel('Sine of x')>>ylabel('Sine of x')>>title('Plot of the Sine Function','FontSize',12)>>title('Plot of the Sine Function','FontSize',12)

Page 11: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Plot a GraphPlot a Graph

Page 12: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Graph..moreGraph..more

Placing Markers at Every Tenth Data PointPlacing Markers at Every Tenth Data Point You might want to use fewer data points to plot the You might want to use fewer data points to plot the

markers than you use tomarkers than you use to plot the lines. This example plots the data twice using plot the lines. This example plots the data twice using

a different number ofa different number of points for the dotted line and marker plots:points for the dotted line and marker plots: x1 = 0:pi/100:2*pi;x1 = 0:pi/100:2*pi; x2 = 0:pi/10:2*pi;x2 = 0:pi/10:2*pi; plot(x1,sin(x1),'r:',x2,sin(x2),'r+')plot(x1,sin(x1),'r:',x2,sin(x2),'r+')

Page 13: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Graph ..moreGraph ..more

Page 14: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Saving Workspace DataSaving Workspace Data You can save the variables in your workspace You can save the variables in your workspace

by selecting by selecting Save Workspace As Save Workspace As from the from the figure figure File File menu. menu.

You can reload saved data using the You can reload saved data using the Import Import Data Data item in the figure item in the figure File File menu. MATLAB menu. MATLAB supports a variety of data file formats, supports a variety of data file formats, including MATLAB data files, which have a including MATLAB data files, which have a .mat extension..mat extension.

Or just type in Command window the name Or just type in Command window the name >> save today>> save todayall var in memory is saved in today.matall var in memory is saved in today.mat

>>clear all; >>load today; >>whos>>clear all; >>load today; >>whos

Page 15: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Visualisation..graphVisualisation..graph

[X,Y] = meshgrid([X,Y] = meshgrid(--8:.5:8);8:.5:8); R = sqrt(X.^2 + Y.^2) + eps;R = sqrt(X.^2 + Y.^2) + eps; Z = sin(R)./R;Z = sin(R)./R; mesh(X,Y,Z,'EdgeColor','black')mesh(X,Y,Z,'EdgeColor','black')

surf(X,Y,Z)surf(X,Y,Z) colormap hsvcolormap hsv colorbarcolorbar

Page 16: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Visualisation..Visualisation..

Page 17: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Creating Function MCreating Function M--filefile Instead of using the command line, you may create a function with Instead of using the command line, you may create a function with

extension .mextension .m For example here is a simplified version of the function humps from the For example here is a simplified version of the function humps from the

matlab/demos directory. You create a new window under File New and matlab/demos directory. You create a new window under File New and type the commands:type the commands:

function y = humps(x)function y = humps(x)

y = 1./((xy = 1./((x--.3).^2 + .01) + 1./((x.3).^2 + .01) + 1./((x--.9).^2 + .04) .9).^2 + .04) -- 6;6;y = humps(x);y = humps(x);%Then plot the function with%Then plot the function withplot(x,y)plot(x,y)

FileFile-- SaveSave-- new1.mnew1.m In command window, Evaluate this function at a set of points in the interval In command window, Evaluate this function at a set of points in the interval

0 ≤ x ≤ 1 0 ≤ x ≤ 1 >> x = 0:.002:1>> x = 0:.002:1>> humps(x)>> humps(x)

Page 18: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Function handle..Function handle..• The graph shows that the function has a local minimum near x = 0.6

• The function fminsearch finds the minimizer, the value of x where the function takes on this minimum.

• The first argument to fminsearch is a function handle to the function being minimized and the second argument is a rough guess at the location of the minimum:

• Call the function in Command Window>> p = fminsearch(@humps,.5)• What does p represent?

• To evaluate the function at the minimizer,>> humps(p)What do you get?

Page 19: (MATLAB..A WALK THROUGH) AI (1... · algorithms, enormous data handling abilities, and powerful programming tools. The Matlab is not designed for symbolic computation, but does include

Your job..Your job..SOLVE XOR PROBLEM USING NN IN SOLVE XOR PROBLEM USING NN IN

MATLAB….MATLAB….