Docsity

Solving for θ4 in a 4-Bar Mechanism using Matlab - Prof. Aldo Ferri, Assignments of Computer Science

Toggle sidebar
bg1
ME2016, Fall 2008, Dr. Ferri
Homework Assignment 3
Due Wednesday, September 10, 2008
θ2θ4
a
b
c
d
Problem 1 (5 points)
A 4-bar mechanism is a popular mechanism that converts rotary motion of one link to oscillatory or
“rocking” motion of another. In the mechanism above, the following dimensions are given: a = 1, b
= 2, c = 4, and d = 4. If θ2 is specified, θ4 can be obtained as the solution (root) of the following
equation:
41 42 23 24
() cos() cos() cos( )0fk k k
θ
θθ θθ
=−+−−=
where
1d
ka
=, 2d
kc
=
, and
222 2
32
abcd
kac
−
++
=
Write a Matlab script that performs the following operations (Use θ2 = π/6 radians):
(i) Defines an anonymous function for 4
()f
θ
.
(ii) Solves for the closest root to θ4 = 2 radians using the Matlab function fzero
(iii) Defines a second anonymous function 44 4
() ()gf
θ
θθ
=
+.
(iv) Uses the function g to perform a fixed-point algorithm, starting with θ4 = 2 radians.
(v) Automatically stops the fixed-point algorithm when εa < 0.01 %.
Compare the result from your fixed-point algorithm to the answer obtained using fzero.
Anonymous Functions
To make an anonymous function in Matlab, use the following syntax. Suppose that we want to
create a function
(
)
22
() 3 /
x
f
xxe x
−
=− . Suppose also that we want to find the closest root to
x = 0.5. This can be accomplished with the Matlab commands (in blue), generating the Matlab
responses (in black):
>> fc = @(x) (3*x.^2 - exp(-2*x))./sqrt(x)
fc =
@(x)(3*x.^2-exp(-2*x))./sqrt(x)
pf3
pf4
pf5

Partial preview of the text

Download Solving for θ4 in a 4-Bar Mechanism using Matlab - Prof. Aldo Ferri and more Assignments Computer Science in PDF only on Docsity!

ME2016, Fall 2008, Dr. Ferri Homework Assignment 3 Due Wednesday, September 10, 2008

a

b

c

d

Problem 1 (5 points) A 4-bar mechanism is a popular mechanism that converts rotary motion of one link to oscillatory or “rocking” motion of another. In the mechanism above, the following dimensions are given: a = 1, b = 2, c = 4, and d = 4. If θ 2 is specified, θ 4 can be obtained as the solution (root) of the following equation:

f ( θ 4 ) = k 1 cos( θ 4 ) − k 2 cos(θ 2 ) + k 3 − cos(θ 2 − θ 4 ) = 0

where

1

d

k

a

d

k

c

= , and

2 2 2 2 3

a b c d

k

ac

Write a Matlab script that performs the following operations (Use θ 2 = π/6 radians):

(i) Defines an anonymous function for f ( θ 4 ).

(ii) Solves for the closest root to θ 4 = 2 radians using the Matlab function fzero

(iii) Defines a second anonymous function g ( θ 4 ) = θ 4 + f (θ 4 ).

(iv) Uses the function g to perform a fixed-point algorithm, starting with θ 4 = 2 radians. (v) Automatically stops the fixed-point algorithm when εa < 0.01 %.

Compare the result from your fixed-point algorithm to the answer obtained using fzero.

Anonymous Functions To make an anonymous function in Matlab, use the following syntax. Suppose that we want to

create a function f ( ) x = ( 3 x^2 − e −^2 x )/ x. Suppose also that we want to find the closest root to

x = 0.5. This can be accomplished with the Matlab commands (in blue), generating the Matlab responses (in black):

>> fc = @(x) (3x.^2 - exp(-2x))./sqrt(x)**

fc =

@(x)(3x.^2-exp(-2x))./sqrt(x)

>> fzero(fc,0.5)

ans =

Solution: With θ 2 = π/6, the function is shown in the figure below.

0 0.5 1 1.5 2 2.5 3 3.

0

2

4

6

θ 4 (rad)

f(θ

) 4

It is seen that the root is near the location θ 4 = 2.5 rad, or 143O. Before applying the fixed-point algorithm, let us compute g(θ 4 ) and its derivative:

g ( θ 4 ) = θ 4 + k 1 cos( θ 4 ) − k 2 cos(θ 2 ) + k 3 − cos(θ 2 −θ 4 )

4 1 4 2 4 4

1 sin( ) sin( ) dg k d

At θ 4 = 2.5 rad, we find that dg ( θ 4 ) / d θ 4 = g ′(θ 4 )= -0.4750. Since the magnitude is less than 1, the

fixed-point method should converge. The first few iterates are given below, clearly showing (oscillatory) convergence

2.0000, 3.0001, 2.5859, 2.4187, 2.4968, 2.4505, 2.4759, 2.4612, 2.4695, 2.4648, 2.4674, 2.4659, 2.4668, 2.4663, 2.4666, 2.

Problem 2 (3 points) Compute the true, relative, percent error, εt, in the fixed point algorithm of Problem 1; use the value of θ 4 obtained using fzero as the “true root” value. Plot the log of the magnitude of εt vs the iteration number n for n = 1 to 10. What does the plot resemble, especially as n increases?

Solution According to the theory, we should have (^) ( Et ) (^) i + 1 = g ′( xr )( Et ) i where Et is the true error. Used

recursively, we have

0 1 2 3 4 5 6 7

3

θ 2 (rad)

θ^4

(rad)

Matlab Code

% Solution to HW % 4-Bar Mechanism

a=1; b=2; c=4; d=4; t2 = pi/6; % theta2 in radians k1=d/a; k2=d/c; k3=(a^2 - b^2 + c^2 + d^2)/(2ac);

fa = @(x) k1cos(x)-k2cos(t2)+k3-cos(t2-x);

% Plot function

figure(5) x=0:0.01:pi; plot(x,fa(x),'linewidth',2) xlabel('\theta_4 (rad)') ylabel('f(\theta_4)') grid

x4 = 2 x4true = fzero(fa,x4)

eps_a = 1e6; eps_s = 0.01; i = 1; x4 = 2; x4v(1) = x4; while eps_a > eps_s; i = i + 1;

x4old = x4; x4 = x4 + fa(x4); x4v(i) = x4; % store intermediate values eps_a = 100*abs((x4-x4old)/x4); end i x4v'

eps_t = 100*abs((x4true - x4v)/x4true); Elog = log10(eps_t); n = 1:i; % form vector from n = 1 to n = last value of i figure(12) plot(n,Elog,'linewidth',2) xlabel('n'); ylabel('log(\epsilon_t)'); grid

gprime = @(x) -k1*sin(x) - sin(t2-x) + 1; figure(6) plot(x,gprime(x)) grid

x2=0:0.01:(2pi); L2=length(x2); x4i=3; % initial guess for theta for i = 1:L2; t2 = x2(i); fa = @(x) k1cos(x)-k2*cos(t2)+k3-cos(t2-x); x4i = fzero(fa,x4i); x4(i) = x4i; end

figure(7) plot(x2,x4,'linewidth',2) xlabel('\theta_2 (rad)') ylabel('\theta_4 (rad)') grid

min_theta4 = min(x4)180/pi max_theta4 = max(x4)180/pi