I am struggling with integrating a function.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Betul
le 26 Août 2023
Commenté : Walter Roberson
le 28 Août 2023
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 commentaires
Dyuman Joshi
le 26 Août 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.
Réponse acceptée
John D'Errico
le 27 Août 2023
Modifié(e) : John D'Errico
le 27 Août 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)
Or, we can substitute in a symbolic variable...
syms X
f(X)
Since you have not specified limits to the integral, I will presume you want a symbolic result.
int(f(X),[0,X])
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)
There, with limits of [0,3] for the integral, or
vpaintegral(f(X),0,3)
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)
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))
vpaintegral(f(X),0,0.01)
And that did reasonably well. But it will fail for even moderately sized upper limits.
2 commentaires
Walter Roberson
le 28 Août 2023
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)
I = int(T, X, 0, UB)
ub = solve(I == realmax(), 'real', true)
double(ub)
Not such a small upper bound.
... Though you lose precision badly at much much smaller values.
Plus de réponses (1)
Star Strider
le 26 Août 2023
‘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.
1 commentaire
Walter Roberson
le 26 Août 2023
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.
Voir également
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!