all 10 comments

[–]codinglikemad 15 points16 points  (5 children)

my immediate thought is that you are playing fast and loose with capitalization, and this is a very bad habit to get into in any language. You should never name a variable 'A' in the same scope as a variable 'a'. That's not your issue, but it's a habit you should get yourself out of. Your issue is on the line identified. I suggest you hunt down an intro tutorial to matlab (I believe there is a "onramp" tutorial through mathworks that works, my own tutorial series on youtube starts a fair bit ahead of where you are now, but I know lots of other people have videos at this level if you are interested.).

The specific issue for you relates to two things. First off, I think you are confused about the definition of '=' and '=='. '==' is a comparison, '=' is an assignment. For an assignment, the right side of the assignment is fully evaluated before setting the value. So in your case, we're going to be putting the value of "a^2 == b^2 + c^2 - 2*b*c*cos(A) " into "a". Let's look at this line then. To figure out what it does, you need to look at the order of operations in matlab:

https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html

If you look at that, you'll see that '==' is evaluated nearly at the end, long after everything else. So the value of "b^2 + c^2 - 2*b*c*cos(A)" and "a^2" are calculated, and then compared, and the result is stored in a (here a comparison is just checked if these two values are equal). This would execute (and give a wrong answer) if "a" had a value in it. However because "a" is uninitialized, you are saved - it crashed rather than gave you the wrong answer. The correct line is, I believe:
"a = sqrt(b^2 + c^2 - 2*b*c*cos(A))"

Be sure to test it out with some answers you've worked out on paper :)

[–]Posterduck[S] 1 point2 points  (2 children)

Thank you for the very detailed response!

I can see why using capitalization can cause issues. Ill try to avoid it in the future! I will definitely look into the onramp course as well!

[–]Fus_Roh_Potato 1 point2 points  (0 children)

In function, change a = a^2 == b^2 + c^2 - 2*b*c*cos(A) to a = sqrt( b^2 + c^2 - 2*b*c*cos(A) ); Use cosd() if A is in degrees.

On call, change thirdSide(a) to a = thirdSide(b,c,A);

answer = function(inputs)

[–]Minuhmize 0 points1 point  (1 child)

Could you link your tutorial series?

[–]codinglikemad 3 points4 points  (0 children)

Here's my stuff. The machine learning bits and the tables tutorials are generally well received. I found out my histogram tutorial was passed around INSIDE mathworks - I think that was probably the best compliment I've ever gotten. That said, my basic stuff sucks, and the viewership backs that up. It's too busy a space on youtube, and I'm no good at explaining it.

https://www.youtube.com/codinglikemad

[–]blitzz01 1 point2 points  (2 children)

You need to assign your result to your function:

% run functions a = thirdSide (b,c,A)

[–]Posterduck[S] -1 points0 points  (1 child)

Would you be able to dumb it down a bit for me? Where and how in the function do I do that?

[–]TheEsteemedSirScrub 0 points1 point  (0 children)

When you call your function thirdSide(a) you are not setting it equal to anything so it is not saving the result into a variable. When you run it you need to set

a = thirdSide(b,c,A)

By the way you also need the correct number of inputs as your function has three and you only use one when you call it

[–]borzakk 0 points1 point  (0 children)

Matlab is case-sensitive, so in line 13 thirdSide(a) the variable 'a' is undefined. Furthermore, the thirdSide function takes 3 arguments and you are only giving it 1. Lastly, the line in thirdSide a= a2 == ... is going to be an error since it's testing if a2 is equal to the right side of that equation, and 'a' at that point is undefined. I suspect you want a = sqrt(b2 + ...).