Reputation: 13
i am trying to integrate a power sum raised to power of 1.2.
The question is this integrate (((t^1)+(t^2)+(t^3))^(1.2)) from 0 to 1, with respect to t.
x=1:3
syms t
y=sum(t.^x)
fun=@(y) y^(1.2)
integral(fun,0,1)
Output as: Error using ^ Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.
But I'm not trying to compute elementwise.
Any comment/insight would be helpful. Thank you.
Upvotes: 1
Views: 74
Reputation: 36710
You are mixing symbolic math (syms
and sum
) with a numeric function to evaluate the integral (integarl
). While it is possible, it is typically not a good idea because you end up with the precision problems of a numeric solution and the bad performance of the symbolic math toolbox. If you want a numeric solution, do not use any function from the symbolic toolbox. If you want to solve it with the symbolic math toolbox, maybe getting a analytic result, use int
from the symbolic toolbox.
To explain what happened in your case. integral
evaluates your function for multiple y-values to calculate the integral, something like fun([0,.5,1])
. Your function calculates y^1.2 which is not possible, you want the element-wise operation in this case.
A further problem is, that the first y
you assign is unused. The y
in the next line where you define fun
is a new variable.
This answer does not contain a solution because I do not know if a symbolic or numeric solution is intended.
Upvotes: 1
Reputation: 1345
I think, in your last line you refer to the generic y
that has nothing to do with y
you have specified before. so, instead of fun
you need fun(y)
. Then, since the output of your fun
is symbolic expression, then you need to convert this expression to the function handle using matlabFunction
. So, the final code would look like:
x=1:3
syms t
y=sum(t.^x)
fun=@(y) y^(1.2)
integral(matlabFunction(fun(y)),0,1)
Output:
1.1857
Hope that helps, good luck!
Upvotes: 3