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 workspaceBCon3 = [-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 laterfor 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\SomedirectoryRun the script by typing the file name without the extension
>> Bscript1Since we have used the semicolon to suppress printing check out the value of XY by
>> XYAt 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 figureplot(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 o while the curve is blue line
% if you are impatient you can save the file and run the script agin in Matlabxlabel(' 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