
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 21E
Solution:
The function file is,
% MATLAB code to calculate the side length of the triangle.
%function file.
function [out] = sidetriangle(x1, y1, x2, y2)
%define the side of the triangle by using the function sidetriangle.
out = sqrt((x1-x2)^2+(y1-y2)^2);
end
% end of function
%The script file should be placed in the same folder.
The function file to calculate the area is,
% MATLAB code to calculate the area of the triangle and print the result.
%function file.
function areatriangle(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.
area = sqrt (s*(s-a)*(s-b)*(s-c));
fprintf('Area of the triangle is %3.2f\n', area);
%print the area of the triangle.
end
% end of function
%The script file should be placed in the same folder.
The script file is,
% MATLAB code to calculate the area of the triangle by calling a function.
%script file.
point1 = input('the coordinates of the fist point is entered:');
%enter the coordinates of the first point.
point2 = input('the coordinates of the second point is entered:');
%enter the coordinates of the second point.
point3 = input('the coordinates of the third point is entered:');
%enter the coordinates of the third point.
x1 = point1(1);y1 = point1(2);
%define the variable x1.
x2 = point2(1);y2 = point2(2);
%define the variable x2.
x3 = point3(1);y3 = point3(2);
%define the variable x3.
areatriangle(x1, y1, x2, y2, x3, y3)
% end of file
%The script file should be placed in the same folder.
Explanation of Solution
The given two points are
The formula for 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:
clc
clear all
close all
% MATLAB code to calculate the side length of the triangle.
%function file.
function [out] = sidetriangle(x1, y1, x2, y2)
%define the side of the triangle by using the function sidetriangle.
out = sqrt((x1-x2)^2+(y1-y2)^2);
end
% end of function
%The script file should be placed in the same folder.
% MATLAB code to calculate the area of the triangle and print the result.
%function file.
function areatriangle(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.
area = sqrt (s*(s-a)*(s-b)*(s-c));
fprintf('Area of the triangle is %3.2f\n', area);
%print the area of the triangle.
end
% end of function
%The script file should be placed in the same folder.
% MATLAB code to calculate the area of the triangle by calling a function.
%script file.
point1 = input('the coordinates of the fist point is entered:');
%enter the coordinates of the first point.
point2 = input('the coordinates of the second point is entered:');
%enter the coordinates of the second point.
point3 = input('the coordinates of the third point is entered:');
%enter the coordinates of the third point.
x1 = point1(1);y1 = point1(2);
%define the variable x1.
x2 = point2(1);y2 = point2(2);
%define the variable x2.
x3 = point3(1);y3 = point3(2);
%define the variable x3.
areatriangle(x1, y1, x2, y2, x3, y3)
% end of file
%The script file should be placed in the same folder.
Save the MATLAB scripts with names, sidetriangle.m, areatriangle.m and areacall.m in the current folder. Execute the script by typing the script name at the command window to generate result.
Result:
The results are,
Therefore, the results and script files are stated above.
Want to see more full solutions like this?
Chapter 6 Solutions
MATLAB: A Practical Introduction to Programming and Problem Solving
- The daily sales (in hundreds of dollars) for a store in one month (30 days) are: 15, 22, 18, 25, 19 12, 17, 24, 20, 23 30, 28, 26, 31, 35 21, 19, 27, 18, 20 16, 15, 32, 30, 17 24, 29, 22, 33, 25 a. Construct a grouped frequency distribution with class intervals of width 5 starting from 12. b. Draw a histogram and state whether the data is symmetric, skewed left, or skewed right. Instruction: 1. Please answer the question given above for your tutorial participation mark. 2. Please upload your hand-written answers (pdf format).arrow_forwardDon’t solve i will dislike ?arrow_forwardWhat is the difference between population and sample in statistics?arrow_forward
- Don’t solve questionarrow_forwardDon’t solve questionsarrow_forwardFred needs to choose a password for a certain website. Assume that he will choose an 8-character password, and that the legal characters are the lowercase letters a, b, c, ..., z, the uppercase letters A, B, C, ..., Z, and the numbers 0, 1, . . ., 9. (a) How many possibilities are there if he is required to have at least one lowercase letter in his password? (b) How many possibilities are there if he is required to have at least one lowercase letter and at least one uppercase letter in his password? (c) How many possibilities are there if he is required to have at least one lowercase letter, at least one uppercase letter, and at least one number in his password?arrow_forward
- a =1500, b=1700 what is percentage of a is barrow_forwardA 12-inch bar that is clamped at both ends is to be subjected to an increasing amount of stress until it snaps. Let Y = the distance from the left end at which the break occurs. Suppose Y has the following pdf. f(y) = { (a) Compute the cdf of Y. F(y) = 0 0 y -옴) 0 ≤ y ≤ 12 1- 12 y 12 Graph the cdf of Y. F(y) 1.0 0.8 0.6 0.4 0.2 y 2 6 8 10 12 F(y) F(y) F(y) 1.01 1.0ㅏ 1.0 0.8 0.6 0.4 0.2 0.8 0.8 0.6 0.4 ཨཱུ སྦེ 0.6 0.4 0.2 2 4 6 8 10 12 (b) Compute P(Y ≤ 5), P(Y > 6), and P(5 ≤ y ≤ 6). (Round your answers to three decimal places.) P(Y ≤ 5) = P(Y > 6) = P(5 ≤ y ≤ 6) = (c) Compute E(Y), E(y²), and V(Y). E(Y) = in E(Y2) v(x) = in 2 2 2 4 6 8 10 12 y 2 4 6 8 10 12arrow_forwardA restaurant serves three fixed-price dinners costing $12, $15, and $20. For a randomly selected couple dining at this restaurant, let X = the cost of the man's dinner and Y = the cost of the woman's dinner. The joint pmf of X and Y is given in the following table. p(x, y) 15 y 12 20 12 0.05 0.10 0.35 x 15 0.00 0.20 0.10 20 0.05 0.05 0.10 (a) Compute the marginal pmf of X. x 12 Px(x) Compute the marginal pmf of Y. y Pyly) 12 15 20 15 20 (b) What is the probability that the man's and the woman's dinner cost at most $15 each? (c) Are X and Y independent? Justify your answer. X and Y are independent because P(x, y) = Px(x) · Py(y). X and Y are not independent because P(x, y) =Px(x) · Pyly). X and Y are not independent because P(x, y) * Px(x) · Py(y). X and Y are independent because P(x, y) * Px(x) · Py(y). (d) What is the expected total cost, in dollars, of the dinner for the two people? $ (e) Suppose that when a couple opens fortune cookies at the conclusion of the meal, they find the…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




