写一段求三角形面积的matlab代码
时间: 2024-05-12 19:03:36 浏览: 6
Sure, here's a MATLAB code to calculate the area of a triangle:
syms x1 y1 x2 y2 x3 y3;
A = sqrt((x1 - x3)^2 + (y1 - y3)^2);
B = sqrt((x2 - x1)^2 + (y2 - y1)^2);
C = sqrt((x3 - x2)^2 + (y3 - y2)^2);
s = (A + B + C) / 2;
area = sqrt(s * (s - A) * (s - B) * (s - C));
Note that this code uses the symbolic math toolbox in MATLAB to handle the square roots and other mathematical operations. You'll need to define the coordinates of the three vertices (x1, y1), (x2, y2), and (x3, y3) before running the code.
相关问题
写一段java代码求三角形的面积
好的,以下是求解三角形面积的Java代码:
```java
import java.util.Scanner;
public class TriangleArea {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入三角形的底边长:");
double bottom = input.nextDouble();
System.out.print("请输入三角形的高:");
double height = input.nextDouble();
double area = bottom * height / 2;
System.out.println("三角形的面积为:" + area);
}
}
```
上述代码中,我们使用Scanner类来获取用户输入的底边长和高,并使用公式 $S = \frac{1}{2}bh$ 来计算三角形的面积。最后输出计算结果即可。
使用c语言写一个求三角形面积的代码
以下是使用 C 语言编写的求三角形面积的代码:
#include <stdio.h>
int main() {
float base, height, area;
printf("请输入三角形的底边长:");
scanf("%f", &base);
printf("请输入三角形的高:");
scanf("%f", &height);
area = 0.5 * base * height;
printf("三角形的面积为:%f", area);
return 0;
}
注意:这个程序只适用于计算普通三角形的面积,不适用于计算等边三角形、等腰三角形等特殊情况。