Unexpected MATLAB Answer Explained: Solving Complex Valued Equations | Homework

  • Thread starter sara_87
  • Start date
  • Tags
    Matlab
In summary, the conversation was about finding the reason for a complex valued answer when using a specific value in MATLAB. It was determined that this could be due to rounding error and the boundary of the real domain for the asin() function. To obtain a real answer, the code can be modified to use the real() function or test and clip the argument to the appropriate range.
  • #1
sara_87
763
0

Homework Statement



I put the following code into MATLAB:

d=(0.2+0.1)-0.2

y=(asin((d)/(0.1)))

The answer gives:

d=0.100000000000000

and i know that d/0.1 is equal to 1. so, i expect the answer of y to be
y=1.570796326794897 (which is pi/2)
but, MATLAB gives:

y= 1.570796326794897 - 0.000000021073424i

Homework Equations





The Attempt at a Solution



if i put:
d=0.1
then it would give:
y= 1.570796326794897

But, i want to know why the answer is complex valued when it uses 0.1000000000000000 ?

Thank you in advance.
 
Physics news on Phys.org
  • #2
sara_87 said:
But, i want to know why the answer is complex valued when it uses 0.1000000000000000 ?

Thank you in advance.

The only reason I can think of is rounding error. The computer thinks .1000000000000 > .1, so when computing d/.1, you obtain a value greater than 1. Taking the arcsin of a value greater than 1, as you know, does not exist for real numbers. This is why there's a small complex term which contributes to the answer.
 
  • #3
Hi Sara87. Floating point numbers on a computer generally can't be represented exactly, but this is usually hidden from the user by limiting the number of significant digits to which results are displayed. What's unusual in this case is that you are operating precisely on the boundary of the real domain (and the extended complex domain) of the asin() function.

BTW. Are you familiar with complex numbers?

Code:
> format long

> d=0.3
d =  0.300000000000000
> ((d - 0.2)/0.1)*1e16
ans = 9999999999999998

> d = 0.2+0.1
d = 0.300000000000000
> ((d - 0.2)/0.1)*1e16
ans = 10000000000000002
 
Last edited:
  • #4
Thank you both.
Yes, i am familiar with the complex numbers.
So, how can i change my code so that is gives pi/2 instead of pi/2+ some complex number ?
 
  • #5
There are several possible ways.

The simplest, if you're certain your result should be real, is: y = real(asin(...))

OR you could test if the argument is in [-1-delta ... +1+delta], then throw an error if it isn't and clip it back to [-1...1] if it is.
 
  • #6
Thank you. I will use y=real(asin(...))
 

FAQ: Unexpected MATLAB Answer Explained: Solving Complex Valued Equations | Homework

Why am I getting an unexpected matlab answer?

This is a common question when working with matlab. The most likely reason for an unexpected answer is a mistake in your code, such as a typo or a mathematical error. It's important to carefully review your code and make sure all variables and syntax are correct.

How do I troubleshoot an unexpected matlab answer?

If you're not sure where the mistake is in your code, try using the debugging tools in matlab. You can set breakpoints and step through your code to see where the unexpected answer is occurring. Also, make use of the command window to check the values of variables and see the output of each line of code.

Can an unexpected matlab answer be caused by incorrect input?

Yes, incorrect input can lead to unexpected answers. Make sure you are using the correct data type and format for your input. For example, if your code is expecting a string but you input a number, it may produce an unexpected answer.

Is it possible for an unexpected matlab answer to be caused by a bug in the program?

While it's always possible for a bug to exist in any program, matlab is a well-tested and reliable software. If you're consistently getting unexpected answers, it's more likely that there is an error in your code rather than a bug in the program itself.

How can I avoid unexpected matlab answers in the future?

The best way to avoid unexpected matlab answers is to practice good coding habits. This includes using clear and consistent variable names, commenting your code, and testing your code with different inputs. It's also helpful to break your code down into smaller, manageable chunks and test each section individually before putting it all together.

Similar threads

Replies
10
Views
2K
Replies
2
Views
4K
Replies
2
Views
3K
Replies
4
Views
3K
Back
Top