Introduction to Matlab 7 Lesson III - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Introduction to Matlab 7 Lesson III

Description:

Institut f r Chemie und Bioingenieurwissenschaften ... Arial, Times, Courier, etc... FontSize. Size of Font. Scalar. Default: 10. FontWeight ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 25
Provided by: alessand64
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Matlab 7 Lesson III


1
Introduction to Matlab 7Lesson III
  • Marco Lattuada
  • Swiss Federal Institute of Technology - ETH
  • Institut für Chemie und Bioingenieurwissenschaften
  • ETH Hönggerberg/ HCI F135 Zürich (Switzerland)
  • E-mail lattuada_at_chem.ethz.ch
  • http//www.morbidelli-group.ethz.ch/education/inde
    x

2
Short Resume
  • Decisions
  • Loops

if expression statements elseif
expression statements else expression statement
s end
switch test case result1 statements case
result2 statements otherwise statements end
for variable expression statements end
while expression statements end
3
M-Files
  • What is an M-File?
  • An M-File can be used to
  • Types of M-Files

An M-File is a collection of commands. It is
equivalent of programs, functions, subroutines,
prodecures in other programming languages.
  • Experiment an algorithm
  • Create a perment record of an algorithm
  • Built utilities
  • Script M-File no input and no output. Operates
    on workspace variables.
  • Function M-File contains a function command,
    accepts input variables and may delivers outputs.
    Other variables are local.

4
Example of Script
  • How to run a script
  • From the Matlab Command Window
  • (Double-check the directory path...)
  • From the Matlab Editor
  • In Matlab Editor ? Debug ? run

5
Exercise
  • Create a new M-File called 'solve_triangular'
  • Write down the solution of the following
    triangular system
  • Solution

6
A clarification about matrix operations
  • Suppose that A is an NxN matrix, and b is an Nx1
    column vector
  • The following divisions are allowed in Matlab
  • XA\b Solution of AXb (X is Nx1)
  • Yb/A gt Yb (A)-1 Solution of YAb (Y is
    1xN)
  • ZA/b gt Best solution of ZbA There is no
    exact solution for this problem Matlab finds
    least square solution
  • ZbA
  • (Z is Nx1)

7
Functions in Matlab
  • Function definition (in the M-File)
  • Function handling from Matlab Command Window

By pressing fTAB, all the functions are
recalled
8
Example of Function
  • The M-File the "mysum" function (saved in
    mysum.m)
  • The call

The workspace at the end
9
Exercise
  • Create a new function called 'my_first_function'
  • Scope of the function
  • Compute the function f(x) 2x2-log(x)3
  • Parameters of the function
  • Input the x value for which the function must
    be evaluated
  • Output the value of the function in x (that is,
    f(x))
  • Run the function for different values of x
  • Evaluate the function for x linspace(1,10,10)
  • If x is less or equal than zero, then return the
    value f -1, otherwise compute the correct value
    f(x)
  • Re-evaluate the function for x
    linspace(0,10,11)
  • How would you rewrite the function so that it
    evaluates f(x) for all the components of a
    generic vector x?
  • Plot f(x) versus x (use plot(x,y))

10
Exercise
  • Create a new function called 'my_second_function'
  • Scope of the function
  • Compute the function f(x) 2x2-exp(x)3
  • Parameters of the function
  • Input a generic array x with n components
  • Output an array n?1 with the values of the
    function in x
  • Evaluate the function for 40 x values ranging
    from -5 to 5
  • Plot f(x) versus x

11
View variables inside functions (debugging)
  • Use the command keyboard
  • Every time the function is called, it will stop
    the execution when reaching the keyboard command
  • Type the name of the variables you want to see
  • Use return to exit form the keyboard mode

12
Local vs. Global Variables
  • All variables inside functions are local
    variables.
  • This means that they exist only inside the
    function.
  • If one wants to create a variable which is
    visible everywhere, it has to be declared as
    global
  • The global declaration has to be done in the main
    program and in all functions where the variable
    should be visible

