I have a task where I have to calculate the area of a triangle from degrees 1 to 90, that is bound within a semi circle. I keep getting an error with the last line for some reason

1 回表示 (過去 30 日間)
I have a task where I have to calculate the area of a triangle from degrees 1 to 90, that is bound within a semi circle. I keep getting an error with the last line for some reason.
radius = rand;
a = 2*radius;
alpha = (1:90)
b = a * cosd(alpha(1:90));
area = 0.5*a*sind(1:90)
triangle= area*b
  2 件のコメント
Rik
Rik 2020 年 10 月 19 日
Question originally posted by Beenish Shabir (retrieved from Google cache):
I have a task where I have to calculate the area of a triangle from degrees 1 to 90, that is bound within a semi circle. I keep getting an error with the last line for some reason
I have a task where I have to calculate the area of a triangle from degrees 1 to 90, that is bound within a semi circle. I keep getting an error with the last line for some reason.
radius = rand;
a = 2*radius;
alpha = (1:90)
b = a * cosd(alpha(1:90));
area = 0.5*a*sind(1:90)
triangle= area*b

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 10 月 16 日
You need to be using .* instead of * to do an elemenet by element multiplication instead of a matrix multiplication. Try this:
radius = 2 * rand;
alpha = (1:90)
bases = radius * cosd(alpha) % A vector of 90 elements
heights = radius * sind(alpha) % A vector of 90 elements
triangle_areas = 0.5 * bases .* heights % A vector of 90 elements
% For optional fun, plot the area.
plot(alpha, triangle_areas, 'b.-', 'LineWidth', 2);
grid on;
xlabel('Angle', 'FontSize', 18);
ylabel('Area', 'FontSize', 18);
title('Triangle Area vs. Angle', 'FontSize', 18);
  2 件のコメント
Beenish Shabir
Beenish Shabir 2020 年 10 月 17 日
Thank you so much!! You are such a life saver :)
Image Analyst
Image Analyst 2020 年 10 月 17 日
You can Accept only one answer, so accept the best one. However you can also vote for that and the other(s) to award those persons additional reputation points also.

サインインしてコメントする。

その他の回答 (1 件)

meghannmarie
meghannmarie 2020 年 10 月 16 日
編集済み: meghannmarie 2020 年 10 月 16 日
You need to use the dot operator if you are doing element wise multiplication (.*). You use just the asterick for matrix multiplication.
triangle= area.*b

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by