scientific programming using matlab fall 2010 2011 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Scientific Programming Using MATLAB, Fall 2010-2011 PowerPoint Presentation
Download Presentation
Scientific Programming Using MATLAB, Fall 2010-2011

Loading in 2 Seconds...

play fullscreen
1 / 25

Scientific Programming Using MATLAB, Fall 2010-2011 - PowerPoint PPT Presentation


  • 91 Views
  • Uploaded on

Scientific Programming Using MATLAB, Fall 2010-2011. TIRGUL #5: Graphics & Debugging. Simple 2D graphics. x = [0:0.1:10]; y = sin(x); >> plot (x, y) x, y need to have the same length If x is undefined, it defaults as the indices of y. Plotting styles.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Scientific Programming Using MATLAB, Fall 2010-2011' - hedda


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
simple 2d graphics
Simple 2D graphics
  • x = [0:0.1:10];

y = sin(x);

>> plot(x, y)

  • x, y need to have

the same length

  • If x is undefined, it defaults as the indices of y
plotting styles
Plotting styles
  • ‘plot’ by default draws a blue line, but you can draw the actual data points as well
    • Plot(x,y, ‘color-style’)
      • plot(x,y, 'mp')
styling options
Styling options
  • (This is taken from the highly recommended MATLAB tutorial in Hebrew, that can be found on the course website)
figure window
Figure window
  • When creating a new plot, MATLAB opens a figure window
    • Some plotting functions don’t do this automatically, and the figure needs to be defined: figure(#figure)
      • example : figure(2)
hold on hold off
hold on / hold off
  • Unless told otherwise, any new plot created overrides the last one
    • Define a new ‘figure’ window for your new plot
  • If you want both plots in the same figure, use ‘hold on’ between plotting commands
    • x = [0:0.1:10];

y = sin(x);

holdon

plot(x, y)

plot(x,-y, 'mp')

hold off

figure handling functions
Figure-handling functions
  • clf – clear figure
  • close
    • Default closes “current figure” – last figure that was changed
    • You can specify the figure to be closed
      • close(2)  closes figure 2
    • close all  closes all figures
other graph properties
Other graph properties
  • axis([xminxmaxyminymax])
  • title(‘string’, propertyName, propertyValue)
  • legend('string1','string2',...)
  • xlabel
  • ylabel
  • grid
  • text
  • Example:
    • title('My Figure Title', … 'fontsize', 16)

legend('-sin(x)', 'sin(x)')

xlabel('My x-label', … 'fontsize', 14)

ylabel('My y-label', … 'fontsize', 14)

grid

gtext(‘my floating text’)

text properties
Text properties
  • Functions dealing with text can take properties as inputs
    • (title, xlabel, ylabel, text etc.)
  • Syntax:
    • functionName(‘string’, ‘propertyName’, ‘propertyValue’)
  • Useful properties:
    • FontSize, FontName, Color, FontWeight
      • Other properties can be found at help text property list
text special cases
Text – special cases
  • Subscript letters/digits – use underscore (_)
    • An underscore is required before each letter/digit.
  • Superscript letters/digits – use ^
  • Greek letters – use backslash and the name of the letter
  • Example:
    • text('\alpha \beta under_s_c_o_r_e pow^e^r', 'fontsize', 16)
figure editor
Figure editor
  • Tempting to use, but try not to
    • you will probably need to output the figure many more times – code will create the desired figure automatically.
        • File  generate m-file
sub plotting
Sub-plotting
  • subplot(num_rows, num_columns, plot_index)
    • Figure(1)

subplot(2, 2, 1)

plot(x, y)

title('sinus', 'fontsize', 12)

subplot(2, 2, 2)

plot(x,-y, 'mp')

title('minus sinus', …

'fontsize', 12)

subplot(2, 2, [3 4])

plot(x, y)

hold on

plot(x,-y, 'mp')

title('both', 'fontsize', 12)

suptitle('Sub-plotting, example')

hist and bar
‘hist’ and ‘bar’
  • We want: to display distributions
  • 2 steps:
    • Calculate distribution – how many cases in each bin
    • Plot results
  • hist(data, nBins)
  • Or:
    • distribution = hist(data, nBins)
    • bar(distribution)
using bar with matrices
Using ‘bar’ with matrices
  • bar(Y): if Y is a matrix, bar groups the bars produced by the elements in each row
  • champMat:
    • bar(champMat')
    • legend('Russia', 'Canada', 'Bulgaria', 'Albania', 'USA')
    • colormap winter
other 2d plotting functions
Other 2D plotting functions
  • pie
  • scatter
    • gscatter (group-scatter)
  • errorbar
saving figures
Saving figures
  • File  save
    • Saves as ‘.fig’ file
      • Can later change figure properties
      • A heavy, ‘original’, version of the figure
  • File  save as
    • ‘.png’, for example
      • A light-weight version of the figure
      • Good for the final version of your figure
  • Copy-paste
plotting help
Plotting help
  • help graph2d
  • Examples:
    • exampleGraphics2D.m
3d graphics
3D graphics
  • Standard plot functions with an extra dimension
    • Plot3
    • Scatter3
    • Bar3
    • Pie3
  • Color-coding
    • Image
    • Imagesc
  • 4D graphics?  3D+color
  • Example:
    • exampleGraphics3D.m
debugging
Debugging
  • Bugs
    • Syntax errors
      • no problem, MATLAB errors
    • Illegal outputs – under some conditions the expression is illegal
      • More difficult to handle, but still manageable
    • Wrong output
      • Your worst nightmare
handling bugs
Handling bugs
  • Syntax errors
  • Illegal outputs – under some conditions the expression is illegal
    • Test your program with all input types and values you can think about
    • Take care of edge cases
  • Wrong output
    • Look at the outputs you get – do they make sense?
slide22
lint
  • Runs in your editor at all times automatically
  • Shows colored problem-messages :
    • Critical error 
    • Possible error 
  • Marks the place of the bug
  • Gives a description of the problem
debug mode
Debug mode
  • Put breakpoints ( ) in strategic places
    • A gray circle ( )– syntax error/ need to save
  • Press ‘run’

Step in

Step out

Set/clear breakpoint

Exit debug

clear all breakpoints

step

Run/continue

slide24
Example:
    • plotMyParabola.m
    • plotParabolasScript.m
practice
Practice
  • In this practice, pay special attention to ‘lint’ messages and use the debug mode to get comfortable about working with breakpoints
  • Create 2 row vectors of the same size. Plot them one versus the other.
  • Create another vector of the same size. Plot it versus the first vector on the same graph (use ‘hold on’).
  • Create a data vector of 50 random grades between 0 and 100.
  • Create a new figure and plot a histogram of the grades.
  • Plot a histogram of the grades again, this time divide the data into 5 bins.
  • Create a new figure and plot a pie chart of the distribution.