0

Totally new at MatLab and I need it to answer this homework question for my class. I'm trying to see when this equation (mu) will equal a certain value (sae30mu) while decreasing x so that I can find the correct value for x. However, when it prints x, I get a negative number. I cannot tell why this code won't work, as it appears (to me) that it should just spit out the answer.

  x = 1.0;
   mu = ((sae10mu)^x)*((sae50mu)^(1-x));

   while (mu ~= sae30mu & x >= 0)
       x = x - 0.01;
       mu = ((sae10mu)^x)*((sae50mu)^(1-x));
   end

   x

1 Answer 1

4

There are two problems here.

Firstly, you aren't getting the right answer because mu will never equal sae10mu, so mu~=sae10mu will always be true. Limits of numerical precision mean that you can't do floating point comparison like this. Instead, you need to define some tolerance, tol, and do a comparison like abs(mu-sae10mu)>tol. Then, once mu is within tol of sae10mu, the condition will be true.

Secondly, the x>=0 will be true until x is less than 0. The first value you will get to is the largest value of x less than 0, which is -0.01. To stop at x=0, do x>0.

Your while loop should look like this: while(abs(mu-sae10mu)>tol & x>0).

Sign up to request clarification or add additional context in comments.

1 Comment

Technically it should also use && instead of just & as well

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.