-1

When trying to calculate the Integral of two variables, I got nothing!

Here is the code:

syms x;
a=0.4;
theta=(9 - 4*x*(5*x - 141/25))^(1/2)/2 - 3/2;
theta_prime=-(40*x - 564/25)/(4*(9 - 4*x*(5*x - 141/25))^(1/2));
g=(1/theta)*theta_prime
s=(1+a*(theta-1))*(g)^2
sgen = int(s,x,0.1,1) %x=0.1:0.1: 1

What was my mistake? It supposed to be one value, such as '4.86'. Please advise.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182

1 Answers1

4

sgen is a symbolic object representing the integral of your function s. You can cast it to double to obtain a numerical value for your integral:

syms x;
a=0.4;
theta=(9 - 4*x*(5*x - 141/25))^(1/2)/2 - 3/2;
theta_prime=-(40*x - 564/25)/(4*(9 - 4*x*(5*x - 141/25))^(1/2));
g=(1/theta)*theta_prime;
s=(1+a*(theta-1))*(g)^2;
sgen = double(int(s,x,0.1,1)) % returns 4.8694

But if you're not interested in the symbolic equation for the integral, there really is no point in using the symbolic toolbox for this. It is much faster to compute the integral numerically. One way to do so is to create a function s(x) and then use integral to find the numerical integration. Do note that s(x) must be vectorized on the x variable for this to work (integral will call it with a vector of x values to save time). For vectorized computation, it is necessary to add dots in front of some of the *, / and ^ operators. This is the result:

a = 0.4;
theta = @(x) (9 - 4*x.*(5*x - 141/25)).^(1/2)/2 - 3/2;
theta_prime = @(x) -(40*x - 564/25)./(4*(9 - 4*x.*(5*x - 141/25)).^(1/2));
g = @(x) (1./theta(x)).*theta_prime(x);
s = @(x) (1+a*(theta(x)-1)).*g(x).^2;
sgen = integral(s,0.1,1.0) % returns 4.8694
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Just got same [result with WolframAlpha](https://www.wolframalpha.com/input/?i=integral%28%281%2B0.4*%28%289+-+4*x*%285*x+-+141%2F25%29%29%5E%281%2F2%29%2F2+-+3%2F2-1%29%29*%28%281%2F%28%289+-+4*x*%285*x+-+141%2F25%29%29%5E%281%2F2%29%2F2+-+3%2F2%29%29*-%2840*x+-+564%2F25%29%2F%284*%289+-+4*x*%285*x+-+141%2F25%29%29%5E%281%2F2%29%29%29%5E2%2Cx%2C0.1%2C1%29) – Cedric Zoppolo Jan 02 '20 at 17:25
  • @Cris_Luengo I found my error and then saw you comment. Everything matches now. Thanks – Cedric Zoppolo Jan 02 '20 at 17:27
  • Great. Highly appreciated. – user3311415 Jan 02 '20 at 18:37
  • 1
    @user3311415 Accept the answer (and don't vandalize your question) if it helped you. – S.S. Anne Jan 02 '20 at 21:58