Beruflich Dokumente
Kultur Dokumente
edit test.m
clear
W = 100;
theta = pi/6;
F = W*sin(theta)
Save and close the M-file and then go back to the command
window. To run this program type
test1
MATLAB is a popular programming language that was
developed for technical computing. This section shows you and hit enter. The program will display F = 50.00 in the
how to use MATLAB. You’ll be given three representative command window. To see which variables are defined in the
MATLAB computer programs. As you look through the three workspace, type
programs, you’ll see what syntax is used. The first program
shows how to graph. The second program shows how to use who
arrays. The third program shows how to create functions.
and hit enter. The variables F, W, and theta will be listed. To
see what programs you have in your current directory, type
Getting Started
what
When you start MATLAB, the MATLAB desktop shown in
Fig. A2 – 1 appears. Let’s begin by using the command and hit enter. You’ll only see test1.m listed because it’s the only
window in the desktop. In the command window, type program you’ve created so far.
theta = pi/6;
and hit enter. This time, MATLAB did not print the result of
the command because of the semicolon you placed at the end of
the instruction. Next type
F = W*sin(theta)
1
and hit return. A figure window with the graphs of the cosine
function and the sine function will appear. Notice that graphing
has a lot of optional features. You have the option to label your
axes, create a title for the graph, set the range of the horizontal
and vertical axes, put in a grid, graph more than one function in
the same graphing window, etc. Before proceeding, you may
want to take a moment and experiment with the program to try
out its different features.
2
%
% tgraph.m
%
% This m-file is a template for creating graphs of functions. It contains many of the
% commands that are commonly used in graphing.
%
% In this template, the graphs of the sine function and the cosine function are created
% in one figure window.
%
% This template shows:
%
% - several ways of plotting
% - how to create a grid in a figure window
% - how to label axes
% - how to create a title for a figure window
% - how to create a legend when more than one plot is drawn in a figure window
%
3
Arrays
Next, look over the second template shown in Table 2.
This program determines the force reactions acting on the bar
shown in Fig. A2 – 2. The reactions are found by solving a set
of three linear algebraic equations. The unknowns in this
problem are
⎛ F ⎞
⎜ ⎟
x = ⎜NA⎟,
⎜N ⎟
⎝ B⎠
4
%
% tarray.m
%
% This m-file is a template for creating arrays. It contains many of the
% commands that are commonly used for arrays.
%
% In this template, a set of linear algebraic equations is solved.
%
% This template shows:
%
% - several ways to input a vector
% - how to transpose a vector and a matrix
% - how to invert a matrix
% - how to swap a column in a vector with another column vector
% - how to multiply vectors and matrices
% - how to perform a for-end loop more efficiently
%
clear
W = 100;
L = 2;
theta = pi/4;
beta = pi/6;
b = [W,0,W*L/2*cos(beta)]' % An array is defined by enclosing the elements in
% brackets, separating them by commas.
% This creates a row vector. The prime following the
% right bracket transposes the row vector, making it a
% column vector.
%
% b = [W;0;W*L/2*cos(beta)] % This is another way to define a column vector. Each row
% (the rows are one element long) is separated by a
% semicolon.
A = [0,1,cos(theta);1,0,-sin(theta);0,0,L*cos(theta-beta)] %A matrix is defined by
% listing its elements row by row. Each
% row is separated by a semicolon.
ainv = inv(A); % inv inverts a matrix.
% detofA = det(A) % det takes the determinant of a matrix.
% Atranspose = A' % a prime after a matrix transposes the matrix.
% A(:,2) = [10,10,10]' % Using the colon, the second row of A is replaced with
% the column vector [10,10,10]'.
x = ainv*b % multiplication/addition of vectors and matrices uses
% the same syntax that is used for scalars.
%time1 = [0:1:2*pi] % Instead of using a for-end loop. The following syntax
%can be used. This instruction creates a row vector of
% values starting from 0, stepping in increments of
% 1,ending just before 2pi. The loop is created using the
% brackets.
%time2 = [0:2*pi] % The default increment is 1.
%c = cos(time1) % Creates a row vector called c. The array is created
% using the parentheses, inside which the array time1
% appears.
5
To try out a few other vector operations, type in
clear
a = [1, 2, 3]
b = [2, 3, 4]
and then type in each of the following quantities and hit enter:
Functions
The third and final template shows you how to create
functions. This will help you become more efficient in
programming. Create the M-file called tfunction.m and type in
Table 3. The M-file tfunction.m turns the force reactions in the
second template into a function of beta and theta. In the
command window, type in
[F,NA]=tfunction(pi/6,pi/4)
6
function [F,NA] = tfunction(beta,theta)
%
% tfunction.m
%
% This m-file is a template for a function.
%
% In this template, the function tfunction finds the friction force F and the normal
% force NA.
% The inputs to the function are the variables beta (the angle of the bar) and theta
% (the angle of the incline). These two inputs are in radians.
% The outputs to the function are F and NA.
% Notice the syntax for the function. The output variables are surrounded by brackets,
% the input 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,-sin(theta);0,0,L*cos(theta-beta)];
ainv = inv(A);
x = ainv*b;
F = x(1);
NA = x(2);
test2
7
clear
mu = 0.5;
theta = pi/4;
error = 100;
betamax = 0;
for i = 1:101
beta = (i-1)*theta/100;
[F,NA]=tfunction(beta,theta); % The function tfunction is called.
x(i) = beta*180/pi; % Notice that x in test2.m is
% different than x in tfunction.m.
% Quantities are not passed
% between programs.
y(i) = F;
FMAX(i)=mu*NA;
er = abs(F-FMAX(i));
Table 4: Test2.m
help tfunction
and hit enter. The comments that you placed at the beginning of
the M-file tfunction will appear in the command window, just
like any of the other MATLAB commands that are built-in.
Key Terms
Arrays, Command Window, Functions, Graphing,
MATLAB Desktop, M-file, Workspace
8
Key Commands Problems
clc, clear, edit, what, who A2 – 1
A W = 50 lb block rests on a rough surface when the friction
force F acting on the block is less than µN, in which N denotes
Review Questions the normal force acting on the block by the surface and µ = 0.5
1. What is the MATLAB desktop?
is the static friction coefficient. When F exceeds 0.5N, the
2. Describe the purpose of a semicolon placed at the end of an
block begins to slide. The friction force F and the normal force
instruction.
N depend on the incline angle θ by F = 50cosθ and N = 50sinθ.
3. What command erases what’s in the command window?
4. What command clears all of the variables in the workspace? Plot the functions F(θ) and µN(θ) on the same graph letting θ
5. Describe the purpose of the commands who and what. range between 0 and 90○. Determine from the graph the largest
angle θmax for which the block remains at rest.
Answer: θmax = 26.6○ (Note that θmax can also be found
analytically.)
A2 – 2
A triangular truss is acted upon by a P = 500 lb load, as shown.
The load causes a tensile force of FAB = P/sinθ in member AB
and a compressive force of FAC = Pcosθ /sinθ in member AC.
Plot FAB + FAC versus θ. For what angle θ is FAB + FAC
minimal?
Answer: θmin = 60○ (Note that θmin can also be found
analytically.)
9
A2 – 3 A2 – 5
A L = 15 foot inextensible cord is tensioned by a spring having A bridge is supported by cables that are suspended from a
a spring constant of k = 1000 pounds per foot and an circular arch, as shown. The number of cables is 2n + 1. The
unstretched length of a = 0.25 feet. A W = 20 pound weight cables are tensioned to equal tensions T. After some analysis, in
hangs from point A, as shown. After some analysis, the tension order to support a 100 kilo-pound weight, it has been
T in the cord (in pounds), the angle θ of the spring, and the determined that the tension in the cables is
stretch u of the spring were found to be related to each other by
the three equations W
T=
a = (a + u ) cos θ , W = ku sin θ , T = ku cos θ . n
sin kθ
It was determined from these equations that the angle θ of the 1+ 2 ∑
k =1 [cos kθ − 1 /(n + 1)] 2 + sin 2 (kθ )
spring is constrained by the equation
0 = f(θ) = a – [a + W/(ksinθ)]cosθ π
where θ = . Graph the tension T versus n for a range of
To find the angle θ, plot f(θ) over the range of θ between 0 and 2(n + 1)
90○ and locate from the graph where f(θ) = 0. Then, find the n between 1 and 10.
tension T in the cord and the stretch u in the spring. Answer:
Answer: θ = 30.321○, T = 34.2 pounds, u = 0.475 inches. T = 34.3, 23.1, 17.7, 14.4, 12.2 ,10.5, 9.27, 8.29, 7.50, 6.84 kilo - poun
A2 – 4
After analyzing the triangular truss given in problem A2 – 2, it
was determined that the unknown internal forces FAB and FAC
and the unknown reactions at B and C, denoted by RBx, RBy, and
RCx, are related to each other by the five equations
− F AB cos θ + F AC = 0,
F AB sin θ = 500
F AB cos θ + R Bx = 0,
− F AB sin θ + R By = 0,
− F AC + RCx = 0
10
A2 – 6
A stone ornamental bridge being considered as a gateway to a
European city combines Greek and gothic elements. The cables
are not just ornamental. They also pull together beam A and
pillars B. The cables are tensioned to the same tension to pull
down on the beam with a force of 2,500 pounds. The number of
cables is 2n. After some analysis, it has been determined that
the tension in the cables is
2500
T= n
∑
k
2H
k =1 L2 [2 − k /( n + 1)] 2 + H 2 [k /( n + 1)] 2
where L = 7 feet and H = 25 feet. Graph the tension T versus n
for a range of n between 5 and 10.
Answer: T = 361, 303, 261, 229, 205, 185 pounds.
11