matlab by xiaocuisanmin

VIEWS: 0 PAGES: 7

									Hey folks,
This is the equivalent of Professor Drela’s gnuplot tutorial in MATLAB. I’m going to
assume that you know *nothing* about MATLAB, so the document is (ridiculously)
lengthy. But hopefully it will be helpful. AND, after going through this stuff, most of
the coding for the Lab5 will be done!

Also, since I’m the one writing this guide, I will also be the one providing tech-support
for it. Feel free to contact me at ehliu@mit.edu with questions. I am also at most of the
UE office hours too. Additionally, I guarantee that the code provided in this document
works! You have to edit my constants/formulas if Drela has changed anything.

Lastly, if something is wrong with the formatting, you can find the document in .doc and
.rtf form here:
web.mit.edu/ehliu/Public/Spring2007

Getting Started:
You can get MATLAB two ways:
1) Go to matlab.mit.edu. Download it for yourself, read the EULA/etc for instructions.
2) On Athena:
athena% add matlab
athena% matlab &
(The & is there so that you can continue to use the terminal after matlab opens.)
(On Athena, type “desktop” w/o the quotes to open up the graphical user interface if you
don’t feel comfortable with the command line.)

Creating a Script File:
Now at the MATLAB prompt, type “edit” (without the quotes). This should open up
MATLAB’s text editor. From here on out, I will be showing you code that you can paste
into the m-editor window.

Note: From now on, the stuff between --------------------- sequences indicates pieces of
code that you can copy-paste directly into a MATLAB .m script file (don’t copy-paste the
----------- marks!). I don’t know if the numbers I have are the numbers you will use this
year, so don’t blindly copy without first reading.

Additionally, anything that can be saved into a .m script file can also be typed into the
matlab command prompt.

First, let’s see how script files work in MATLAB. Script files are one of two kinds of
code-files you can create with matlab (the other one is .m functions; we will ignore this).
Go ahead and put the following into your m-editor window:
----------------------------start
temp = 1 %stores value 1 into temp and prints the operation to the screen
temp2 = 2; %stores value 2 into temp without printing to the screen
temp3 = 3*temp; %multiples temp by 3 and stores the result into temp3, no print
temp = 2; %stores value 2 into temp, no print
temp3 %prints the value of temp3: note that this is 3, NOT 6
----------------------------end
Now save it. The default location is OK for now. Name it something you can remember,
like maybe “UE_Lab5 .m”. Note: DO NOT USE SPACES IN FILENAMES.

Running the Script File
At the MATLAB prompt, type the name of the file you just saved; it should look like
this:
>>UE_Lab5 .m
Press enter. The following should happen:

temp =

   1

temp3 =

   3

>>
MATLAB executed the commands in your script file.

Also, if you look in the left part of the MATLAB window, you should see a box called
“workspace”. It should have been blank before, but now you should see the two
variables you just created sitting there. Also, if you want to see a list of all the variables
& their sizes in the command window, type “whos”. If you want to see the contents of
the variable, type the variable name—for example: “temp”

NOTE: If you want to delete all the variables, type “clear” at the command prompt. If
you want to delete only a few variables, do: “clear var1 var2 var3 var4” etc (you can
chain on as many as you want).

NOTE: The command “clc” clears the command window of text.

KEY IDEA:
MATLAB .m script files basically ask MATLAB to “type” each line into the command
line one at a time and press “enter” at the end of each line. So script files are convenient
because you can ask matlab to do a lot of things all at once without having to type them
individually! This also means that any line I ask you to copy into the script file can be
typed into the prompt individually.

But also keep in mind that because
temp3 = 3*temp
Computes the value on the right-hand-side and stores it, there is NO PERMANENT
REFERENCE to the variable temp. Hence like I demonstrated, if you change the value
of temp after assigning temp3, temp3 DOES NOT CHANGE.
%Commenting your code
% is the matlab syntax for comments. “Commenting” means telling the computer to
ignore part of the line of code, like so:
%MATLAB will ignore everything on this line
temp = 1; %matlab store 1 in temp, but ignore things after the %-sign
% temp = 2;
%Matlab will not store the value of 2 into the variable temp b/c the line is commented

