Concept explainers
To write:
A script that will prompt the user to enter the coordinates of three points that determine a triangle, then calculate and print the area of the triangle and then call one
Answer to Problem 19E
Solution:
The script file is,
% MATLAB code to calculate the area of the triangle by calling a function.
%script file.
x1 = input('the x coordinate of the fist point is entered:');
%enter the x coordinate of the first point.
x2 = input('the x coordinate of the second point is entered:');
%enter the x coordinate of the second point.
x3 = input('the x coordinate of the third point is entered:');
%enter the x coordinate of the third point.
y1 = input('the y coordinate of the fist point is entered:');
%enter the y coordinate of the first point.
y2 = input('the y coordinate of the second point is entered:');
%enter the y coordinate of the second point.
y3 = input('the y coordinate of the third point is entered:');
%enter the y coordinate of the third point.
Area = trianglearea(x1, y1, x2, y2, x3, y3);
fprintf('The triangle’’s area is %.2f\n', Area)
% end of script file
%The script file should be placed in the same folder.
The function file is,
% MATLAB code to calculate the area of the triangle.
%function file.
function out = trianglearea(x1, y1, x2, y2, x3, y3)
%define the area of triangle by using the function areatriangle.
a = sidetriangle(x1, y1, x2, y2);
%define the variable a.
b = sidetriangle(x2, y2, x3, y3);
%define the variable b.
c = sidetriangle(x3, y3, x1, y1);
%define the variable c.
s = (a+b+c)/2;
%define the variable s.
out = sqrt (s*(s-a)*(s-b)*(s-c));
end
function out2 = sidetriangle(x1, y1, x2, y2)
%calculate the distnace between the two points by calling a function
%sidetriangle.
out2 = sqrt((x1-x2)^2 + (y1-y2)^2);
%calculate the sides of the triangle.
end
% end of function
%The function file should be placed in the same folder.
Explanation of Solution
The given two points are
The formula of the distance between the two points is given as,
Substitute 0 for
Consider the three points are
The side of the triangle is,
Substitute 0 for
The side of the triangle is,
Substitute 5 for
The side of the triangle is,
Substitute 5 for
The formula for half sum of the sides of the triangle is,
Substitute 5 for a, 5 for b and
The formula for the area of the triangle is,
Substitute 5 for a, 5 for b,
MATLAB Code:
% MATLAB code to calculate the area of the triangle by calling a function.
%script file.
x1 = input('the x coordinate of the fist point is entered:');
%enter the x coordinate of the first point.
x2 = input('the x coordinate of the second point is entered:');
%enter the x coordinate of the second point.
x3 = input('the x coordinate of the third point is entered:');
%enter the x coordinate of the third point.
y1 = input('the y coordinate of the fist point is entered:');
%enter the y coordinate of the first point.
y2 = input('the y coordinate of the second point is entered:');
%enter the y coordinate of the second point.
y3 = input('the y coordinate of the third point is entered:');
%enter the y coordinate of the third point.
Area = trianglearea(x1, y1, x2, y2, x3, y3);
fprintf('The triangle’’s area is %.2f\n', Area)
% end of script file
%The script file should be placed in the same folder.
% MATLAB code to calculate the area of the triangle.
%function file.
function out = trianglearea(x1, y1, x2, y2, x3, y3)
%define the area of triangle by using the function areatriangle.
a = sidetriangle(x1, y1, x2, y2);
%define the variable a.
b = sidetriangle(x2, y2, x3, y3);
%define the variable b.
c = sidetriangle(x3, y3, x1, y1);
%define the variable c.
s = (a+b+c)/2;
%define the variable s.
out = sqrt (s*(s-a)*(s-b)*(s-c));
end
function out2 = sidetriangle(x1, y1, x2, y2)
%calculate the distnace between the two points by calling a function
%sidetriangle.
out2 = sqrt((x1-x2)^2 + (y1-y2)^2);
%calculate the sides of the triangle.
end
% end of function
%The function file should be placed in the same folder.
Save the MATLAB script with name, mainscript.m, and the function file is trianglearea.m in the current folder. Execute the program by typing the script name at the command window to generate result.
Result:
The output is,
Therefore, the result is stated above.
Want to see more full solutions like this?
Chapter 6 Solutions
Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
- Algebra & Trigonometry with Analytic GeometryAlgebraISBN:9781133382119Author:SwokowskiPublisher:CengageTrigonometry (MindTap Course List)TrigonometryISBN:9781337278461Author:Ron LarsonPublisher:Cengage LearningElementary AlgebraAlgebraISBN:9780998625713Author:Lynn Marecek, MaryAnne Anthony-SmithPublisher:OpenStax - Rice University
- Holt Mcdougal Larson Pre-algebra: Student Edition...AlgebraISBN:9780547587776Author:HOLT MCDOUGALPublisher:HOLT MCDOUGALCalculus For The Life SciencesCalculusISBN:9780321964038Author:GREENWELL, Raymond N., RITCHEY, Nathan P., Lial, Margaret L.Publisher:Pearson Addison Wesley,