Unexpected MATLAB Answer Explained: Solving Complex Valued Equations | Homework

  • Thread starter Thread starter sara_87
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB code snippet that produces a complex-valued result when calculating the arcsine of a value derived from floating-point arithmetic. Participants explore the reasons behind this unexpected output, focusing on the implications of numerical precision and representation in computing.

Discussion Character

  • Homework-related, Technical explanation, Debate/contested

Main Points Raised

  • One participant notes that the output of the variable d is slightly different from the expected value due to floating-point precision, suggesting that rounding errors may lead to a value greater than 1 when calculating d/0.1.
  • Another participant explains that floating-point numbers cannot be represented exactly, which can lead to unexpected results when operating near the boundaries of the real domain of the asin() function.
  • A suggestion is made to use the real part of the arcsine function to avoid the complex component, or to implement checks to ensure the argument remains within the valid range for real results.
  • Participants discuss the implications of using floating-point arithmetic and how it affects the results of mathematical functions in programming.

Areas of Agreement / Disagreement

Participants generally agree that floating-point precision issues are likely responsible for the complex result, but there is no consensus on the best approach to handle the situation or whether the output should be adjusted.

Contextual Notes

Limitations include the inherent inaccuracies in floating-point representation and the specific behavior of the asin() function at the boundaries of its domain.

Who May Find This Useful

This discussion may be useful for students and practitioners working with numerical computing, particularly those using MATLAB and dealing with floating-point arithmetic and complex numbers.

sara_87
Messages
748
Reaction score
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
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.
 
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:
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 ?
 
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.
 
Thank you. I will use y=real(asin(...))
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K