I am struggling with integrating a function.

2 次查看(过去 30 天)
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-8-26
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 2023-8-27
编辑:Torsten 2023-8-27
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.

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2023-8-27
编辑:John D'Errico 2023-8-27
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 个评论
Betul
Betul 2023-8-27
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.
Walter Roberson
Walter Roberson 2023-8-28
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.

请先登录,再进行评论。

更多回答(1 个)

Star Strider
Star Strider 2023-8-26
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 个评论
Walter Roberson
Walter Roberson 2023-8-26
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by