Also, MATLAB reads comments on a per-line basis. So if you comment line 1, it will
have no effect on line 2. There is no way to block-comment like (/* and */); but you can
highlight a section, and go to Text->Comment to comment out that section.

Storing things in variables
Go ahead and erase the 2 lines in the UE_Lab5 file. Copy in the following lines into the
file:
------------------------------start
%constants
g = 9.81;                % gravity, m/s^2
rho = 1.22;               % air density, kg/m^3
rhof = 32;                % foam density, kg/m^3
E = 19.6e6;                % foam modulus, Pa
CDA0 = 3.0 * 0.0254^2; % non-wing drag area, m^2
cd = 0.028;               % cd in Pmin flight
CL = 1.0;               % CL in Pmin flight
etap = 0.65;              % propeller efficiency
etam = 0.50;               % motor efficiency
lambda = 0.468;                % wing taper ratio
tau = 0.10;               % airfoil t/c
Wrest = 0.235*g;              % non-wing weight, N
-----------------------------end
This code will create the above variables and initialize them to the values used in 2006.

So the syntax here is:
variable_name = value;

Implicit Functions
------------------start
%(implicit) functions
c = @(AR,S) sqrt(S./AR);
b = @(AR,S) sqrt(S.*AR);
vol = @(AR,S) 0.6.*tau.*c(AR,S).^2.*b(AR,S);
Wwing = @(AR,S) rhof.*vol(AR,S).*g;
W = @(AR,S) Wwing(AR,S) + Wrest;
CD= @(AR,S) CDA0./S +cd + CL.^2./(pi.*AR);
P = @(AR,S) 1/(etap*etam) .* sqrt(2.*W(AR,S).^3./(rho.*S)) .* CD(AR,S) ./
sqrt(CL.^3);
dob = @(AR,S) 0.4 * W(AR,S)./(E.*tau.^3) .* ((1+lambda)./2.0).^4 .* AR.^3 ./ S;
------------------------end
NOTE: THE .* ./ and .^ operations are VERY IMPORTANT and VERY
DIFFERENT from just * / and ^. See the bottom of the document (DOT
OPERATOR) for an explanation.

%Syntax here:
function_name = @(var_in1, var_in2, ...) expression;
You can write formal functions in MATLAB like what you may have seen in Java,
Ada95, C, or various other languages. We will ignore that for now. E-mail me if you
really want to know how to use these.

Also, note that you can add more inputs for more design variables. It might look like
this:
%vol = @(AR, S, tau) 0.6.*tau.*c(AR,S).^2.*b(AR,S)

NOTE: I will reiterate this again. If you change any of the constants, like CDA0, YOU
MUST RE-ASSIGN ALL OF THE OTHER VARIABLES AND IMPLICIT
FUNCTIONS THAT INVOLVE CDA0. It is easiest if you simply-run the entire script.

1-D plots (e.g. CL vs CD)
---------------------start
%1D plot:
ARvec = linspace(0.1,15,100);
%forms a vector of 100 uniformly distributed elements, starting at 0.1 and ending at 15
plot(ARvec, P(ARvec, 0.2))
%This will plot P as a function of AR, with S = constant = 0.2

plot(ARvec,P(ARvec,0.2),ARvec,P(ARvec,0.3),ARvec,P(ARvec,0.4))
%This will plot P as a function of AR three times, one for each S value.
%You can chain as many plot inputs as you want.

%The following will make a plot and label it:
figure %create a new plot window
hold on
% hold on tells matlab to do all following operations on the same plot window
plot(ARvec,P(ARvec,0.2),r-) %r- makes a red line
plot(ARvec,P(ARvec,0.3),b-) %b- makes a blue line
%You do not have to chain all the plot commands into one group as long
%as you have hold on
title('AR vs Power');
xlabel('AR');
ylabel('Power');
legend('S=0.2','S=0.3','location','NORTHWEST');
%creates a legend and places it in the NW corner
grid on
%turns on a grid
hold off
%Now further plot/plot-related commands will to the last active graph window
------------------------------------ end

NOTE: Naturally to run the 1-D plot commands, you will need to have run all the
variable assignments and the function assignments in the preceding sections first.

