Hello Rohith,
As per my knowledge ,the reason for the difference between 'val1' and 'val3' in your MATLAB code is due to the way MATLAB handles symbolic computations using the "symsum" function.
- In the case of 'val1', you are directly evaluating the Bessel function "besseli(5,6)" numerically and storing the result as a double-precision floating-point number.
- In the case of 'val2', you are using the symsum function to symbolically compute the sum of Bessel functions. However, when you later convert it to a double with "double(val2)", MATLAB is performing a numerical approximation to evaluate the symbolic expression, and this can introduce some numerical error.
This discrepancy in the numerical approximation can lead to small differences between 'val1' and 'val3'. When dealing with symbolic computations, especially with functions that involve special functions like Bessel functions, numerical approximation errors can occur and accumulate, especially for large values.
Using vpa (Variable Precision Arithmetic) helps to achieve higher precision in symbolic computation and may reduce the discrepancy between val1 and val3.
You can refer to the following MathWorks documentation for more details
Hope that will help.