Anda di halaman 1dari 13

ENME 337

ASSIGNMENT #5



Name: Uday Karri
Lab Section: B02
Date: November 5, 2013






















SECTION ONE
TEXTBOOK QUESTIONS















Part A: Outputs

Chapter 6
Question 18

Please enter an angle in degrees: 45
The value of Sin(45.000000) = 0.707107

Please enter an angle in degrees: 195
The value of Sin(195.000000) = -0.258819

Question 20

Please enter the gender (male or female): male
Please enter the age (in numerical format): 21
Please enter the RHR (in numerical format): 62
Please enter the fitnesss level (low, medium or high): low

THR =

137.3500

Please enter the gender (male or female): female
Please enter the age (in numerical format): 19
Please enter the RHR (in numerical format): 67
Please enter the fitnesss level (low, medium or high): high

THR =

164.8240

Question 21

Please enter coordinates of the first point (in the format: [0 0]): [13 15]
Please enter coordinates of the second point (in the format: [0 0]): [4 18]
Please enter the coordinates of the third point (in the format: [0 0]): [19 3]

Center =

4.0000 3.0000


Radius =

15




Question 26

Please enter the number to find a square root of: 110
The square root of the input number 110.000000 is 10.488088

Please enter the number to find a square root of: 93443
The square root of the input number 93443.000000 is 305.684478

Please enter the number to find a square root of: 23.25
The square root of the input number 23.250000 is 4.821825

Question 28

Please enter the numerical value of the volume: 3.5
Please enter the current units of volume in cubicmeter, liter, cubicfeet, gallons etc.:
cubicmeter
Please enter the new units of volume in cubicmeter, liter, cubicfeet, gallons etc.:
gallons
The value of volume in the new desired units = 924.602000

Please enter the numerical value of the volume: 200
Please enter the current units of volume in cubicmeter, liter, cubicfeet, gallons etc.:
liter
Please enter the new units of volume in cubicmeter, liter, cubicfeet, gallons etc.:
cubicfeet
The value of volume in the new desired units = 7.062920

Please enter the numerical value of the volume: 480
Please enter the current units of volume in cubicmeter, liter, cubicfeet, gallons etc.:
cubicfeet
Please enter the new units of volume in cubicmeter, liter, cubicfeet, gallons etc.:
cubicmeter
The value of volume in the new desired units = 13.592112

Question 29

Average number of steps when B = 10, is 123.860000

Chapter 7
Question 20

>> A = [6 1.5; 2 4; -3 -1.8];
>> cirpnts(A)



Question 21

>> AddVecPol(5,23,12,40)

r =

16.8451


theta =

35.0215


>> AddVecPol(6,80,15,125)

r =

19.7048


theta =

-67.4337

Part B: Code

% Assignment #5
% Name: Uday Karri
% Lab Section:

clc
clear

%----------Chapter 6----------
%----------Question 18----------

ang = input ('Please enter an angle in degrees: ');
%user inputs the value of the angle in degrees
angrad = ang*pi/180;
%converts the angle from degrees to radians
n = 1;
first = angrad;
E = 1;
%Predefining variables

while abs(E) > 0.000001
%Start of while loop
Series = first + (-1)^n*angrad^(2*n+1)/factorial(2*n+1);
%Adding the nth term
E = abs((Series - first)/first);
%Error calculation is done here
first = Series;
n = n+1; %increment
end

fprintf('The value of Sin(%f) = %f\n\n\n', ang, first)
%printing the value of Sin(x)

%----------Question 20----------

gender = input('Please enter the gender (male or female): ' ,'s');
%User inputs the gender
AGE = input('Please enter the age (in numerical format): ');
%User inputs the age
RHR = input('Please enter the RHR (in numerical format): ');
%User inputs the resting heart rate number
fitnesslevel = input('Please enter the fitnesss level (low, medium or high): ','s');
%User inputs the fitness level

%The following conditional statements assign values to the variable INTEN
%based on the fitness level

if length(fitnesslevel) == 3
INTEN = 0.55;
elseif length(fitnesslevel) == 6
INTEN = 0.65;
elseif length(fitnesslevel) == 4
INTEN = 0.8;
end

%The following conditional statement calculates the THR for a man and woman

if length(gender) == 4
THR=(220-AGE-RHR)*INTEN+RHR
elseif length(gender) == 6
THR=((206-0.88*AGE)-RHR)*INTEN+RHR
end

%----------Question 21----------

