I am struggling with integrating a function.

I have this function that I have to integrate:
And I added inline function to matlab as such:
f=inline('sqrt(1+((66*x)^2/((1.2*x^2)+3))','x')
f =
Inline function:
f(x) = sqrt(1+((66*x)^2/((1.2*x^2)+3))
However, it gives me this error:
> int(f(x),x)
Error using inlineeval
Error in inline expression ==> sqrt(1+((66*x)^2/((1.2*x^2)+3))
Error: This statement is incomplete.
Error in indexing (line 23)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr); %#ok<DILEVAL>
How can I fix this? I would be so grateful for a help!

5 comentarios

Torsten
Torsten el 26 de Ag. de 2023
Integration variable and upper limit of the integral both have the same name (x). That doesn't make sense.
As far as I know, that's a common occurence; basically obtaining another function with the upper limit as the independent variable.
syms x z
y(x) = x^2+x+1;
i1 = int(y,x,0,x)
i1 = 
i2 = int(y,x,0,z)
i2 = 
Torsten
Torsten el 26 de Ag. de 2023
Editada: Torsten el 26 de Ag. de 2023
Maybe common, but often misleading from my experience.
Especially if you want to define a function of the upper limit:
F = @(x)integral(@(x)x.^2,0,x)
It works, but it's quite confusing in my opinion.
Dyuman Joshi
Dyuman Joshi el 26 de Ag. de 2023
I'd say rather confusing than misleading.
What you have wrote is the numerical version of the symbolic integration; idk what's your point with that but it does work.
Torsten
Torsten el 27 de Ag. de 2023
Editada: Torsten el 27 de Ag. de 2023
In MATLAB, many things "work", also the unintended ones.
Coming from the FORTRAN generation, I often wonder if this is help or curse for the programmers.
But that's an old question discussed many times in the forum.

Iniciar sesión para comentar.

 Respuesta aceptada

John D'Errico
John D'Errico el 27 de Ag. de 2023
Editada: John D'Errico el 27 de Ag. de 2023
Don't use inline functions. They are slow, inefficient things, introduced long ago, in a galaxy far, far away. Instead use function handles, or anonymous functions. (They remain unnamed. Sort of the Voldemort of functions.)
Next, decide if you are going to use a numerical integration utility, thus integral, or a symbolic one, so int. The two are not the same. You used int, but then used it on a function handle.
f = @(x) sqrt(1+(66*x./(1.2*x.^2+3)).^2);
Note my use of the dotted oeprators. They are important to make a numerical code work. Also, see this is a function handle. We can evaluate f at any numerical value for x.
f(2)
ans = 16.9526
Or, we can substitute in a symbolic variable...
syms X
f(X)
ans = 
Since you have not specified limits to the integral, I will presume you want a symbolic result.
int(f(X),[0,X])
ans = 
A problem is that MATLAB did not find an analytical solution for that integral, so it just returned the original expression. That may force you to use numerical integrations, but then you won't get a pretty result.
Had you wanted to perform a numerical integration, you would do it like this:
integral(f,0,3)
ans = 42.1297
There, with limits of [0,3] for the integral, or
vpaintegral(f(X),0,3)
ans = 
42.1297
Finally, you might have decided to try other methods. For example, if we do a Taylor series on f, we get this:
taylor(f(X),'order',20)
ans = 
Now, we might have decided to integrate the Taylor series, we would expect it would not be viable except for small upper limits on X. For example...
vpa(int(taylor(f(X),'order',20),0,.01))
ans = 
0.010080087111363533488105224583383
vpaintegral(f(X),0,0.01)
ans = 
0.0100801
And that did reasonably well. But it will fail for even moderately sized upper limits.

2 comentarios

Betul
Betul el 27 de Ag. de 2023
I see thank you so much! It seems like it is not able to further solve the integration, as it does not take the funciton out.
Now, we might have decided to integrate the Taylor series, we would expect it would not be viable
except for small upper limits on X
format long g
f = @(x) sqrt(1+(66*x./(1.2*x.^2+3)).^2);
syms X UB
T = taylor(f(X),'order',20)
T = 
I = int(T, X, 0, UB)
I = 
ub = solve(I == realmax(), 'real', true)
ub = 
double(ub)
ans =
1.32548992166858e+15
Not such a small upper bound.
... Though you lose precision badly at much much smaller values.

Iniciar sesión para comentar.

Más respuestas (1)

Star Strider
Star Strider el 26 de Ag. de 2023

0 votos

How can I fix this?
Be certain that tthe expression matches the symbolic expression in the figure. (It currently does not.) Then, be sure that the parentheses enclose the correct sub-expressions and the entire expression since you are taking the square root of all of it.
Also, see the documentation section on Anonymous Functions.

1 comentario

f=inline('sqrt(1+((66*x)^2/((1.2*x^2)+3))','x')
% 1 2 34 3 45 4 32 1
... should be 0 at the end of the line.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 26 de Ag. de 2023

Comentada:

el 28 de Ag. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by