1

I am trying to make a function for MATLAB and I just don't see how to set it up right. I set up the function and then I do it manually with a calculator and I seem to get a different answer. I am sure it is the way I set it up. If someone can point out the problem I would greatly appreciate it.

I am trying to make the following function:

x*e^(cos(x))

what I wrote in MATLAB:

function y = fun2(a)

  y = a*exp(cos(a))

end
2
  • A couple of tips for the future. I changed mathlab into MATLAB. I'm pretty sure this is not mathlab. Also, when you ask a question like this, provide the inputs that you used, and the outputs. When you say you get two different answers, show us what those answers are. That's always useful. Without that information, we may have to guess. Oct 14, 2012 at 11:41
  • You asked the question, but have been quiet since. Do you still need help? Oct 14, 2012 at 18:02

1 Answer 1

2

Your code is just fine. The most likely explanation is that your calculator is in deg mode and so treats the argument to cos as an angle measured in degrees. Computer code like MATLAB code always use radians rather than degrees. Switch your calculator to rad mode and the two evaluations will agree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.