




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































th
INTRODUCTION
Computers and software have had a tremendous impact upon engineering education over the past several years and most engineering schools now incorporate computational software such as MATLAB in their curriculum. Since you have this supplement the chances are pretty good that you are already aware of this and will have to learn to use MATLAB as part of a Dynamics course. The purpose of this supplement is to help you do just that.
There seems to be some disagreement among engineering educators regarding how computers should be used in an engineering course such as Dynamics. I will use this as an opportunity to give my own philosophy along with a little advice. In trying to master the fundamentals of Dynamics there is no substitute for hard work. The old fashioned taking of pencil to paper, drawing free body and mass acceleration diagrams, struggling with equations of motion and kinematic relations, etc. is still essential to grasping the fundamentals of Dynamics. A sophisticated computational program is not going to help you to understand the fundamentals. For this reason, my advice is to use the computer only when required to do so. Most of your homework can and should be done without a computer.
The problems in this booklet are based upon problems taken from your text. The problems are slightly modified since most of the problems in your book do not require a computer for the reasons discussed in the last paragraph. One of the most important uses of the computer in studying Mechanics is the convenience and relative simplicity of conducting parametric studies. A parametric study seeks to understand the effect of one or more variables (parameters) upon a general solution. This is in contrast to a typical homework problem where you generally want to find one solution to a problem under some specified conditions. For example, in a typical homework problem you might be asked something about the trajectory of a particle launched at an angle of 30 degrees from the horizontal with an initial speed of 30 ft/sec. In a parametric study of the same problem you might typically find the trajectory as a function of two parameters,
trajectory for different launch angles and speeds. A plot of this type is very beneficial in visualizing the general solution to a problem over a broad range of variables as opposed to a single case.
AN INTRODUCTION
TO MATLAB
This chapter provides an introduction to the MATLAB programming language. Although the chapter is introductory in nature it will cover everything needed to solve the computer problems in this booklet.
1.1 Numerical Calculations
When you open MATLAB you should see a prompt something like the following.
EDUª
To the right of this prompt you will write some sort of command. The following example assigns a value of 200 to the variable a. Simply type ìa = 200î at the prompt and then press enter.
EDUª a= a = 200
There are many situations where you may not want MATLAB to print the result of a command. To suppress the output simply type a semi-colon ì;î after the command. For example, to suppress the output in the above case type ìa = 200;î and the press enter. The basic mathematical operations of addition, subtraction, multiplication, division and raising to a power are accomplished exactly as you would expect if you are familiar with other programming languages, i.e. with the keys +, -, *, , and ^. Here are a couple of examples.
EDUª c=2*5-120/ c =
EDUª d=2^ d = 16
MATLAB has many built in functions that you can use in calculations. If you already know the name of the function you can simply type it in. If not, you can find a list of functions in the ìHelp Desk (HTML)î file available under Help in the main menu bar. Here are a few examples.
EDUª e=sin(.5)+tan(.2) e =
As with most mathematical software packages, the default unit for angles will be radians.
EDUª f=sqrt(16) f = 4
EDUª g=log(5) g =
EDUª h=log10(5) h =
In the last two examples be careful to note that log is the natural logarithm. For a base 10 logarithm (commonly used in engineering) you need to use log.
Range Variables
In the above examples we have seen many cases where a variable (or name) has been assigned a numerical value. There are many instances where we would like a single variable to take on a range of values rather than just a single value. Variables of this type are often referred to as range variables. For example, to have the variable x take on values between 0 and 3 with an increment of 0.25 we would type ìx=0:0.25:3î.
EDUª x=0:0.25: x = Columns 1 through 7 0 0.2500 0.5000 0.7500 1.0000 1.2500 1. Columns 8 through 13
0 0.2500 -1.0000 -3.7500 -8.0000 -13.7500 -21.
Note that we wrote ì2xî instead of ì2.xî since 2 is a scalar. If in doubt, it never hurts to include the period.
1.2 Writing Scripts (m-files)
If you have been working through the examples above you have probably noticed by now that you cannot modify a line once it has been entered into the worksheet. This makes it rather difficult to debug a program or run it again with a different set of parameters. For this reason, most of your work in MATLAB will involve writing short scripts or m-files. The term m-file derives from the fact that the programs are saved with a ì.mî extension. An m-file is essentially just a list of commands that you would normally enter at a prompt in the worksheet. After saving the file you just enter the name of the file at the prompt after which MATLAB will execute all the lines in the file as if they had been entered. The chief advantage is that you can alter the script and run it as many times as you like.
To get started, select ì FileÖNewÖM-File î from the main menu. A screen like that below will then pop up.
Now enter the program line by line into the editor. Basically, you just enter the commands you would normally enter at a prompt (do not, however, enter >> at the beginning of a line). If you want to enter comments, begin the line with ì%î. These lines will not be executed. Once you have finished entering the script into the editor you can save the file (be sure to remember where you saved it). The file name can contain numbers but cannot begin with a number. It also cannot contain any mathematical operators such as +, -, *, / etc. Also, the name of the file should not be the same as a variable it computes.
To run the script all you have to do is type the name of the program at a prompt (>>), but without the ì.mî extension. A common error at this point is that MATLAB will give some response indicating that it hasnít a clue what you are doing. The reason this happens is that the file you saved is not in the current path. You can check (or change) the path by selecting FileÖSet PathÖ from the main menu.
Following is a simple example of a script file that sets up a range variable x and then calculates two functions over the range.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This script calculates two % functions over a specified range x=0:0.2:1; f=sin(x)./(1+x) g=sin(x).*exp(x) %%%%%%%%% end of script %%%%%%%%%%%%%%%%%%%%
The program was saved as example1, so here is the output you should see when the program is entered at a prompt.
EDUª example f = 0 0.1656 0.2782 0.3529 0.3985 0. g = 0 0.2427 0.5809 1.0288 1.5965 2.
To save space we will, in the future, show the output of a script immediately after the script is given without actually showing the file name being entered at a prompt.
EDUª load1(load1(3)) ans = 87
EDUª x=1:0.2:2; EDUª g=load1(x) g = 3.0000 2.3200 1.8800 1.6800 1.7200 2.
1.4 Graphics
One of the most useful things about a computational software package such as MATLAB is the ability to easily create graphs of functions. As we will see, these graphs allow one to gain a lot of insight into a problem by observing how a solution changes as some parameter (the magnitude of a load, an angle, a dimension etc.) is varied. This is so important that practically every problem in this supplement will contain at least one plot. By the time you have finished reading this supplement you should be very proficient at plotting in MATLAB. This section will introduce you to the basics of plotting in MATLAB.
MATLAB has the capability of creating a number of different types of graphs. Here we will consider only the X-Y plot. The most common and easiest way to generate a plot of a function is to use range variables. The following example will guide you through the basic procedure.
EDUª x=-3:0.1:3; EDUª f=x.*exp(-x.^2); EDUª plot(x,f)
The procedure is very simple. First define a range variable covering the range of the plot, then define the function to be plotted and issue the plot command. After typing in the above you should see a graph window pop up which looks something like the following figure.
Things like titles and axis labels can be added in one of two ways. The first is by line commands such as those shown below.
EDUª xlabel('x') EDUª ylabel('x*exp(-x^2)') EDUª title('A Simple Plot')
Take a look at the graph window after you type each of the lines above. At this point, the graph window should look something like that shown below.
Parametric Studies
One of the most important uses of the computer in studying Mechanics is the convenience and relative simplicity of conducting parametric studies (not to be confused with parametric plotting discussed below). A parametric study seeks to understand the effect of one or more variables (parameters) upon a general solution. This is in contrast to a typical homework problem where you generally want to find one solution to a problem under some specified conditions. For example, in a typical homework problem you might be asked to find the reactions at the supports of a structure with a concentrated force of magnitude 200 lb that is oriented at an angle of 30 degrees from the horizontal. In a parametric study of the same problem you might typically find the reactions as a function of two parameters, the magnitude of the force and its orientation. You might then be asked to plot the reactions as a function of the magnitude of the force for several different orientations. A plot of this type is very beneficial in visualizing the general solution to a problem over a broad range of variables as opposed to a single case.
Parametric studies generally require making multiple plots of the same function with different values of a particular parameter in the function. As a simple example, consider the following function.
f = 5 + x − 5 x^2 + ax^3
What we would like to do is gain some understanding of how f varies with both x and a. It might be tempting to make a three dimensional plot in a case like this. Such a plot can, in some cases, be very useful. Usually, however, it is too difficult to interpret. This is illustrated by the following three dimensional plot of f versus x and a.
% This script produces a 3-d plot of % f versus x and a. x=linspace(-10,10,30); a=linspace(-3,3,30); [X,A]=meshgrid(x,a); F=5+X-5X.^2+A.X.^3; B=0.*A+2; mesh(X,A,F) colormap gray %%%%%%% end of script %%%%%%%%%%%%%%%%%%%%
The plot above certainly is interesting but, as mentioned above, not very easy to interpret. In most cases it is much better to plot the function several times (with different values of the parameter of interest) on a single two-dimensional graph. We will illustrate this by plotting f as a function of x for a = -1, 0, and 1. This is accomplished in the following script.
% script for plotting f versus x for several % values of a. x=-5:0.1:5; g=5+x-5*x.^2; f1=g-x.^3; % a = - f2=g; % a = 0 f3=g+x.^3; % a = 1 plot(x,f1,x,f2,x,f3) xlabel('x') ylabel('f') %%%%%%%% end of script %%%%%%%%%%%%%%%%%%%%%%
% This script file illustrates parametric plotting. % A function f is plotted versus another function g. % The two functions are related by the % common parameter a. a=-1:0.05:3.5; f=10a.(2-a); g=sin(3*a); plot(g,f) xlabel('g') ylabel('f') %%%%%%%% end of script %%%%%%%%%%%%%%%%%%%%%%%%%%%
The range selected for the parameter can have a big, and sometimes surprising effect on the resulting graph. To illustrate, try increasing the upper limit on the range on a a few times and see how the graph changes.
You can, of course, also plot g as a function of f.
% Another example of parametric plotting a=-1:0.05:6; f=10a.(2-a); g=sin(3*a); plot(f,g) xlabel('f') ylabel('g') %%%%%%%%% end of script %%%%%%%%%%%%%%%%%