Point1 = input('Please enter coordinates of the first point (in the format: [0 0]): ');
Point2 = input('Please enter coordinates of the second point (in the format: [0 0]):
');
Point3 = input('Please enter the coordinates of the third point (in the format: [0 0]):
');
%User inputs the coordinated for three points

%Matrix:

A(1,1) = 2*Point1(1); A(1,2) = 2*Point1(2); A(1,3) = 1;
A(2,1) = 2*Point2(1); A(2,2) = 2*Point2(2); A(2,3) = 1;
A(3,1) = 2*Point3(1); A(3,2) = 2*Point3(2); A(3,3) = 1;

B(1,1) = -Point1(1)^2 - Point1(2)^2;
B(2,1) = -Point2(1)^2 - Point2(2)^2;
B(3,1) = -Point3(1)^2 - Point3(2)^2;

X = A\B;
% Solution of the matrix outputs unknowns g, f and c in the circle eqn

Center = [-X(1) -X(2)]
Radius = (X(1)^2 +X(2)^2 - X(3))^(1/2)
%Determines the center and the radius of the circle

x = [Point1(1) Point2(1) Point3(1)];
y = [Point1(2) Point2(2) Point3(2)];
plot(x, y, 'o')
%Plotting the input points

x = -11:0.5:19;

y1 = Center(2)+(Radius^2 - (x-Center(1)).^2).^0.5;
y2 = Center(2)-(Radius^2 - (x-Center(1)).^2).^0.5;

hold on
plot(x,y1)
hold on
plot (x,y2)


title('Plot of circle for Question 21')
xlabel('x')
ylabel('y')


%----------Question 26----------

N = input('Please enter the number to find a square root of: ');
X(1) = N; %creates a vector X with N as its first number for now
i = 1;
E = 1;
%predefining some variables to be used in the loop

while E>=0.00001
%start of the while loop to find the square root of the input number
%and also estimated error is less than 0.00001

X(i+1) = (X(i)+N/X(i))/2;
E = abs((X(i+1)-X(i))/X(i));
i = i+1;
end
%end of while loop

l = length(X);
%for outputting purposes: the last number will be the square root in Matrix
%X

fprintf('The square root of the input number %f is %f \n', N, X(l))

%----------Question 28----------
value = input('Please enter the numerical value of the volume: ');
oldinp = input('Please enter the current units of volume in cubicmeter, liter,
cubicfeet, gallons etc.: ','s');
newinp = input('Please enter the new units of volume in cubicmeter, liter, cubicfeet,
gallons etc.: ','s');
%User inputs all the required values

%First conditional statement converts old units to m^3 and the value with
%it
if length(oldinp) == 10
value1 = value;
elseif length(oldinp) == 5
value1 = value*(1/1000);
elseif length(oldinp) == 9
value1 = value*(1/35.3146);
elseif length(oldinp) == 7
value1 = value*(1/264.172);
end

%Second conditional statement converts the m^3 unit and value into new
%desired units
if length(newinp) == 10
value2 = value1;
elseif length(newinp) == 5
value2 = value1*1000;
elseif length(newinp) == 9
value2 = value1*35.3146;
elseif length(newinp) == 7
value2 = value1*264.172;
end
fprintf('The value of volume in the new desired units = %f\n',value2)

%----------Question 29----------
x=0;
B=10;
avgSteps=0;
%Predefining variables above

%For loop:
for n=1:100
xj=x;
steps=0;

%while loop executes as long as B is greater than x-xj
while( abs(x-xj) < B)
s= randn(1,1);
xj = xj + s;
steps=steps+1; %increment steps
end

%average steps are incremented accordingly
avgSteps = avgSteps + steps;
end

avgSteps = avgSteps/100;

fprintf('Average number of steps when B = %d, is %f\n',B,avgSteps);

Chapter 7 Code

%----------Question 20----------
%This function is user defined and is used to plot a circle that passes
%through three given points. The input argument is a 3x2 matrix in which
%two elements of a row are the x and y coordinates of a point.
function cirpnts(P)
x1 = P(1,1); x2 = P(2,1); x3 = P(3,1);
y1 = P(1,2); y2 = P(2,2); y3 = P(3,2);

%equation to solve for the centre of the circle:
P11 = 2*(x2-x1);
P12 = 2*(y2-y1);
P21 = 2*(x3-x2);
P22 = 2*(y3-y2);

Pmatrix = [P11 P12; P21 P22];

Q1 = -x1^2+x2^2-y1^2+y2^2;
Q2 = -x2^2+x3^2-y2^2+y3^2;
Q = [Q1 Q2]';
%

X = inv(Pmatrix)*Q;
%solving the matrix

xcenter = X(1);
ycenter = X(2);
%x and y coordinates of the center
Radius = sqrt((x1-xcenter)^2 + (y1-ycenter)^2);
%Radius of the circle

x = -Radius+xcenter:0.000001:Radius+xcenter;
a = ycenter+sqrt(Radius.^2-(x-xcenter).^2);
b = ycenter-sqrt(Radius.^2-(x-xcenter).^2);
%precisely creating a blue outline of a circle by defining the distances
%from central x and y coordinates

plot(x,a,x,b,'b')
%plotting the circle in blue color
hold on
plot(x1,y1,'o',x2,y2,'*',x3,y3,'+')
%plotting three points
grid on
%using a grid for easy visualization
hold off

xlabel('X')
ylabel('Y')
title('Question 20: Plot of a circle')
end

%----------Question 21----------
%This is a user defined function that is used to add two vectors that are
%given in polar coordinates. The input arguments are r1, th1, r2 and th2
%and the output arguments are resultant and theta.
function [r,theta] = AddVecPol(r1,th1,r2,th2)
x1 = r1*cosd(th1);
x2 = r2*cosd(th2);
y1 = r1*sind(th1);
y2 = r2*sind(th2);
x = x1+x2;
y = y1+y2;

r = sqrt(x^2+y^2)
theta = atand(y/x)
end













































SECTION TWO
MINI-PROJECT #2: CURVE FITTING
AND
COORDINATE TRANSFORMATION