Area, Write a MATLAB function that calculates the area of a triangle using Heron
ID: 2248350 • Letter: A
Question
Area,
Write a MATLAB function that calculates the area of a triangle using Heron’s formula.
(30 Marks) Use the function in a program that prompts the user for the lengths of the sides of a triangle and displays the area of the triangle.
After displaying the value, the program should indefinitely go back to prompt for the lengths of sides of the next triangle then recalculate and display the area.
The program should quit when the user enters a negative number.
The prompt of the program should inform the user as to how to quit it (by entering a negative number for a length of the triangle’s side).
(10 Bonus Marks) Write a second program based on the program in b that it takes the x, y and z coordinates as user input and then computes the lengths of the triangle’s sides before computing the area.
Explanation / Answer
question a)
%matlab code
display('enter negative values of sides to quit')
a = input('enter a:');
b = input('enter b:');
c = input('enter c:');
while(a>0&&(b>0)&&(c>0))
s = (a+b+c)/2;
areaofTraingle = sqrt(s*(s-a)*(s-b)*(s-c))
display('enter negative values of sides to quit')
a = input('enter a:');
b = input('enter b:');
c = input('enter c:');
end
%end of code
question b)
%matlab code
clc;
close all;
clear all;
x = zeros(1,2);
y = zeros(1,2);
z = zeros(1,2);
x = input('enter point1 x(1) x(2) :');
y = input('enter point2 y(1) y(2):');
z = input('enter point3 z(1) z(2):');
a = sqrt((x(1)-y(1))^2+(x(2)-y(2))^2);
b= sqrt((z(1)-y(1))^2+(z(2)-y(2))^2);
c = sqrt((x(1)-z(1))^2+(x(2)-z(2))^2);
while(a>0&&(b>0)&&(c>0))
s = (a+b+c)/2;
areaofTraingle = sqrt(s*(s-a)*(s-b)*(s-c))
x = input('enter point1 x(1) x(2) :');
y = input('enter point2 y(1) y(2):');
z = input('enter point3 z(1) z(2):');
a = sqrt((x(1)-y(1))^2+(x(2)-y(2))^2);
b= sqrt((z(1)-y(1))^2+(z(2)-y(2))^2);
c = sqrt((x(1)-z(1))^2+(x(2)-z(2))^2);
end
%end of code
note: in question b enter input values of coordinate points as arrays
for example:
enter point1 x(1) x(2) :[2 3]
enter point2 y(1) y(2):[4 5]
enter point3 z(1) z(2):[5 6]
areaofTraingle =
1.2277e-07
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.