You are on page 1of 29

AGUILAR, KEITH ANGELINE A.

BSCE 3C

CHAPTER 5-5.14

PROBLEM 2:
Use the plot command to plot the function for .

SOLUTION:
clc, clear, close all
x=[-3:10];
%Define the vector x
y=[((0.5*(x.^4))+(1.1.*(x.^3))-(0.9.*(x.^2))).*(exp((-0.7).*(x)))]

y = 1×14
22.048658763932625 -17.842879854116571 -3.020629061205715 0

%Define function y
plot(x,y,'-r*')

The plot of the function for is stated above.

1
PROBLEM 4:
Plot the function and its derivative for in one figure. Plot the function with a solid line,
and the derivative with a dashed line. Add a legend and label the axes.

SOLUTION:
clc, clear, close all
x=[0:10];
%Define the vector x
y=(x.^2).*exp(-x);
yd=[(x.*exp(-x)).*(2-x)];
plot(x,y,'-b*',x,yd,'--r*')
%x-axis label
xlabel('0<x<10')
%y-axis label
ylabel('f(x) and df(x)/dx value')
legend('y=(x.^2).*exp(-x))','yd=(x.*exp(-x)).*(2-x)')

The plot of function and its derivative for is stated above.

PROBLEM 6:
Use the fplot command to plot the function for for

SOLUTION:

2
clc, clear, close all
x=[0:10];
%Define the vector x
y=5.*((exp((-0.5).*(x)))-(exp((-0.8).*(x))))

y = 1×11
0 0.786008477977059 0.829914615883935 0.662061034295087

fplot(@(x)5.*((exp((-0.5).*(x)))-(exp((-0.8).*(x)))),[0 10])

The Plot of the function for .

PROBLEM 8:
The orbit of the planet Mercury around the sun can be approximated by the

equation miles. Make a plot of the orbit.

SOLUTION:
clc, clear, close all
theta=linspace(0,2*pi);
%Theta is the range of angular orbit.
r=3.44*10^7./(1-0.206.*cos(theta));
polar(theta,r,'b');

3
Therefore, the plot of the orbit of the planet Mercury around the sun that can be approximated by the equation

is stated above.

PROBLEM 10:
The butterfly curve (Fay, T. H. "The Butterfly Curve." Amer. Math. Monthly 96, pp. 442-443, 1989) is given by the
following parametric equations:

On one page make two plots of butterfly curves. One for and the other for .

SOLUTION:
clc, clear, close all
figure
ax1=subplot(2,1,1);
t=[0:0.01:2*pi];
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,'b')

4
%x-axis label
xlabel('x','fontsize',12)
%y-axis label
ylabel('y','fontsize',12)
axis([-4 4 -4 4])
ax2=subplot(2,1,2);
t=[0:0.01:10*pi];
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,'g')
%Axis label
xlabel('x','fontsize',12)
xlabel('y','fontsize',12)
axis([-4 4 -4 4])

Therefore, the plot two butterfly curves on same page for and for from the given functions
is stated above.

PROBLEM 12:
Make the plot of the astroid that is shown in the previous problem by using the parametric equation:

and for .

SOLUTION:

5
clc, clear, close all
t=[-pi:0.01:pi];
x=(cos(t).^3);
y=(sin(t).^3);
plot(x,y,'b')
%Axis label
xlabel('x','fontsize',12)
xlabel('y','fontsize',12)
axis([-1 1 -1 1])

Therefore, the plot of parametric equation for .

PROBLEM 14:
Plot the function for . Notice that the function has two vertical asymptotes. Plot the

function by dividing the domain of x into three parts: one from –4 to near the left asymptote, one between the
two asymptotes, and one from near the right asymptote to 4. Set the range of the y axis from –15 to 15.

SOLUTION:
clc, clear, close all
%Function for 'f'
f=@(x) (x+(1./((x.^2)-1)));
x=-4:0.1:4;

