I need some assistance with my function program?

I'm not sure where my mistakes are. The following is my main program:
a = input('Enter value for side a: ');
if a <= 0
disp('Not a valid input! Side "a" must be greater than zero.');break;
end
b = input('Enter value for side b: ');
if b <= 0
disp('Not a valid input! Side "b" must be greater than zero.');break;
end
c = input('Enter value for side c: ');
if c <= 0
disp('Not a valid input! Side "c" must be greater than zero.');break;
end
if a > b+c
fprintf('\n');
disp('Error: Side "a" must be less than the sum of sides b & c.');break;
elseif b > a+c
fprintf('\n');
disp('Error: Side "b" must be less than the sum of sides a & c.');break;
elseif c > a+b
fprintf('\n');
disp('Error: Side "c" must be less than the sum of sides a & b.');break;
end
fprintf('\n');
[Angle_A,Angle_B,Angle_C] = angle_calc(a,b,c) % This is the correct name of my function program
My function program is:
function [law_cosines] = angle_calc (side_A,side_B,side_C)
angle_A = acosd((b^2+c^2-a^2)/(2*b*c));
angle_B = acosd((a^2+c^2-b^2)/(2*a*c));
angle_C = acosd((a^2+b^2-c^2)/(2*a*b));

Antworten (2)

Image Analyst
Image Analyst am 22 Jul. 2012

2 Stimmen

Your "function program" needs to look like this:
function [angle_A, angle_B, angle_C] = angle_calc (a, b, c)
angle_A = acosd((b^2+c^2-a^2)/(2*b*c));
angle_B = acosd((a^2+c^2-b^2)/(2*a*c));
angle_C = acosd((a^2+b^2-c^2)/(2*a*b));
Walter Roberson
Walter Roberson am 22 Jul. 2012

0 Stimmen

Your line
function [law_cosines] = angle_calc (side_A,side_B,side_C)
should be
function [angle_A, angle_B, angle_C] = angle_calc (side_A,side_B,side_C)

10 Kommentare

Aaron
Aaron am 22 Jul. 2012
Now getting the following error:
*Error in angle_calc (line 2)
angle_C = acosd((a^2+b^2-c^2)/(2*a*b)); *
It is set up like the others, as far as I can see.
Jan
Jan am 22 Jul. 2012
And what is the error message?
Aaron
Aaron am 22 Jul. 2012
Error in angle_calc (line 2)
angle_C = acosd((a^2+b^2-c^2)/(2*a*b));
Image Analyst
Image Analyst am 22 Jul. 2012
See my answer.
Does it give an error message such as "inner dimensions must agree" ?
Image Analyst
Image Analyst am 22 Jul. 2012
That's what I thought at first (passing in arrays instead of scalars), but it's because a, b, and c are undefined because he used different names for them in the input arguments.
Jan
Jan am 22 Jul. 2012
Bearbeitet: Jan am 22 Jul. 2012
@Aaron: You've posted, where the error occurs, but not the description of the actual problem. But this message would reveal the cause immediately.
Good catch about the names!
Image Analyst
Image Analyst am 22 Jul. 2012
Well because he didn't list the actual error (like Jan noted) I actually had to copy and paste, fix it up a little, and run it to find out the error. This should have been discovered immediately by Aaron, particularly if he knew how to use the debugger (which is always faster than debugging via Answers postings).
Aaron
Aaron am 22 Jul. 2012
Thank you all for your input. I am fairly new to Matlab so I will do my best to learn how to use the debugger.

Diese Frage ist geschlossen.

Tags

Gefragt:

am 22 Jul. 2012

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by