12.07.2015 Views

II.1 Programming in MATLAB Getting Started Graphing

II.1 Programming in MATLAB Getting Started Graphing

II.1 Programming in MATLAB Getting Started Graphing

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

function [F,NA] = tfunction(beta,theta)%% tfunction.m%% This m-file is a template for a function.%% In this template, the function tfunction f<strong>in</strong>ds the friction force F and the normal% force NA.% The <strong>in</strong>puts to the function are the variables beta (the angle of the bar) and theta% (the angle of the <strong>in</strong>cl<strong>in</strong>e). These two <strong>in</strong>puts are <strong>in</strong> radians.% The outputs to the function are F and NA.% Notice the syntax for the function. The output variables are surrounded by brackets,% the <strong>in</strong>put variables are surrounded by parentheses, and the name of the function is% the same as the name of the file.%% This template shows:%% - the syntax for a function%W = 100;L = 2;b = [W,0,W*L/2*cos(beta)]';A = [0,1,cos(theta);1,0,-s<strong>in</strong>(theta);0,0,L*cos(theta-beta)];a<strong>in</strong>v = <strong>in</strong>v(A);x = a<strong>in</strong>v*b;F = x(1);NA = x(2);Table 3: Template for FunctionsThe M-file tfunction.m can also be called from with<strong>in</strong>another program. So can the M-files test1 and test2. Toillustrate this, create the M-file named test2.m and type <strong>in</strong>Table 4. This program determ<strong>in</strong>es the critical angle beta of thebar for which the bar rema<strong>in</strong>s <strong>in</strong> static equilibrium but belowwhich the bar slides (See Fig. A2 – 2). The program test2.mcalls the function tfunction.m <strong>in</strong> a loop, sweep<strong>in</strong>g over a rangeof values of beta between 0 and theta. Each time the function iscalled, the program compares the value of the friction forcewith the maximum allowable level of friction, F max = µN A , tosee if it’s been exceeded. To run test2.m, go to the commandw<strong>in</strong>dow, type <strong>in</strong>test2and hit enter. The command w<strong>in</strong>dow displaysbetacritical = 26.55, which is the critical angle beta<strong>in</strong> degrees, and a figure w<strong>in</strong>dow appears show<strong>in</strong>g two graphs.One graph is of the friction force F versus beta and the secondgraph is of the maximum friction force (above which the barslides) versus beta.Figure A2 – 2: A bar rests on a rough surface at oneend and on a smooth <strong>in</strong>cl<strong>in</strong>ed surface at the other end.7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!