Reputation: 11
I am trying to calculate the integral between two float points. The programm can do it, but it sends this next messagge
warning: passing floating-point values to sym is dangerous, see "help sym"
This is my code
i=2.5;
syms x y
f=((2*x-1)/10)*((2*y-1)/10)/(x+y);
variable=double(int(int(f,x,i,1),y,i,1));
is there anyway to do it whithout that inconvenient?
Upvotes: 0
Views: 81
Reputation: 1665
I assume you're casting to double at the end because of the answer you got to your other question: Could someone explain this error on fprintf() in Octave?, where casting your answer to double was suggested so that you could pass it to fprintf.
Octave is giving you a warning about combining floating type and symbolic numbers. This is a potential concern because symbolic numbers are stored 'exactly', while floating points may not be, depending on their value. But it is only a warning. Not recommended, but you can disable warnings. See https://docs.octave.org/latest/Enabling-and-Disabling-Warnings.html
The problem is occurring because you are using a float value 2.5 in your integration limits. To avoid the issue, define it as a symbolic value:
pkg load symbolic;
syms x, y;
i = sym("2.5")
i = (sym) 5/2
variable = int(int(f,x,i,1),y,i,1)
variable = (sym)
11*log(5) log(2) 33 28*log(7/2)
- --------- - ------ + --- + -----------
30 150 200 75
variable = double (variable)
variable = 0.037950
As suggested in a comment above, if you have no need for a symbolic answer to your question, and you're casting it immediately to a floating point number, you might find it easier to do numeric integration instead. That could be accomplished for your problem as follows:
# define equation as a function. Note use of .* and ./ 'elementwise'
# operators to avoid unwanted matrix multiplication/division.
f = @(x,y) ((2*x-1)/10).*((2*y-1)/10)./(x+y);
# Solve the 2D integral, returning a numeric result
variable = integral2 (f, 2.5, 1, 2.5, 1)
variable = 0.037950
Upvotes: 1