You are on page 1of 5

3.

6
x = [2 2 0 -3 -2 -1 0 0 2]
y = [0 1 3 1 0 -2 0 -2 2]
function carttopolar2(x,y)
%function converts cartesian coordinates to polar.
%x = [2 2 0 -3 -2 -1 0 0 2]
%y = [0 1 3 1 0 -2 0 -2 2]
r = sqrt( (x^2) + (y^2));
if x > 0
theta = atan(y/x);
elseif x < 0
if y > 0
theta = atan(y/x) + pi;
elseif y < 0
theta = atan(y/x) - pi;
else
theta = pi;
end
else
if y > 0
theta = pi/2;
elseif y < 0
theta = -pi/2;
else
theta = 0;
end
end
thetad = theta * (180/pi);
fprintf ( 'The radius is %5.3f units and theta is %5.4f degrees\n', r,
thetad)
end
When run in the command window:
>> carttopolar2(1,2)
The radius is 2.236 units
>> carttopolar2(2,0)
The radius is 2.000 units
>> carttopolar2(2,1)
The radius is 2.236 units
>> carttopolar2(0,3)
The radius is 3.000 units
>> carttopolar2(-3,1)
The radius is 3.162 units
>> carttopolar2(-2,0)
The radius is 2.000 units
>> carttopolar2(-1,-2)
The radius is 2.236 units
>> carttopolar2(0,0)
The radius is 0.000 units
>> carttopolar2(0,-2)
The radius is 2.000 units
>> carttopolar2(2,2)
The radius is 2.828 units

and theta is 63.4349 degrees


and theta is 0.0000 degrees
and theta is 26.5651 degrees
and theta is 90.0000 degrees
and theta is 161.5651 degrees
and theta is 180.0000 degrees
and theta is -116.5651 degrees
and theta is 0.0000 degrees
and theta is -90.0000 degrees
and theta is 45.0000 degrees

3.10
function beamdisp2(l)
%computes beam displacement for particular system division increment size
x=linspace(0,l);
long = length(x);
for k = 1:long
if x(k) == 0
displace(k) = 0;
elseif 10>= x(k) > 8
displace(k) = (-5/6)*(((x(k)-0).^4)-((x(k)-5).^4))+(15/6)*((x(k)8).^3)+75*((x(k)-7).^2)+(57/6)*x(k).^3-238.25*x(k);
elseif 7> x(k) <=8
displace(k) = (-5/6)*(((x(k)-0).^4)-((x(k)-5).^4))+75*((x(k)7).^2)+(57/6)*(x(k).^3)-238.25*x(k);
elseif 5 > x(k) <= 7
displace(k) = (-5/6)*(((x(k)-0).^4)-((x(k)-5).^4))+(57/6)*x(k).^3238.25*x(k);
elseif 0 <x(k) <= 5
displace(k) = (-5/6)*(((x(k)-0).^4))+((57/6)*(x(k).^3))-238.25*x(k)
else
error( 'invalid input')
end
end
plot(x,displace)
end
When Run, this figure is produced:

4500
4000
3500
3000
2500
2000
1500
1000
500
0
-500
0

10

3.11
function cylinder(r,L,plot_title)
%volume of horizontal cylinder
%inputs:
%r = radius

%L = Length
%Plot_title = string holding plot title
%h = height of fluid. max is 2r
h = linspace(0,(2*r));
vol = (r^2 * acos((r-h)./r)-(r-h).*sqrt(2*r*h-(h.^2)))*L;
plot(h,vol)
title(plot_title)
When run, this figure is produced:

Volume versus depth for horizontal cylindrical tank


150

100

50

0
0

3.22
function phasor(r, nt, nm)
%function to show the orbit of a phasor
%r = radius
%nt = number of increments of theta
%nm = number of movies
k=1;
inc = (2*pi)/nt;
theta = 0;
for k = 1:nt
x = cos(theta) * r;
y = sin(theta) * r;
plot(x,y,'o','Markerfacecolor', 'b', 'markersize',8)
axis( [-1.5*r (1.5 *r) -1.5*r (1.5*r)] );
M(k) = getframe;
theta = theta + inc;
end
pause
movie(M,nm)
end

When run, it displays the movie requested.


3.23

function butterfly(tmax,dt)
%creates a movie of the butterfly problem from 2.22
t=0;
k = 1;
kmax = (tmax/dt);
for k = 1:kmax
x = (sin(t)*(exp(cos(t))-2*cos(4*t)-(sin(t/12)^5)));
y = (cos(t)*(exp(cos(t))-2*cos(4*t)-(sin(t/12)^5)));
plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)
axis([ -5 5 -5 5])
M(k) = getframe;
t = t + dt;
end
pause
movie(M)
end

When run, displays movie requested.


4.1
function iteroot(x,es,maxit)
%evaluates a function to a certain precision
if nargin<2|isempty(es),es=0.001; end
if nargin<3|isempty(maxit),maxit=50;end
iter=1;
sol=1;
ea=100;
while (1)
solold=sol
sol = (sol + x/sol)/2
iter = iter+1
if sol~=0
ea = abs((sol-solold/sol)*100)
end
if ea<=es | iter>=maxit, break, end
end
fx = sol;
end

When Run, Produces


sol =

iter =

50

ea =

200

4.2
(a) 1011001 = (1) + 1*2^3 + 1*2^4 + 1*2^6 = 1 + 8+16+64 = 89
(b)0.01011 = 1/4 +1/16 + 1/32 = 11/32
(c) 110.01001 = 2 + 4 + + 1/32 = 6 and 9/32

4.3
(a) 61,565 = 5 + 6*8 + 5*8^2 + 1* 8^3 + 6*8^4 = 25461
(b) 2.71 = 2 + 7/8 + 1/64 = 2 and 57/64

4.4

You might also like