Matlab question about equality check

  • Thread starter yaang
  • Start date
  • Tags
    Matlab
In summary: By adjusting the tolerance, you can check for equality with different levels of precision. In summary, for problems with floating-point numbers, using the isequal() function with a tolerance can help identify and fix errors in your code.
  • #1
yaang
23
0
Matlab question about equality check, decimals

Complete code is attached as an .m file

Code basically finds the inverse kinematics of a 2 part robot arm. There should be 4 mathematical solutions and 2 of these solution should physically solve this system. Problem is one of the correct solutions(3rd solution) find the Py value to be 2.49999999999 where it should be 2.50000000. How can i find out where this error is coming from and how can i fix it ?

My code checks if the Py value is 2.5 and since it isn't according to MATLAB (it should be exactly 2.5 i don't understand why MATLAB thinks otherwise) it returns false. Any way around this problem will be appreciated.
 

Attachments

  • invkin.m
    3.6 KB · Views: 384
Last edited:
Physics news on Phys.org
  • #2
You can use the Matlab function isequal() to compare floating-point numbers that might differ in precision. The syntax for isequal() is:isequal(A,B,tol)where A and B are the values you wish to compare, and tol is the tolerance (in this case, the maximum difference allowed between A and B).For example, if your Py value is 2.49999999999, and you want to check if it is equal to 2.5 (within a tolerance of 0.00001), you can use the following code:isequal(2.49999999999,2.5,0.00001)This will return true, indicating that the two values are equal within the given tolerance.
 

FAQ: Matlab question about equality check

What is an equality check in Matlab?

An equality check in Matlab is a comparison operation that checks whether two values or expressions are equal to each other. It returns a logical value of either true or false.

How do I perform an equality check in Matlab?

To perform an equality check in Matlab, you can use the double equal sign (==) operator. For example, to check if the variables x and y are equal, you can use the expression x == y.

What is the difference between the single equal sign (=) and the double equal sign (==) in Matlab?

The single equal sign (=) is used for variable assignment in Matlab, while the double equal sign (==) is used for equality checks. The single equal sign assigns a value to a variable, while the double equal sign compares two values or expressions and returns a logical value.

What happens if an equality check in Matlab results in an error?

If an equality check in Matlab results in an error, it means that the values or expressions being compared are not of the same type or are not compatible. Make sure to check the data types before performing an equality check to avoid errors.

Can an equality check be performed on arrays or matrices in Matlab?

Yes, an equality check can be performed on arrays or matrices in Matlab. It will compare each element in the array or matrix and return a logical array or matrix with the same dimensions, indicating whether the elements are equal or not.

Similar threads

Replies
13
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
4
Views
17K
Replies
12
Views
2K
Replies
1
Views
6K
Replies
3
Views
2K
Back
Top