MATLAB Homework

profileabdlaz3
ch_1_overview.pdf

Chapter 1

Why Study MATLAB  Widely used in Industry: Signal processing, numerical

analysis, ex. Used by engineers in NASA

 Interactive: can be used as a calculator

 Programmable: can build logic, condition, and loop as other programming language, such as C, and BASIC.

 Has large library of functions: can be used to solve complex math problems in engineering field.

 Visualization Capability: display on screen and print computer

Overview of MATLAB  1.1 MATLAB Interactive Sessions

 1.2 Menus and the Toolbar

 1.3 Arrays, Files, and Plots

 1.4 Script Files and the Editor/Debugger

 1.5 The MATLAB Help System

 1.6 Problem-Solving Methodologies

MATLAB Interactive Sessions  How to Start: double click on MATLAB icon and four

windows appear:

 How to make interactive calculation: type in command

 How to Exit

Five Windows in MATLAB  Command window in the center

 Workspace window in the lower left

 Detail window in the middle left

 Current Directory in the upper left

COMMAND WINDOW  You use command window to communicate with

MATLAB

 Type in commands, functions and statements.

 Make sure cursor is after >>prompt

 Current directory shows which directly you are working

 Workspace displays variable created. Double click on the variable can open the array editor

 Reset environment: Environment->Layout->Default.

Commands and Expressions  Type in commands after prompt >> x=3

 Press Enter to execute the command

 Use the up-arrow key ( ) to scroll back through the commands

 Type in >>8/10>> 8/10 ans = 0.8000

 Four digit precision for answers

 ans is a special variable

 5*ans ans = 4

 Note that the variable ans now has the value 4.

Command  You can use variables to write mathematical

expressions. Instead of using the default variable ans

 you can assign the result to a variable of your own choosing, say, r, as follows:

 >> r=8/10 r = 0.8000

 Spaces in the line improve its readability; for example, you can put a space before and after the = sign if you want. MATLAB ignores these spaces

 >>r

 >>s=20*r

Function and Argument  MATLAB has hundreds of functions

 R=sqrt(9)

 Order of Precedence

 ^ exponentiation: ab a^b

 * multiplication: ab a*b

 / right division: a/b= a/b

 \ left division: a\b =a\b

 + addition: a+b

 - subtraction: a -b

Expression Practice:  >>8 + 3*5 (ans=23)

 >>(8 + 3)*5 (ans = 55)

 >>4^2 - 12 - 8/4*2 (ans = 0)

 >>4^2 - 12 - 8/(4*2) ans = 3 >>3*4^2 + 5 ans = 53 >>(3*4)^2 + 5 (ans = 149)

 >>27^(1/3) + 32^(0.2)(ans = 5 )

 >>27^(1/3) + 32^0.2 (ans = 5)

 >>27^1/3 + 32^0.2 (ans = 11 )

Example Problem:  Volume of a Circular Cylinder The volume of a circular

cylinder of height hand radius r is given by V =π*r2h. A particular cylindrical tank is 15 m tall and has a radius of 8 m. We want to construct another cylindrical tank with a volume 20 percent greater but having the same height. How large must its radius be?

Solutions  First solve the cylinder equation for the radius r. This

gives

 The session is shown below. First we assign values to the variables r and h representing the radius and height. Then we compute the volume of the original cylinder and increase the volume by 20 percent. Finally we solve for the required radius. For this problem we can use the MATLAB built-in constant pi.

Solutions: MATLAB program  >>r = 8;

 >>h = 15;

 >>V = pi*r^2*h;

 >>V = V + 0.2*V;

 >>r = sqrt(V/(pi*h))

 r =8.7636

Variable Names  The term workspace refers to the names and values of

any variables in use in the current work session.

 Variable names must begin with a letter;

 the rest of the name can contain letters, digits, and underscore characters.

 MATLAB is case sensitive. Thus the following names represent different variables: speed, Speed, SPEED, Speed_1, andSpeed_2.

 In MATLAB 7, variable names can be no longer than 63 characters

Arrays  Array is a set of numbers arranged in a specific order.

 Use square brackets to denote the variable x to contain this collection by typing x = [0, 4, 3, 6].

 y = [6, 3, 4, 0];

 y = [6, 3, 4, 0];

 z = x + y

Iteration  The numbers 0, 0.1, 0.2, . . . , 10 can be assigned to the

variable u by typing u = 0:0.1:10.

 In this application of the colon operator, the brackets should not be used.

 >>u = 0:0.1:10;

 >>w = 5*sin(u);

Element in Array  >> u(7)

 ans = 0.6000

 >>w(7)

 ans = 2.8232

 You can use the length function to determine how many values are in an array. For example, continue the previous session as follows:

 >>m = length(w)

 m = 101

Functions  ex -- exp(x)

 √x -- sqrt(x)

 ln x -- log(x)

 log10 x -- log10(x)

 cos x -- cos(x)

 sin x -- sin(x)

 tan x -- tan(x)

 Cos-1 x acos(x)

 Sin-1 x asin(x)

 tan-1 x atan(x)

Plotting with MATLAB  let us plot the function y # 5 sin x for 0<= x <=7.

 Use an increment of 0.01 to generate a large number of x values in order to produce a smooth curve.

 >>x = 0:0.01:7;

 >>y = 3*cos(2*x);

 >>plot(x,y),xlabel(‘x’),ylabel(‘y’)

Plotting with MATLAB

Creating and Using a Script File  New->Script to start text editor

 Type in

 % Program cos_script.m

 % This program computes the cosine of

 % the square root and displays the result.

 x = sqrt(13:3:25);

 y = cos(x)

 plot(x,y),xlabel(‘x’),ylabel(‘y’)

 Save as cos_script

 Type cos_script to execute the program.

Result Graph

Piston Motion - Engineering Problem

 Figure 1.6–2a shows a piston, connecting rod, and crank for an internal combustion engine. When combustion occurs, it pushes the piston down. This motion causes the connecting rod to turn the crank, which causes the crankshaft to rotate.

 We want to develop a MATLAB program to compute and plot the distance d traveled by the piston as a function of the angle A, for given values of lengths L1 and L2. Such a plot would help the engineers designing the engine to select appropriate values for lengths L1 and L2.

Piston Motion - Engineering Problem

Piston Motion - Engineering Problem

 L1 =1 ft and L2 =0.5 ft.

 0 <= A <= 180°.

 d= L1*cosB + L2*cosA

 sin A/ L1 =sin B /L2

 sinB= L2cosA/ L1

 B = sin-1(L2 sin A/ L1 )

Piston Motion - Engineering Problem

MATLAB script  L_1 = 1;

 L_2 = 0.5;

 R = L_2/L_1;

 A_d = 0:0.5:180;

 A_r = A_d*(pi/180);

 B = asin(R*sin(A_r));

 d =L_1*cos(B) + L_2*cos(A_ r);

 plot(A_d,d),xlabel(‘A(degrees)’), ylabel(‘d (feet)’),grid

Piston Motion - Plot