6
y=f(x);
figure(1)
plot(x,y)
grid
% Define the variables
xa = linspace(-4, -1.1, 100);
xb = linspace(-0.9, 0.9, 100);
xc = linspace(1.1, 4, 100);
ya = xa + 1./(xa.^2 - 1);
yb = xb + 1./(xb.^2 - 1);
yc = xc + 1./(xc.^2 - 1);
plot(xa, ya, xb, yb, xc, yc)
xlabel('x')
ylabel('y')

plot(xa, ya, 'k' ,xb, yb, 'k' ,xc, yc, 'k')


xlabel('x')
ylabel('y')

7
Therefore, the plot of the given equation is stated above.

PROBLEM 16:
The shape of the pretzel shown is given by the following parametric equations:

where . Make a plot of the pretzel.

SOLUTION:
clc, clear, close all
t=-4:0.01:4;
%Define the vector x
x=(3.3-0.3.*(t.^2)).*(cos(t));
y=(3.3-0.3.*(t.^2)).*(sin(t));
plot(x,y,'b')
%Axis label
xlabel('x','fontsize',12)
xlabel('y','fontsize',12)
hold on

8
Therefore, the plot of the function and for is stated above.

PROBLEM 18:
Make a polar plot of the function:

for

The plot is shown on the right.

SOLUTION:
clc, clear, close all
n=linspace(1,100);
theta=deg2rad(135.7*n);
r=sqrt(n);
polarscatter(theta,r)

9
Therefore, the plot of the function , for in polar form is stated above.

PROBLEM 20:
Plot two ellipses is one figure (shown). The ellipse with the solid line has major axes of and . The
ellipse with the dashed line is the solid-line ellipse rotated by .

SOLUTION:
clc, clear, close all
a=10;
b=4;
x1=0;
y1=0;
t=linspace(0,2*pi,100);
r=deg2rad(30);
x=(a*sin(t));
y=(b*cos(t));
plot(x,y)
axis([-15,15,-10,10])
hold on
x2 = a*cos(t)*cos(r) - b*sin(t)*sin(r);
y2 = b*sin(t)*cos(r) + a*cos(t)*sin(r);
plot(x2,y2,'--')
axis([-15,15,-10,10])

10
hold off
xlabel('x')
ylabel('y')

Therefore, the plot two ellipses in one figure is shown above.

CHAPTER 6-6.8 PROBLEMS

PROBLEM 2:
Given: . Evaluate the following expressions without using MATLAB. Check the answers with
MATLAB.

SOLUTION:
clc, clear, close all
%(a) Evaluate
d=6;
e=4;
f=-2;
y=d+f>=e>d-e

11
y = logical
0

%Assign 1 if tthe comparison is correct otherwise assign 0


%(b) Evaluate
d=6;
e=4;
f=-2;
y=e>d>f

y = logical
1

%Assign 1 if the comparison is correct otherwise assign 0


%(c) Evaluate
d=6;
e=4;
f=-2;
y=e-d<=d-e==f/f

y = logical
1

%Assign 1 if the comparison is correct otherwise assign 0


%(d) Evaluate
d=6;
e=4;
f=-2;
y=(d/(e*f)<f)>-1*((e-d)/f)

y = logical
1

%Assign 1 if the comparison is correct otherwise assign 0

Therefore, the value of the expresion

PROBLEM 4:
Use the vectors v and w from Problem 3. Use relational operators to create a vector u that is made up of the
elements of v that are smaller than or equal to the elements of w.

SOLUTION:
clc, clear, close all
%Evaluate the expression
%Define the vector v
v=[-2 4 1 0 2 1 2]

v = 1×7
-2 4 1 0 2 1 2

12
%Define the vector w
w=[2 5 0 1 2 -1 3]

w = 1×7
2 5 0 1 2 -1 3

u=v<=w

u = 1×7 logical array


1 1 0 1 1 0 1

%Evaluate the expression

Therefore, the vector u is .

