matlab.spring11

# matlab.spring11 - INTRODUCTORY TUTORIAL TO MATLAB PREPARED...

This preview shows pages 1–4. Sign up to view the full content.

1 ME 352 – Spring 2011 I NTRODUCTORY T UTORIAL TO M ATLAB PREPARED FOR: ME 352: MACHINE DESIGN I Spring Semester 2011 School of Mechanical Engineering Purdue University West Lafayette, Indiana 47907-2088 January 10th, 2011 Table of Contents P a g e s Running from the Command Window . ..................................................................... 2 Running from an M-File . .............................................................................................. 2 Some Matlab Basics . ...................................................................................................... 3 Some Elementary Matlab Commands . ...................................................................... 5 M-File Example: A Slider-Crank Linkage . ................................................................ 9 Debugging and Editing M-files . ................................................................................. 10 Your lab TA will provide web links for additional information on Matlab. Additional information on Matlab can be downloaded from the website: www.mathworks.com

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 ME 352 – Spring 2011 Running from the Command Window > x = 2 > y = 3 > z = x*y > z = y*sin(1.5*x) > x = [1,2,3] > A = [2,1,0;1,2,1;0,1,1] > y = A*x > x = x’ > y = A*x > b = [1;2;3] > lookfor determinant > help det > y(1) = det([b(1),A(1,2);b(2),A(2,2)]) > y(1) = det([b,A(:,1)]) > whos > clear > whos > x = (0:0.01:1)*2*pi > x = (0:0.01:1)*2*pi; > y = sin(x); > plot (x,y) > z = sin(2*x); > plot(x,y,x,z) > xlabel(‘this is the x-axis’) > ylabel(‘this is the y-axis’) > gtext(‘y=sin(x)’) > gtext(‘z=sin(2*x)’) > b = [x;y;z]; > fid = fopen(‘stuff.txt’,’w’); > fprintf(fid,’%12.0f,%12.3f,%12.3f\n’,b); > fclose(fid) Running from an M-File - Open a new file under the File menu (choose “M-file”). - Enter the Matlab commands in the same way that you would through the Command window. For example: clear for k = 1:10 x(k) = k; y(k) = k^2; end
3 ME 352 – Spring 2011 plot(x,y) - Save the file on your tools directory. Matlab will automatically append a “.m” extension at the end of whatever name that you give to the file. For example, if the file is to be named “main”, then Matlab will save the file as “main.m”. - Return to the Command window. - While in the Command window, move to the directory containing your saved file. - To run the file, simply type the name of the file (without the ‘.m” extension). Some Matlab Basics The following are some basic things about Matlab that you will need to know before using the application. The list is by no means complete. You will continually learn new things every time that you use Matlab. (a) Command window. The Command window is the window that first appears when you first get onto Matlab. This is the window within which you will be doing all your Matlab operations. Think of this window as being a calculator, a calculator that will allow you do perform simple arithmetic operations, scientific calculations, plotting, exporting/ importing data to/from M-files, etc. (b) Script files (M-files). If the amount of instructions given to Matlab become long and involved and will be performed repeatedly, you will find it convenient to place your instructions in a script file (known as an M-file) that can be executed as a program. • to create a new M-file , open a new file under the File menu. Within the Matlab Editor, type your Matlab commands and the save the file on your tools directory.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 33

matlab.spring11 - INTRODUCTORY TUTORIAL TO MATLAB PREPARED...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online