Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving
4th Edition
ISBN: 9780128045251
Author: Stormy Attaway Ph.D. Boston University
Publisher: Elsevier Science
bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 6, Problem 19E
To determine

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 function to formulate the area of the triangle by calling a local function that calculates the side lengths formed by any two points.

Expert Solution & Answer
Check Mark

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 (x1,y1) and (x2,y2).

The formula of the distance between the two points is given as,

distance=(x1x2)2+(y1y2)2

Substitute 0 for x1, 5 for x2, 0 for y1 and 0 for y2 in the above formula.

distance=(05)2+(00)2distance=5

Consider the three points are (0,0), (5,0) and (5,5).

The side of the triangle is,

a=(x1x2)2+(y1y2)2

Substitute 0 for x1, 5 for x2, 0 for y1 and 0 for y2 in the above formula.

a=(05)2+(00)2=5

The side of the triangle is,

b=(x2x3)2+(y2y3)2

Substitute 5 for x2, 5 for x3, 0 for y2 and 5 for y3 in the above formula.

b=(55)2+(05)2=5

The side of the triangle is,

c=(x3x1)2+(y3y1)2

Substitute 5 for x3, 0 for x1, 5 for y3 and 0 for y1 in the above formula.

c=(50)2+(50)2=52

The formula for half sum of the sides of the triangle is,

s=a+b+c2

Substitute 5 for a, 5 for b and 52 for c in the above formula.

s=5+5+522=8.5355

The formula for the area of the triangle is,

area=s(sa)(sb)(sc)

Substitute 5 for a, 5 for b, 52 for c and 8.5355 for s in the above formula.

area=8.5355(8.53555)(8.53555)(8.535552)=12.5000

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,

Matlab, Fourth Edition: A Practical Introduction to Programming and Problem Solving, Chapter 6, Problem 19E

Therefore, the result is stated above.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Proctor ents urces ources canvas.pdx.edu/courses/106252/assignments/1102418?module_item_id=4856623 Are there less children diagnosed with Autism Spectrum Disorder (ASD) in states that have larger urban areas over states that are mostly rural? Assume data were collected from a fairly urban state and they found 165 eight-year olds diagnosed with ASD out of 18096 eight-year olds evaluated. Assume data were then collected for a fairly rural state and they found 56 eight-year olds diagnosed with ASD out of 2400 eight-year olds evaluated. Is there enough evidence to show that the proportion of children diagnosed with ASD in the fairly urban state is lower than the proportion in the fairly rural state? a.) Test at the 4% level b.) Compute a 96% confidence interval for the difference in proportions. Use the following steps for the hypothesis test. For the confidence interval you do not need to do all the steps since you did some of them already in hypothesis test. Part a.) HYPOTHESIS TEST…
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.5 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.5 inches. Data (lawn lengths, measured in inches): 2.46 2.64 3.96 4.58 3.34 3.97 3.88 4.22 3.09 3.39 4.53 3.30 2.98 3.10 2.58 3.44 Checksum: 59.34 3.88 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 hypothesis. =2.5 >2.5 Oμ2.5 Ομ 2.5 B. Are the necessary conditions present to carry out this…
4.) A random sample of Print-O-Matic printing company's employee salaries (in dollars) are contained in table #3.1.1. Table # 3.1.11: Salaries of Print-O-Matic Printing Company Employees Employee Salary ($) CEO 272,500 Driver 58,456 CD74 100,702 CD65 57,380 Embellisher 73,877 Folder 65,270 GTO 74,235 Handwork 52,718 Horizon 76,029 ITEK 64,553 Mgmt 108,448 Platens 69,573 Polar 75,526 Pre Press Manager 108,448 Pre Press Manager/ IT 98,837 Pre Press/Graphic Artist 75,311 Designer 90,090 Sales 109,739 Administration 66,346 a.) Find the mean and median. b.) Find the mean and median with the CEO's salary removed. c.) What happened to the mean and median when the CEO's salary was removed? Why? d.) If you were the CEO, who is answering concerns from the union that employees are underpaid, which average of the complete data set would you prefer? Why? e.) If you were a platen worker, who believes that the employees need a raise, which average would you prefer? Why?
Knowledge Booster
Background pattern image
Statistics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, statistics and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Trigonometry (MindTap Course List)
Trigonometry
ISBN:9781337278461
Author:Ron Larson
Publisher:Cengage Learning
Text book image
Elementary Algebra
Algebra
ISBN:9780998625713
Author:Lynn Marecek, MaryAnne Anthony-Smith
Publisher:OpenStax - Rice University
Text book image
College Algebra
Algebra
ISBN:9781305115545
Author:James Stewart, Lothar Redlin, Saleem Watson
Publisher:Cengage Learning
Text book image
College Algebra
Algebra
ISBN:9781337282291
Author:Ron Larson
Publisher:Cengage Learning
Text book image
Holt Mcdougal Larson Pre-algebra: Student Edition...
Algebra
ISBN:9780547587776
Author:HOLT MCDOUGAL
Publisher:HOLT MCDOUGAL
Text book image
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:9781133382119
Author:Swokowski
Publisher:Cengage
Points, Lines, Planes, Segments, & Rays - Collinear vs Coplanar Points - Geometry; Author: The Organic Chemistry Tutor;https://www.youtube.com/watch?v=dDWjhRfBsKM;License: Standard YouTube License, CC-BY
Naming Points, Lines, and Planes; Author: Florida PASS Program;https://www.youtube.com/watch?v=F-LxiLSSaLg;License: Standard YouTube License, CC-BY