From Wikiversity
Jump to navigation Jump to search

How to use this code[edit | edit source]

  1. Make a folder called MatlabSnippets
  2. Copy and paste the code listed in ==plot2D== in to a filename.m file.
  3. Read and modify your copy as you wish
  4. To test a code snippet either run the code and select a section number or copy and paste the snippet into your own code.
  • If you are using Matlab, it helps to maximize the command window. (The code also works with Octave)

Plotting resources[edit | edit source]

plot2D[edit | edit source]

%%%Basic graphing in MATLAB%%%%
clc;clear all;close all; %Always begin with these statements
fprintf('This code is divided into sections (1,2,3 ...)\n' );
%*fprintf* prints to command window. /n makes new line.
sectionNumber= input('Enter the section you wish to run: ');
%*input* prompts user to input an integer (1, 2, 3, ...)
% The code will run only that section.
    %*switch* executes only the part of the code identified
    % by one or more *case* statement. Each the code for each case
    % statement may be copied and pasted as a stand-alone code
    case 1 
        fprintf('Plot with optional title and axis labels \n');
        x = linspace(-1,2); %creates 100 points between -1 and 2
        y = x.^2+1; %Note the . before the ^, needed because x is an array
        plot(x,y);  %Creates line plot
        title('This is case 1'); %This line is optional
        xlabel('the x axis (obviously)') %This line is also optional
        ylabel('and this is the y axis')%This line is also optional
        % If you want more or fewer than 100 points replace linspace(a,b)
        % by linspace(a,b,n), for example linspace(-1,2,500) would have
        % created a finer plot with 500 points.
        % To make a scatter plot use plot(x,y,'*') or plot(x,y,'+')
        % to get different marker styles (many other marker options
        % are possible).
    case 2 
       fprintf(' plot two items on one graph \n');
        x = -2:0.1:2; %Plot points between -2 and +2 incremented by 0.1
        yA = x.^2; %The . before the ^ is needed because x is an array
        yB = x.^3; %The second set to be ploted
        grid; %puts a grid on the final graph
    case 3 
        fprintf('Three functions plotted on two graphs \n');
        %two plot sinusoials of different frequency on one graph
        t =0:0.001:2; %Observe the wave for two seconds
        fprintf('If the frequency is larger than 20 there might ');
        fprintf('be too few data points per oscillation \n'); %\n is linefeed
        f1=input('Enter the frequency of the first wave: ');
        f2=input('Enter the frequency of the second wave: ');
        omega1=2*pi*f1; % angular frequency is easier to plot
        psi1=sin(omega1*t);%psi is a common symbol for wave amplitude
        figure % Is this needed?
        subplot(2,1,1) % (2 rows, 1 column, plot 1)
        plot(t,psi1,t,psi2);%plots both
        subplot(2,1,2) % (2 rows, 1 column, plot 2)
        plot(t,psi1+psi2);%plots both
    otherwise % executes if no case includes *sectionNumber*
        fprintf('No such section exists\n' );
fprintf('To run this code again type *plot2D*\n' );