An interactive introduction to MATLAB

(Jeff_L) #1

34 scripts and functions


Listing 3.1: my_surf.m - Script to plot a surface
1 % my_surf.m
2 % Script to plot a surface
3 %
4 % Craig Warren, 08/07/2010

6 % Variable dictionary
7 % x,y Vectors of ranges used to plot function z
8 % a,c Coefficients used in function z
9 % xx,yy Matrices generated by meshgrid to define points on grid
10 % z Definition of function to plot

12 clear all; % Clear all variables from workspace
13 clc; % Clear command window

15 x = linspace(−1,1,50); % Create vector x
16 y = x; % Create vector y
17 a = 3;
18 c = 0.5;
19 [xx,yy] = meshgrid(x,y); % Generate xx & yy arrays for plotting
20 z = c*sin(2*pi*a*sqrt(xx.^2+yy.^2)); % Calculate z (function to plot)
21 surf(xx,yy,z), xlabel('x'), ylabel('y'), zlabel('z'), ...
22 title('f(x,y)=csin(2\pia\surd(x^2+y^2))') % Plots filled−in surface

Comments:


  • It is extremely useful, for both yourself and others, to put comments in
    your script files. A comment is always preceded with a percent sign (%)
    which tellsMATLABnot to execute the rest of the line as a command.

  • Script file names MUST NOT contain spaces (replace a space with the
    underscore), start with a number, be names of built-in functions, or be
    variable names.

  • It is a good idea to use theclear alland clccommands as the
    first commands in your script to clear any existing variables from the
    MATLABworkspace and clear up the Command Window before you
    begin.

Free download pdf