Matlab Application - Bezier Curve

Let us start with the Bezier Application we built in VB.   We are going to explore many aspects of Matlab with this example.  We are going to write a Matlab script file to do it.  Previously we have used Matlab interactively.  Instead we are going to place all the commands in a file ( -the script file) and run the file in Matlab.  This way we can interactively use Matlab by executing a bunch of statements rather than one at an time.  Also at any time we can return to the interactive mode by typing in the Matlab workspace/window
 

Creating Script File

We create the file using the Matlab editor

    In Matlab window click on the New M-File toolbar button.  The Matlab editor should appear.  This is also a useful ASCII editor ( if you don't like notepad) - but its color coding refers to-built in Matlab commands

    Now we can type directly in the editor

% Bezier cubic curve
%
% This file will create a Bezier cubic curve and dispay the plot
% There are two reasons for including comments in the front.
% If you are working with a lot of programs then this will tell you what
% the file is supposed to accomplish.
% Second, if you type help filename ( without the  .m extension) then these
% comments are displayed in the workspace

BCon3 = [-1 3 -3 1; 3 -6 3 0; -3 3 0 0 ; 1 0 0 0 ]; % our 4 X 4 constant Matrix

BezVert3 = [ 0 0 ; 0.3 0.5 ; 0.5 0.7 ; 1.0 1.0];  % the vertices

% Don't you think it will be more useful if the user can key
% in the values at the prompt. We will do it later

for i = 1:1:50          % for loop 1 - 50 insteps of 1
   par = (i - 1)/49;
   XY(i,:) = [par^3 par^2 par 1]*BCon3*BezVert3;           % yep! we have created our data
end

    Save this file (in your directory) - for ex:    Bscript1.m

    Since the script file is in your directory and Matlab doesn't know about it we inform Matlab where to find the file by

>> addpath     C:\EMEM899\Somedirectory
    Run the script by typing the file name without the extension
>> Bscript1
    Since we have used the semicolon to suppress printing check out the value of XY by
>> XY
    At this time we have the option of continuing to work interactively by typing the following code in the Matlab window or to add the code to the file in the editor.  Lets do the latter
% we will display the vertices and the curve using
% Matlabs built-in graphic functions
clf    % this will clear the figure

plot(BezVert3(:,1),BezVert3(:,2),'ro',XY(:,1),XY(:,2),'b-')
% this will create a plot of both the Vertices and curve
% the vertices will be red while the curve is blue line
% if you are impatient you can save the file and run the script agin in Matlab

xlabel(' x value')
ylabel ('y value')
title('Cubic Bezier Curve')
legend('Vertex','Curve')   % you can move the legend on the plot
 

A screen shot of the plot:

    At this time we have generated and plotted the Bezier curve.  What is missing is the user interface.  We will design this later as the project in this segment of the course.  Meanwhile Matlab will probably be the most useful software for general engineering problems

So we will explore some useful numerical tools that come with Matlab