
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
- 3.) The lengths (in kilometers) of a random sample of rivers on the South Island of New Zealand that flow to the Tasman Sea are listed in table #3.1.9 (Lee, 1994). Find the mean, median, and mode. Table #3.1.9: Lengths of Rivers (km) Flowing to Tasman Sea River Length (km) River Length (km) Hollyford 76 Waimea 48 Cascade 64 Motueka 108 Arawhata 68 Takaka 72 Haast 64 Aorere 72 Karangarua 37 Heaphy 35 Cook 32 Karamea 80 Waiho 32 Mokihinui 56 Whataroa 51 Buller 177 Wanganui Waitaha Hokitika 56 Grey 121 40 Taramakau 80 64 Arahura 56arrow_forward2.) The lengths (in kilometers) of a random sample of rivers on the South Island of New Zealand that flow to the Pacific Ocean are listed in table #3.1.8 (Lee, 1994). Find the mean, median, and mode. Table #3.1.8: Lengths of Rivers (km) Flowing to Pacific Ocean River Length River Length (km) (km) Clarence 209 Clutha 322 Conway 48 Taieri 288 Waiau 169 Shag 72 Hurunui 138 Kakanui 64 Waipara 64 Rangitata 121 Ashley 97 Ophi 80 Waimakariri 161 Pareora 56 Selwyn 95 Waihao 64 Rakaia 145 Waitaki 209 Ashburton 90arrow_forwardSection 3.1: Homework 1.) Cholesterol levels (mg/dL) were collected from a random sample of patients two days after they had a heart attack (Ryan, Joiner & Ryan, Jr, 1985) and are in table #3.1.7. Find the mean, median, and mode. Table #3.1.7: Cholesterol Levels 270 236 210 142 280 272 160 220 226 242 186 266 206 318 294 282 234 224 276 282 360 310 280 278 288 288 244 236arrow_forward
- > Next question Get a similar question You can retry this question below am Proctor ) ements ns sources esources Quick note for Excel users: Excel analyzes using first variable minus second variable. If need help, watch the video at the bottom. Testing a claim about a population mean: t-Test An overly involved "neighborhood watch" group has been investigating the length of lawns. In years past, lawns had been mowed to a mean length of 2.8 inches. A recent random sample of lawn lengths is given below. Conduct an appropriate test, at a 5% significance level, to determine if the overall mean lawn length is now higher than 2.8 inches. Data (lawn lengths, measured in inches): 3.32 3.30 3.70 2.02 4.63 3.68 3.78 3.49 3.91 3.29 2.19 3.95 3.31 3.68 4.40 3.81 4.39 4.31 Checksum: 65.16 Click Download CSV to download csv file of data or copy/paste the data into Excel. After downloading the file, you may want to save it as an Excel Workbook. A. Consider the parameter of interest and choose the…arrow_forwardext question Get a similar question You can retry this question below Proctor nts Quick note for Excel users: Excel analyzes using first variable minus second variable. If need help, watch the video at the bottom. Testing a claim about a population mean: t-Test An overly involved "neighborhood watch" group has been investigating the length of lawns. In years past, lawns had been mowed to a mean length of 2.8 inches. A recent random sample of lawn lengths is given below. Conduct an appropriate test, at a 5% significance level, to determine if the overall mean lawn length is now higher than 2.8 inches. Data (lawn lengths, measured in inches): 3.32 3.30 3.70 2.02 4.63 3.68 3.78 3.49 3.91 3.29 2.19 3.95 3.31 3.68 4.40 4.39 4.31 3.81 rces urces Checksum: 65.16 Click Download CSV to download csv file of data or copy/paste the data into Excel. After downloading the file, you may want to save it as an Excel Workbook. A. Consider the parameter of interest and choose the correct alternative…arrow_forwardWhat is the detailed explaination of the solution and answer?arrow_forward
- Trigonometry (MindTap Course List)TrigonometryISBN:9781337278461Author:Ron LarsonPublisher:Cengage LearningElementary AlgebraAlgebraISBN:9780998625713Author:Lynn Marecek, MaryAnne Anthony-SmithPublisher:OpenStax - Rice UniversityCollege AlgebraAlgebraISBN:9781305115545Author:James Stewart, Lothar Redlin, Saleem WatsonPublisher:Cengage Learning
- Holt Mcdougal Larson Pre-algebra: Student Edition...AlgebraISBN:9780547587776Author:HOLT MCDOUGALPublisher:HOLT MCDOUGALAlgebra & Trigonometry with Analytic GeometryAlgebraISBN:9781133382119Author:SwokowskiPublisher:Cengage




