Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
658 views

Chapter 2 Solution - Matlab Programming With Applications For Engineers

The document contains MATLAB code for several exercises involving vector calculations and plotting graphs. The code asks the user to input initial values like height and velocity of an object, coordinates of vectors, and calculates/plots the corresponding graphs of height vs time, velocity vs time, and polar vs rectangular coordinates. Angle between 2D and 3D vectors is also calculated using dot product and norm functions. Results are displayed after each calculation.

Uploaded by

CarolMichalski
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
658 views

Chapter 2 Solution - Matlab Programming With Applications For Engineers

The document contains MATLAB code for several exercises involving vector calculations and plotting graphs. The code asks the user to input initial values like height and velocity of an object, coordinates of vectors, and calculates/plots the corresponding graphs of height vs time, velocity vs time, and polar vs rectangular coordinates. Angle between 2D and 3D vectors is also calculated using dot product and norm functions. Results are displayed after each calculation.

Uploaded by

CarolMichalski
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Homework 3

Exercise 2.10
CODE
clear all
close all
%Asking the user for the input also defining the initial height and
%velocity of the ball
h0 = input('Write the initial height of the ball in meters: ');
v0 = input('Write the initial velocity of the ball in meters per second: ');
%value of gravity
g = -9.81;
%time is defined in an interval from 0 to 10 seconds, in steps of 0.1
t = (0:0.1:10);
%Height equation
h = ((0.5*g*(t.^2)) + (t*v0) + h0);
%Figure one - plot of height vs time
figure(1);
plot(t,h);
title('Plot of the Height (m) vs. Time (s)')
xlabel('Height (m)')
ylabel('Time (s)')
%Velocity Equation
v = ((g*t) + v0);
%Figure two - plot of velocity vs time
figure(2);
plot (t,v);
title('Plot of the Velocity (m/s) vs. Time (s)')
xlabel('Time (s)')
ylabel('Velocity (m/s)')
%Figure three - plot comparing height and velocity vs time
figure(3);
plot(t,h,t,v);
title('Comparison of height and velocity vs time')
xlabel('Time (s)')
ylabel('Height in meters and Velocity m/s')
legend('h(t)','v(t)')

RESULTS
*The users is asked to input the initial height and velocity of the ball, in these case it was respectively
defined has 15 meters and 6 m/s.*
Write the initial height of the ball in meters: 15
Write the initial velocity of the ball in meters per second: 6

Figure 1

Figure 2

Figure 3

Exercise 2.13
(a) CODE
clear all
close all
%The user is asked to write the vector coordinates in rectangular
%coordinates
x = input('Write the value of the x coordinate of the vector: ');
y = input('Write the value of the y coordinate of the vector: ');
%Calculation of the polar coordinates
%Radius calculation
r = ((x^2) + (y^2))^0.5;
%Angle calculated in degrees
theta = atan2(y,x);
theta1 = (theta*180)/pi;

%The program returns the input vector in polar coordinates


fprintf('The value of the vector coordinates in polar is: %f, %f', r,theta1);

Results
Write the value of the x coordinate of the vector: 10
Write the value of the y coordinate of the vector: 7
The value of the vector coordinates in polar is: 12.206556, 34.992020

(b) CODE
clear all
close all
%The user is asked to write the vector coordinates in polar
%coordinates
r = input('Write the value of the r coordinate of the vector: ');
theta1 = input('Write the value of the theta coordinate of the vector, in
degrees: ');
%The angle is converted from degrees to radianos
theta = (theta1*pi)/180;
%Converting from polar to rectangular coordinates
x = r*cos(theta);
y = r*sin(theta);
%The program returns the input vector in rectangular coordinates
fprintf('The value of the vector coordinates in rectangular coordinates is:
%f, %f', x,y);

RESULTS
Write the value of the r coordinate of the vector: 12.206556
Write the value of the theta coordinate of the vector, in degrees: 34.992020
The value of the vector coordinates in rectangular coordinates is: 10.000000, 7.000000

Exercise 2.17
CODE
clear all
close all
%The
x1 =
y1 =
x2 =
y2 =

user is asked to
input('Write the
input('Write the
input('Write the
input('Write the

write
value
value
value
value

the coordinates values


of the x coordinate of
of the y coordinate of
of the x coordinate of
of the y coordinate of

for
the
the
the
the

both vectors
first vector: ');
first vector: ');
second vector: ');
second vector: ');

%The coordinates values are stored has two vectors


u = [x1;y1];
v = [x2;y2];
%Calculos for the angle (in degrees) between the two vectors
theta = acos((dot(u,v))/((norm(u))*(norm(v))))*(180/pi);
%The program returns the value of the angle between the 2D vectors in
%degrees
fprintf('The angle between the two vectors is equal to %f degrees\n\n',
theta);

RESULTS
Write the value of the x coordinate of the first vector: 1
Write the value of the y coordinate of the first vector: 5
Write the value of the x coordinate of the second vector: 7
Write the value of the y coordinate of the second vector: 2
The angle between the two vectors is equal to 62.744672 degrees

Exercise 2.18
CODE
clear all
close all
%theta = arccos((u*v)/(|u|*|v|))
%The
x1 =
y1 =
z1 =
x2 =
y2 =
z2 =

user is asked to
input('Write the
input('Write the
input('Write the
input('Write the
input('Write the
input('Write the

write
value
value
value
value
value
value

the coordinates values


of the x coordinate of
of the y coordinate of
of the z coordinate of
of the x coordinate of
of the y coordinate of
of the z coordinate of

for
the
the
the
the
the
the

both 3D vectors
first vector: ');
first vector: ');
first vector: ');
second vector: ');
second vector: ');
second vector: ');

%The coordinates values are stored has two vectors


u = [x1;y1;z1];
v = [x2;y2;z2];
%Calculos for the angle (in degrees) between the two vectors
theta = acos((dot(u,v))/((norm(u))*(norm(v))))*(180/pi);
%The program returns the value of the angle between the 3D vectors in
%degrees
fprintf('The angle between the two vectors is equal to %f degrees\n\n',
theta);

RESULTS
Write the value of the x coordinate of the first vector: 15
Write the value of the y coordinate of the first vector: 6
Write the value of the z coordinate of the first vector: 3
Write the value of the x coordinate of the second vector: 4
Write the value of the y coordinate of the second vector: 20
Write the value of the z coordinate of the second vector: 2
The angle between the two vectors is equal to 56.472312 degrees

Reference
Chapman, S. J. MATLAB Programming with applications for Engineers; 1st edition.

You might also like