The value of alpha has changed!
13
Exercise
  • Plot the result of 'my_first_function' vs
    -50.55
  • Type hold on
  • Plot the result of 'my_second_function' vs
    -50.55 using the command plot(x,f,'ro')
  • Check the actual axis interval by typing axis
  • Define a new axis interval by typing axis(-3 3
    -10 20)
  • Introduce the labels by xlabel('x'),
    ylabel('f(x)')
  • Superimpose the function 3sin(x) plot(x,
    3sin(x),'g--')
  • Create a new figure figure(2)
  • Plot the function 2exp(x21)6 using the command
    semilogy instead of plot
  • Place axis labels and rescale the plot to x
    -22 and y 10...1000

14
More on 2D plots
  • To plot data in a Cartesian plane, the command is
    plot(x,y)
  • Try
  • gtgtxlinspace(0,10,100)
  • gtgtytanh(x).cos(x)
  • gtgtplot(x,y)
  • In order to specify line thickness, type of line,
    color, etc.
  • gtgtplot(x,y,g,LineWidth,3)

Color
Line Width (default 0.5)
Line Style
15
Plot Details
  • Colors Line Styles

16
Plots of data
  • Try
  • gtgtx110
  • gtgtysin(x)
  • gtgtplot(x,y)
  • Plot by default draws a line between two
    successive points
  • gtgtplot(x,y,k-d,MarkerSize,10,MarkerEdgeColor
    ,b,Marker
  • FaceColor,y)

Color of Marker Edge (default line color)
Marker Size
Marker Type
Color of Marker Face (default white)
17
Markers

18
  • gtgtxlinspace(0,10,100)
  • gtgtysin(x)
  • gtgty2cos(x)
  • To plot multiple data in the same figure
  • gtgtplot(x,y,-o,x,y2,d)
  • Alternatively
  • gtgt plot(x,y,-o)
  • gtgthold on
  • gtgtplot(x,y2,--d)
  • gtgthold off

Hold on keeps the graph open. Additional data
will be plotted in the same figure!
19
Formatting a Plot
  • gtgtxlabel(x)
  • gtgtylabel(y)
  • gtgttext(xt,yt,Text here) adds text at
    coordinates xt,yt
  • gtgtlegend(this is a legend)
  • gtgttext(xt,yt,Text here,PropertyName,PropertyValu
    e)
  • gtgtaxis(xmin,xmax,ymin,ymax) specifies the
    interval where the plot should be visualized

20
Text Properties
21
Additional types of plot
  • gtgtfplot(8xcos(x.2),1,10) plot a
    function in an interval
  • gtgt x,yfplot(8xcos(x.2),1,10) no
    plot, just returns x,y
  • gtgtsemilogx(x,y)
  • gtgtsemilogy(x,y)
  • gtgtloglog(x,y)
  • gtgtthetalinspace(0,2pi,100)
  • gtgtr1./(1theta.2)
  • gtgtpolar(theta,r) plot in polar coordinates

Interval
Function
Like plot, but with either x, or y or both axes
in logarithmic scale
22
Plots with Error Bars
  • gtgtxlinspace(1,10,20)
  • gtgty10./x
  • gtgt errandn(20,1)
  • gtgterrorbar(x,y,er)

er is the array containing the error on the
data The error bar is drawn from y-er to yer
23
3D plots I- lines in space
  • gtgttlinspace(0,2pi,100)
  • gtgtxsqrt(t).cos(2t)
  • gtgtysqrt(t).sin(2t)
  • gtgtzt
  • gtgtplot3(x,y,z)

Lines can be manipulated as done with plot
24
3D-plots II
  • gtgtxlinspace(-5,5,30)
  • gtgtylinspace(-2,2,20)
  • gtgtX,Ymeshgrid(x,y)
  • gtgtZsin(X).cos(Y)
  • gtgtmesh(X,Y,Z)
  • gtgtsurf(X,Y,Z)
  • gtgtshading interp
  • gtgtcontour(X,Y,Z) like looking at mountain
    heights on a map
  • gtgtcolorbar
  • gtgtcontour3(X,Y,Z)
  • gtgtcolorbar
Write a Comment
User Comments (0)
About PowerShow.com