Docsity

matlab-intro Brian D. Harper, Notas de estudo de Cultura

Toggle sidebar
bg1
Solving Dynamics Problems
in MATLAB
Brian D. Harper
Mechanical Engineering
The Ohio State University
A supplement to accompany
Engineering Mechanics: Dynamics, 6th Edition
by J.L. Meriam and L.G. Kraige
JOHN WILEY & SONS, INC.
New York • Chichester • Brisbane • Toronto • Singapore
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe matlab-intro Brian D. Harper e outras Notas de estudo em PDF para Cultura, somente na Docsity!

Solving Dynamics Problems

in MATLAB

Brian D. Harper

Mechanical Engineering

The Ohio State University

A supplement to accompany
Engineering Mechanics: Dynamics, 6

th

Edition
by J.L. Meriam and L.G. Kraige
JOHN WILEY & SONS, INC.
New York • Chichester • Brisbane • Toronto • Singapore
  • Introduction
  • Chapter 1 An Introduction to MATLAB
  • Numerical Calculations
  • Writing Scripts (m-files)
  • Defining Functions
  • Graphics
  • Symbolic Calculations
  • Differentiation and Integration
  • Solving Equations
  • Chapter 2 Kinematics of Particles
  • 2.1 Sample Problem 2/4 (Rectilinear Motion)
  • 2.2 Problem 2/87 (Rectangular Coordinates)
  • 2.3 Problem 2/126 (n-t Coordinates)
  • 2.4 Sample Problem 2/9 (Polar Coordinates)
  • 2.5 Sample Problem 2/10 (Polar Coordinates)
  • 2.6 Problem 2/183 (Space Curvilinear Motion)
    • of Connected Particles) 2.7 Sample Problem 2/16 (Constrained Motion
  • Chapter 3 Kinetics of Particles
  • 3.1 Sample Problem 3/3 (Rectilinear Motion)
  • 3.2 Problem 3/98 (Curvilinear Motion)
  • 3.3 Sample Problem 3/17 (Potential Energy)
  • 3.4 Problem 3/218 (Linear Impulse/Momentum)
  • 3.5 Problem 3/250 (Angular Impulse/Momentum)
  • 3.6 Problem 3/365 (Curvilinear Motion)
  • Chapter 4 Kinetics of Systems of Particles
  • 4.1 Problem 4/26 (Conservation of Momentum)
  • 4.2 Problem 4/62 (Steady Mass Flow)
  • 4.3 Problem 4/86 (Variable Mass)
  • Chapter 5 Plane Kinematics of Rigid Bodies
  • 5.1 Problem 5/3 (Rotation)
  • 5.2 Problem 5/44 (Absolute Motion)
  • 5.3 Sample Problem 5/9 (Relative Velocity)
  • 5.4 Problem 5/123 (Relative Acceleration)
  • 5.5 Sample Problem 5/15 (Absolute Motion)
  • Chapter 6 Plane Kinetics of Rigid Bodies
  • 6.1 Sample Problem 6/2 (Translation)
  • 6.2 Sample Problem 6/4 (Fixed-Axis Rotation)
  • 6.3 Problem 6/98 (General Plane Motion)
  • 6.4 Problem 6/104 (General Plane Motion)
  • 6.5 Sample Problem 6/10 (Work and Energy)
  • 6.6 Problem 6/206 (Impulse/Momentum)
    • Dynamics of Rigid Bodies Chapter 7 Introduction to Three-Dimensional
  • 7.1 Sample Problem 7/3 (General Motion)
  • 7.2 Sample Problem 7/6 (Kinetic Energy)
  • Chapter 8 Vibration and Time Response
  • 8.1 Sample Problem 8/2 (Free Vibration of Particles)
  • 8.2 Problem 8/139 (Damped Free Vibrations)
  • 8.3 Sample Problem 8/6 (Forced Vibration of Particles)

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,

the launch angle θ and initial speed v. You might then be asked to plot the

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 =

8 CH. 1 AN INTRODUCTION TO MATLAB

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

10 CH. 1 AN INTRODUCTION TO MATLAB

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.

INTRODUCTION TO MATLAB 11

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.

INTRODUCTION TO MATLAB 13

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.

14 CH. 1 AN INTRODUCTION TO MATLAB

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.

16 CH. 1 AN INTRODUCTION TO MATLAB

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 %%%%%%%%%%%%%%%%%%%%

INTRODUCTION TO MATLAB 17

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 %%%%%%%%%%%%%%%%%%%%%%

INTRODUCTION TO MATLAB 19

% 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.

20 CH. 1 AN INTRODUCTION TO MATLAB

% 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 %%%%%%%%%%%%%%%%%