MATLAB: A Practical Introduction to Programming and Problem Solving
MATLAB: A Practical Introduction to Programming and Problem Solving
5th Edition
ISBN: 9780128154793
Author: Stormy Attaway Ph.D. Boston University
Publisher: Elsevier Science
bartleby

Videos

Question
Book Icon
Chapter 6, Problem 21E
Expert Solution & Answer
Check Mark
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 calculate the area of the triangle by calling a local function that calculates the length of the side formed by any two points.

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

The formula for 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)2a=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)2b=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)2c=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+522s=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)area=12.5000

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,

MATLAB: A Practical Introduction to Programming and Problem Solving, Chapter 6, Problem 21E , additional homework tip  1

MATLAB: A Practical Introduction to Programming and Problem Solving, Chapter 6, Problem 21E , additional homework tip  2

MATLAB: A Practical Introduction to Programming and Problem Solving, Chapter 6, Problem 21E , additional homework tip  3

Therefore, the results and script files are 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
m Proctor ments sources Dak is investigating how long their phone's battery lasts (in hours) for various brightness levels (on a scale of 0-100). A part of the regression analysis done on Ti Calculator and in Excel for their data are displayed below. Ti Calculator Click this button to reveal the Ti Calculator Output. Click again to collapse it. Ti Calculator y = ax+b B0 and p 0 t = -3.02431335 p = 0.0233 df - 6 a =-0.0380555845 b=5.88809202 s = 0.436054238 20.603868129 r = -0.777089524 Resources Excel Type here to search Click this button to reveal the Excel Output. Click again to collapse it. SUMMARY OUTPUT Multiple R 0.777089524 R Square 0.603868129 Adjusted R Square 0.537846150 Standard Error 0.436054238 Observations 8 ANOVA df SS MS F Significance F Regression 1 1.73914021 Residual 6 1.14085979 1.73914021 0.190143298 9.1465 0.0233 Total 7 2.88000000 Coefficients Standard Error Intercept Brightness 5.88809202 -0.0380555845 t Stat 7.62593816 -3.02431335 P-value 0.0003 0.0233 Use…
ce Problems x + canvas.pdx.edu/courses/106252/assignments/1102408?module_item_id=4856656 m Proctor ments THIS IS THE second question of exdin where you have to Snow compere work to get run points. If you use TI calculator functions, you may write your work along with functions and values you I used and answers on a paper or type on an empty Word document or Excel file. If you use Ti calculator app on your computer or phone, you may provide screen shots of those on a word document or Excel file. Clearly provide the function along with values for the work. If you use Excel, you may use the Excel calculator or Excel as a calculator on your computer to provide complete work and the final answer on it. Please provide all the answers to the following questions on the same Excel sheet. If you use formula, you may write all your steps with numbers on a paper and submit a photo of it. Otherwise, you may type all those steps on a Word document or an Excel file. Do NOT round in the middle steps…
Suppose the model for certain data is a parabola y = Bo+B1x+B2x² with observations (1, 2.2), (2, 6.9), (3, 16.1), (4, 28.7), (5, 46.1). Describe the design matrix, the observation vector, and the parameter vector. Using these write down the system of equations to be approximated Xẞ = y, the parts of the normal equations XTX and XTy and write down the normal equations. Solve and determine the residual vector €. You may use a calculator for the computations but show the steps as described above.
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
UG/ linear equation in linear algebra; Author: The Gate Academy;https://www.youtube.com/watch?v=aN5ezoOXX5A;License: Standard YouTube License, CC-BY
System of Linear Equations-I; Author: IIT Roorkee July 2018;https://www.youtube.com/watch?v=HOXWRNuH3BE;License: Standard YouTube License, CC-BY