Contour Plots
------------------------------start
[XX YY] = meshgrid(0.1:0.5:15,0.1:0.05:1);
%forms the X and Y axis values needed by matlab to do the following plots.
%Syntax: [X Y] = meshgrid(xmin:stepsize:xmax , ymin:stepsize:ymax)
%So the code I gave you is for AR & S:
%the AR values (stored in XX) will range from 0.1 to 15 in steps of 0.5.
%The S values (stored in YY) will range from 0.1 to 1 in steps of 0.05.

%Contour Plot:
figure
hold on
[tC,th] = contour(XX,YY,P(XX,YY));
set(th,'ShowText','on','TextStep',get(th,'LevelStep')*2,'LevelList',[2 5 8 10 15 20])
%set overlays the labels for P onto the contour plot

[tC,th] = contour(XX,YY,dob(XX,YY));
%the [tC,th] is important so that we can reference the graph opject later

set(th,'ShowText','on','TextStep',get(th,'LevelStep')*2,'LevelList',[0.01 0.05 0.1 0.2])
%the 2nd set command overlays the labels for dob onto the contour plot.

hold off
------------------------------ end
You CANNOT chain contour commands like you can plot commands. Each must be
entered separately.

You can modify the stuff in [ ] after LevelList to tell MATLAB to plot different level
curve values.

Note: if you’re plotting functions with more than 2 inputs, remember to put in some value
for the third variable. Suppose I have P(AR,S,tau) and I want to plot it on AR-S axes. I
would use the above syntax, except for this:
%contour(XX,YY,P(XX,YY,0.1))
where 0.1 is my tau value. I can also create a new set of XX YY to graph on S-tau axes,
or whatever you want.
Surface Plotting:
-------------------start
%Assuming that you have already executed the meshgrid command...
%Surface Plot:
figure %opens a new graph window
surfc(XX,YY,P(XX,YY));
%this draws the surface plot of P with the contour lines drawn in the P=0 plane
--------------end

You CANNOT chain surfc commands like you can plot commands. Each must be
entered separately.

As far as I can tell, surfc will not display the level curve values. I haven’t looked that
hard because I don’t see a need for the 3D-surface.

The DOT OPERATOR
Some more KEY things to know:
MATLAB functions can accept scalar OR vector inputs. But the syntax varies a bit for
doing math operations on them.
If you expect the value to be a vector and you are doing *, / or ^, you need a . (period)
before the math symbol. This is not required for scalars, but it won’t break the thing
either. (So to be safe, put a . on every *, /, and ^ if you don’t feel comfortable.)

Note that because MESHGRID creates matrices, when you call
contour(XX,YY,P(XX,YY)), MATLAB will be passing NON-SCALAR arguments to
P(). So don’t forget the dot operators!

It works like this:
----------------------start
X = linspace(1,10,100); %creates a vector of 100 elements, starting at 1 and ending at 10
Y = linspace(5, 20, 100); %see above
X.*Y %multiplies each element of Y by the corresponding element of X; output is a
vector. So answer(i)=X(i)*Y(i)
X.^Y %see above
X./Y %see above
X*Y %Attempts to do a vector multiply on a 1x100 vector times a 1x100 vector, so you
get dimension mismatch.
X*Y’ %Calculates X*Ytranspose--this is the dot (inner) product
X’*Y %Calculates Xtranspose*Y--this is the outer product...forms a matrix.
-------------------end

Matlab can also handle matrices...we aren’t going to worry about that either.

Saving the Script File Somewhere Else
For now, I have asked you to save the script-file in the default location. We can actually
put it somewhere else that is more convenient for you/your group to access (e.g. Public
folder).

When you do a “save-as”, obviously you can save it wherever you want. BUT to make
MATLAB look in that folder, you have to do one of two things:
1) At the matlab prompt, using the “cd” command (like in linux, DOS, etc), navigate to
the folder where you saved the file. There is also a drop-down menu for “current
directory”; you can change this instead if you want.
2) Go to File->Set Path, click “Add Folder” and add the folder that your file is saved in.
Click “Save” and “Close”.

I think this covers all the bases.

Good Luck,
-Eric Liu

P.S. Admittedly, getting started in MATLAB is a bit harder than getting started in
gnuplot, especially for this segment of the lab. But MATLAB has a lot more cool stuff
built in if you have the patience to learn (like doing constrained optimization later and
many other course XVI classes—16.06, 16.07, 16.100, 16.30, 16.901, etc).

								
To top