PROBLEM 6:
Use loops to create a matrix in which the value of each element is two times its row number minus three
times its column number. For example, the value of element (2,5) is .

SOLUTION:
clc, clear, close all
rows=size(1);
columns=size(2);
for i=1:4
for j=1:6
A(i,j)=2*i-3*j;
end
end
display(A)

A = 4×6
-1 -4 -7 -10 -13 -16
1 -2 -5 -8 -11 -14
3 0 -3 -6 -9 -12
5 2 -1 -4 -7 -10

Therefore, the created matrix a is,

PROBLEM 8:
Write a program that asks the user to input a vector of integers of arbitrary length. Then, using a for-end loop
the program examines each element of the vector. If the element is positive, its value is doubled. If the element
is negative, its value is tripled. The program displays the vector that was entered and the modified vector.

13
Execute the program, and when the program ask the user to input a vector type This
creates a 19-element vector with random integers between –10 and 20.

SOLUTION:
clc, clear, close all
Vi=randi([-10 20],1, 19);
V=Vi;
for i= 1:19
if V(i)>0
V(i)=2*V(i);
end
if V(i)<0
V(i)=3*V(i);
end
end
fprintf('Vi is modified using a for-end loop the program examines each element of
the vector. If the element is positive, its value is doubled. If the element is
negative, its value is tripled resulting in Vf'); Vi, Vf=V

Vi is modified using a for-end loop the program examines each element of the vector. If the element is positive, its
Vi = 1×19
19 19 7 -9 -3 0 15 -10 -9 -5 10 12 10
Vf = 1×19
38 38 14 -27 -9 0 30 -30 -27 -15 20 24 20

Therefore, the given program is executed as stated above.

PROBLEM 10:
The daily high temperature (°F) in New York City and Denver, Colorado, during the month of January 2014 is
given in the vectors below (data from the U.S. National Oceanic and Atmospheric Administration).

where the elements in the vectors are in the order of the days in the month. Write a program in a script file that
determines and displays the following information:

(a) The average temperature for the month in each city (rounded to the nearest degree.

(b) The number of days that the temperature was above the average in each city.

(c) The number of days that the temperature in Denver was higher than the temperature in New York.

SOLUTION:
clc, clear, close all
%(a)Compute
%Define the vector array DEN
DEN=[39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 52 62 45 62 40 25 57 60 57
20 32 50 48 28];

14
%Define the vector array NYC
NYC=[33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 39 36 45 33 18 19 19 28 34
44 21 23 30 39];
Average_DEN=round(mean(DEN));
Average_NYC=round(mean(NYC));
fprintf('\nThe average temperature of DENVER in the month of January is
%g(F)',Average_DEN)

The average temperature of DENVER in the month of January is 44(F)

fprintf('\nThe average temperature of NEW YORK CITY in the month of January is


%g(F)',Average_NYC)

The average temperature of NEW YORK CITY in the month of January is 35(F)

%(b)Compute
%Define the vector array DEN
DEN=[39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 52 62 45 62 40 25 57 60 57
20 32 50 48 28];
%Define the vector array NYC
NYC=[33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 39 36 45 33 18 19 19 28 34
44 21 23 30 39];
Average_DEN=round(mean(DEN));
Average_NYC=round(mean(NYC));
count=1;
DNYC=0;
DDEN=0;
while count<=length(NYC)
if NYC(count)>Average_NYC
DNYC=DNYC+1;
end
if DEN(count)>Average_DEN
DDEN=DDEN+1;
end
count=count+1;
end
fprintf('\nThe temperature in New York City was above the average during
%gdays',DNYC);

The temperature in New York City was above the average during 15days

fprintf('\nThe temperature in Denver was above the average during %g days',DDEN);

The temperature in Denver was above the average during 18 days

%(c)Compute
%Define the vector array DEN
DEN=[39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 52 62 45 62 40 25 57 60 57
20 32 50 48 28];
%Define the vector array NYC
NYC=[33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 39 36 45 33 18 19 19 28 34
44 21 23 30 39];

15
Average_DEN=round(mean(DEN));
Average_NYC=round(mean(NYC));
count=1;
higher_DEN=0;
while count<=length(NYC)
if NYC(count)>DEN(count)
higher_DEN=higher_DEN+1;
end
count=count+1;
end
fprintf('\nThe temprature in Denver was higher than the temprature in New York City
during %g days',higher_DEN);

The temprature in Denver was higher than the temprature in New York City during 8 days

Therefore, the number of days that the temperature in Denver was higher than the temperature in New York is 8
days.

PROBLEM 12:
Fibonacci numbers are the numbers in a sequence in which the first three elements are 0, 1, and 1, and the
value of each subsequent element is the sum of the previous three elements:

Write a MATLAB program in a script file that determines and displays the first 25 Fibonacci numbers.

SOLUTION:
clc, clear, close all
%MATLAB script file that displays first 20 Fibonacci numbers
f1=0;
f2=1;
f3=1;
%Print first two numbers
fprintf('\n%d\n%d\n%d\n',f1,f2,f3);

0
1
1

for (i=3:24)
%Calculate third number
f4=f1+f2+f3;
fprintf('%d\n',f4);
f1=f2;
f2=f3;
f3=f4;
end

2
4
7

16
13
24
44
81
149
274
504
927
1705
3136
5768
10609
19513
35890
66012
121415
223317
410744
755476

fprintf('\n');

Therefore, the given program is executed as stated above.

PROBLEM 14:
The value of can be estimated from:

Write a program (using a loop) that determines for a given n. Run the program with
. Compare the result with pi. (Use format long.)

SOLUTION:
clc, clear, close all
format long
%Values of M
M=[10,100,1000];
for x=1:3
num=0;
%Summation is computed using the loop
for n=0:M(x)
num=num+((-1)^n)/((2*n+1)^3);
end
num=(num*32).^(1/3)
end

num =
3.141642788603785
num =
3.141592719141050
num =
3.141592653657139

17
pi

ans =
3.141592653589793

Therefore,the given progpram is executed as stated above.

PROBLEM 16:
Write a program that (a) generates a vector with 20 random integer elements with integers between 10 and
30, (b) replaces all the elements that are not even integers with random integers between 10 and 30, and
(c) repeats (b) until all the elements are even integers. The program should also count how many times (b)
is repeated before all the elements are even integers. When done, the program displays the vector and a
statement that states how many iterations were needed for generating the vector.

SOLUTION:
clc, clear, close all
vI=randi([10 30],1,20);
v=vI;
for i=1:30
c=0;
for j=1:20
if rem(v(j),2)
v(j)=randi([10 30]);
c=1;
end
end
if c==0
break
end
end
vF=v;
vI, vF, fprintf('%.0f interactions were done.\n',i-1);

vI = 1×20
20 12 22 14 18 22 15 16 22 15 27 30 25
vF = 1×20
20 12 22 14 18 22 22 16 22 10 18 30 16
3 interactions were done.

Therefore, the given program is executed is stated above.

PROBLEM 18:
The Pythagorean theorem states that . Write a MATLAB program in a script file that finds all
the combinations of triples a, b, and c that are positive integers all smaller or equal to 50 that satisfy the
Pythagorean theorem. Display the results in a three-column table in which every row corresponds to one triple.
The first three rows of the table are:

18
SOLUTION:
clc, clear, close all
n=1;
%Find all possible values of a,b and c
for a_=1:50
for b_=1:50
c_=sqrt(a_^2+b_^2);
if c_==round(c_)
if c_<=50
a(n)=a_;
b(n)=b_;
c(n)=c_;
n=n+1;
end
end
end
end
%Display result
for n=1:length(a)
if b(n)>a(n)&&c(n)>b(n)
fprintf('%2d %2d %2d\n',a(n),b(n),c(n))
end
end

3 4 5
5 12 13
6 8 10
7 24 25
8 15 17
9 12 15
9 40 41
10 24 26
12 16 20
12 35 37
14 48 50
15 20 25
15 36 39
16 30 34
18 24 30
20 21 29
21 28 35
24 32 40
27 36 45
30 40 50

Therefore, the given program is executed is stated above.

PROBLEM 20:

19
A safe prime is a prime number that can be written in the form where p is also a prime number. For
example, 47 is a safe prime since and 23 is also a prime number. Write a computer program that
finds and displays all the safe primes between 1 and 1,000. Do not use MATLAB’s built-in function

SOLUTION:
clc, clear, close all
Safe_primes=[];
fprintf('All the safe primes from 1 to 1000 are:\n\n')

All the safe primes from 1 to 1000 are:

for i=3:1000
prime=true;
for j=2:i-1
if mod(i,j)==0
prime=false;
end
end
if prime==true
Safe_primes(length(Safe_primes)+1)=i;
p=i-1;
p=p/2;
if sum(Safe_primes==p)==1
fprintf('%d\n',i);
end
end
end

7
11
23
47
59
83
107
167
179
227
263
347
359
383
467
479
503
563
587
719
839
863
887
983

fprintf('\n')

20
Therefore, the given program is executed is stated above.

CHAPTER 7-7.13

PROBLEM 2:
Write a user-defined MATLAB function for the following math function:

The input to the function is (in radians) and the output is Write the function such that can be a vector.

(a) Use the function to calculate

(b) Use the function to plot (polar plot) for .

SOLUTION:
clc, clear, close all
%For (a)
th=[pi/6,5*pi/6];
fprintf('Use the function to calculate r(/6) and r(5/6) with r as the output.');,

Use the function to calculate r(/6) and r(5/6) with r as the output.

r=3*sin(3*cos(0.5*(th)));
%For (b)
thet=linspace(0,2*pi,1000);
th=linspace(0,4*pi,200);
r=3*sin(3*cos(0.5*(th)));
polar(th,r)

21
Therefore, the program is stated above.

PROBLEM 4:
Pressure in U.S. customary units is measured in psi (pound per square inch). In SI metric units pressure is
measured in Pa (N/m2). Write a user-defined MATLAB function that converts pressure given in units of psi to
pressure in units of Pa. For the function name and arguments, use . The input argument psi
is the pressure in units of psi to be converted, and the output argument Pa is the converted pressure in units of
Pa (rounded to the nearest integer). Use the function in the Command Window to:

(a) Convert 120 psi to units of Pa.

(b) Convert 3,000 psi to units of Pa.

SOLUTION:
clc, clear, close all
%For (a)
psi=120;
PsiToPa=6894.75728;
Pa = PsiToPa*psi;
fprintf('120 psi is equal to %.0f Pa.\n',Pa)

120 psi is equal to 827371 Pa.

%For (b)

22
psi=30000;
Pa = PsiToPa*psi;
fprintf('120 psi is equal to %.0f Pa.\n',Pa)

120 psi is equal to 206842718 Pa.

Therefore, the conversion of to units of Pa is

PROBLEM 6:
Write a user-defined MATLAB function that converts torque given in units of N-m to torque in units of lb-ft. For
the function name and arguments, use . The input argument Nm is the torque in N-m, and
the output argument lbft is the torque in lb-ft (rounded to the nearest integer). Use the function to convert 2,000
N-m to units of lb-ft.

SOLUTION:
clc, clear, close all
Nm=2000;
Nm=round(Nm);
%Converting values
NmTOlbft=0.73756;
lbft = NmTOlbft*Nm;
fprintf('2000 N-m is equal to %.0f lb-ft.\n',lbft)

2000 N-m is equal to 1475 lb-ft.

Therefore, the conversion of to units of is

PROBLEM 8:
The fuel tank shown in the figure in shaped as a half a sphere with in.

Write a user-defined function that calculates the volume of fuel in the tank (in gallons) as a function of the height
y (measured from the bottom). For the function name and arguments, use V = Volfuel(y). Use the function to
make a plot of the volume as a function of y for in.

SOLUTION:
clear, clc, close all
%Define depth vector
y=(0:1:24);
%Declare volume vector
vol = zeros(size(y));
%Calculate volume of tank using Vol_fuel function
for i=1:length(y)
vol(i) = Vol_fuel(y(i));
end
%Plot the graph
plot(y,vol);

23
xlabel('Depth (inch)')
ylabel('Volume (gal)')
title('Volume of fuel tank to Depth of fuel');

Therefore, the program is stated above.

PROBLEM 10:
The relative humidity, RH, at sea level can be calculated from measured values of the dry-bulb temperature, ,
and the wet-bulb temperature by (temperatures in degrees Celsius):

where VP is the vapor pressure given by:

and SVP is the saturated vapor pressure given by:

Write a user-defined function for calculating RH for given and . For the function name and
arguments, use . The input arguments are Tdb and Twb are the dry-bulb and wet-buld
temperatures, respectively in °F. The output argument RH is the relative humidity in percent (rounded to the
nearest integer). Inside the user-defined function use a subfunction, or an anonymous function to convert the
unit of the temperature from Cesius to Fahrenheit. Use the function to determine the relative humidity for the
following conditions:

(a)

(b)

SOLUTION:
clc, clear, close all
%For (a) (if temperature is in Celsius(CTdb), use additional function Conv)
Tdb=75;

24
Twb=69;
VP=2.1932;
SVP=2.9659;
RH=((VP)/(SVP))*100

RH =
73.947199838160429

RH=round(RH,0)

RH =
74

fprintf('Rh is the relative humidity in percent.');

Rh is the relative humidity in percent.

Therefore,the required relative humidity is 74.

%For (b)
Tdb=93;
Twb=90;
VP=4.7025;
SVP=5.2901;
RH=((VP)/(SVP))*100

RH =
88.892459499820419

RH=round(RH,0)

RH =
89

fprintf('Rh is the relative humidity in percent.');

Rh is the relative humidity in percent.

Therefore, the required relativite humidity is 89.

PROBLEM 12:
Write a user-defined MATLAB function that determines the angle that forms by the intersection of two lines. For
the function name and arguments, use th=anglines(A,B,C). The input arguments to the function are vectors with
the coordinates of the points A, B, and C, as shown in the figure, which can be two- or three-dimensional. The
output th is the angle in degrees. Use the function anglines for determining the angle for the following cases:

(a) A(–5, –1, 6), B(2.5, 1.5, –3.5), C(–2.3, 8, 1)

(b) A(–5.5, 0), B(3.5, –6.5), C(0, 7)

SOLUTION:
clear, clc, close all
%For (a)

25
A=[-5,-1,6];
B=[2.5,1.5,-3.5];
C=[-2.3,8,1];
th1 = angles(A,B,C);
%For (b)
A=[-5.5,0];
B=[3.5,-6.5];
C=[0,7];
th2 = angles(A,B,C);
fprintf('CONCLUSION: The calculated angle for (a) is %f\n degrees.',th1')

CONCLUSION: The calculated angle for (a) is 56.854288


degrees.

fprintf('and for (b) is %f\n degrees.',th2')

and for (b) is 39.627892


degrees.

PROBLEM 14:
Write a user-defined MATLAB function that determines the unit vector in the direction of the line that connects
two points (A and B) in space. For the function name and arguments, use n = unitvec(A,B). The input to the
function are two vectors A and B, each with the Cartesian coordinates of the corresponding point. The output
n is a vector with the components of the unit vector in the direction from A to B. If points A and B have two
coordinates each (they are in the x y plane), then n is a two-element vector. If points A and B have three
coordinate each (general points in space), then n is a three-element vector. Use the function to determine the
following unit vectors:

(a) In the direction from point (–0.7, 2.1) to point (9, 18).

(b) In the direction from point (10, –3.5, –2.5) to point (–11, 6.5, 5.9).

SOLUTION:
clear, clc, close all
%For (a)
n1 = unitvec([-0.7 2.1],[9 18])

n1 = 1×2
0.520798331676776 0.853679739552654

fprintf('CONCLUSION: The unit vector for (a) is displayed above')

CONCLUSION: The unit vector for (a) is displayed above

%For (b)
n2 = unitvec([10 -3.5 -2.5],[-11 6.5 5.9])

n2 = 1×3
-0.849180003299532 0.404371430142634 0.339672001319813

fprintf('CONCLUSION: The unit vector for (b) is displayed above')

26
CONCLUSION: The unit vector for (b) is displayed above

PROBLEM 16:
The area of a triangle ABC can be calculated by:

where AB is the vector from vertex A to vertex B and AC is the vector from vertex A to vertex C. Write a
user-defined MATLAB function that determines the area of a triangle given its vertices’ coordinates. For the
function name and arguments, use [Area] = TriArea(A,B,C). The input arguments A, B, and C are vectors, each
with the coordinates of the corresponding vertex. Write the code of TriArea such that it has two subfunctions—
one that determines the vectors AB and AC and another that executes the cross product. (If available, use the
user-defined functions from Problem 15). The function should work for a triangle in the x-y plane (each vertex
is defined by two coordinates) or for a triangle in space (each vertex is defined by three coordinates). Use the
function to determine the areas of triangles with the following vertices:

SOLUTION
clear, clc, close all
%For (a)
A=[1,2];
B=[10,3];
C=[6,11];
[Area] = TriArea(A,B,C)

Area =
38

fprintf('Area of the triangle is %.1f\n\n',Area)

Area of the triangle is 38.0

%For (b)
A=[-1.5 -4.2 -3];
B=[-5.1 6.3 2];
C=[12.1 0 -1.5];
[Area] = TriArea(A,B,C)

Area =
87.111780058726836

fprintf('Area of the triangle is %f\n\n',Area)

Area of the triangle is 87.111780

PROBLEM 18:

27
Write a user-defined function that determines the location of the center and the radius of a circle that passes
through three given points in a plane. The function also creates a plot that shows the circle and the points. For
the function name and arguments, use [C R]=Circle3Pts(A,B,C). The input arguments A, B, and C are each a
two-element vector with the x and y coordinates of the corresponding point. The output argument C, is a vector
with the coordinates of the center the output argument R, is the radius (both rounded to the nearest hundredth).
Use the function with the following three points: A(7, 1.2), B(0.5, 2.6), and C(–2.4, –1.4).

SOLUTION:
%Input A B C in the function
[C,R]=Circle3Pts([7 1.2],[0.5 2.6],[-2.4 -1.4])

C = 1×2
2.870000000000000 -2.170000000000000
R =
5.330000000000000

fprintf('CONCLUSION:')

CONCLUSION:

fprintf('The output argument C is %.2f\n.',C)

The output argument C is 2.87


.The output argument C is -2.17
.

fprintf('The output argument R is %.2f\n\n.',R)

The output argument R is 5.33

PROBLEM 20:
Write a user-defined function that plots a triangle and the circle that is inscribed inside, given the coordinates of
its vertices. For the function name and arguments, use TriCirc(A,B,C). The input arguments are vectors with the
x and y coordinates of the vertices, respectively. This function has no output arguments. Use the function with
the points (2.6, 3.2), (11, 14.5), and (–2, 2.8)

SOLUTION:
clear, clc, close all

28
%Define variables
A=[2.6,3.2]

A = 1×2
2.600000000000000 3.200000000000000

B= [11,14.5]

B = 1×2
11.000000000000000 14.500000000000000

C= [-2,2.8]

C = 1×2
-2.000000000000000 2.800000000000000

TriCirc(A, B, C)
title('Circle Inscribred in a Triangle')

29

You might also like