solving an integral to get a numerical answer

I am struggling to solve an integral and get a definite numerical answer from it in Matlab
Here is the code that I am using:
syms r C z x I u_0
k(r,C,z) = (2*sqrt(r*C))/(sqrt(r+C)^2+z^2);
fun_K(r,C,z) = 1/(sqrt(1-k^2*sin(x)^2)); % function K
K(r,C,z) = int(fun_K,x,0,pi/2); % integral of function K
fun_E(r,C,z) = sqrt(1-k^2*sin(x)^2); % function E
E(r,C,z) = int(fun_E,x,0,pi/2); % integral of function E
% print out result
r = 0.004; C = 0.025; z = 0.03;
K(r,C,z)
E(r,C,z)
From this, I get the elliptics:
K(r,C,z) = ellipticK(40000/89401)
E(r,C,z) =ellipticE(40000/89401)
Is it possible to get a numeric answer from this to then use later in calculation or am I doing something fundamentally wrong here?
Thanks in advance.

1 Comment

sorry corrected some errors in the posted code and result.

Sign in to comment.

 Accepted Answer

First, you can probably use the double function, or the vpa function first, then double.
Second, your code throws this error:
Undefined function or variable 'x'.
fun_K(r,C,z) = 1/(sqrt(1-k^2*sin(x)^2)); % function K
When I try to run what you’ve posted.
I could be of more help if you correct that, since neither MATLAB nor I know what ‘x’ refers to here.

10 Comments

sorry, should be fixed now, replace the syms t with x. Meanwhile I will try what you have suggested and return
Your answer seems to have done exactly what I needed. I have posted the code below for any who need it.
syms r C z x I u_0
k(r,C,z) = (2*sqrt(r*C))/(sqrt(r+C)^2+z^2);
fun_K(r,C,z) = 1/(sqrt(1-k^2*sin(x)^2)); % function K
K(r,C,z) = int(fun_K,x,0,pi/2); % integral of function K
fun_E(r,C,z) = sqrt(1-k^2*sin(x)^2); % function E
E(r,C,z) = int(fun_E,x,0,pi/2); % integral of function E
% print out result
r = 0.004; C = 0.025; z = 0.03;
double(vpa(K(r,C,z)))
double(vpa(E(r,C,z)))
Thanks again
I don’t see any changes.
You need to integrate with respect at least to one of the variables of the function you’re integrating. Please note that ‘x’ is not in any of them.
Still waiting for the correct code ...
fun_K and fun_E both have the term x... so I am integrating with respect to x here am I not?
fun_K(r,C,z) = 1/(sqrt(1-k^2*sin(x)^2))
fun_E(r,C,z) = sqrt(1-k^2*sin(x)^2);
The problem is that ‘x’ is not in the argument list of either function.
Try my (slightly modified) version of your code:
syms r C z t I u_0 x
k(r,C,z) = (2*sqrt(r*C))/(sqrt(r+C)^2+z^2);
fun_K(r,C,z,x) = 1/(sqrt(1-k^2*sin(x)^2)); % function K
K(r,C,z) = int(fun_K,x,0,pi/2); % integral of function K
fun_E(r,C,z,x) = sqrt(1-k^2*sin(x)^2); % function E
E(r,C,z) = int(fun_E,x,0,pi/2); % integral of function E
% print out result
r = 0.004; C = 0.025; z = 0.03;
intK = double(K(r,C,z))
intE = double(E(r,C,z))
producing:
intK =
1.81192176849785
intE =
1.37665682183551
Does that do what you want?
The code that I have pasted above, within this comment chain, (second comment to your initial answer) and your code have exactly the same outputs.
In my original post before correcting the error, I had stated the variable t instead of x because I had incorrectly pasted from an in progress bit of code. This was quickly corrected in the original question as I had said earlier. It is a subtle change and you may not have caught it.
It is not necessary to include x as a variable within the function as long as it is listed above under syms other than perhaps for better readability. It is left as a symbolic variable which is then integrated with respect to in the following line of code. Perhaps it has an impact on the speed of operation where yours may be be better. This I do not know.
It is not necessary to include x as a variable within the function as long as it is listed above under syms other than perhaps for better readability.
It is, if you want to integrate with respect to it.
But this is integrating with respect to it.
K(r,C,z) = int(fun_K,x,0,pi/2);
It is not, unless you specify it as an argument to the function.
Star Strider:
When you use a symbolic function such as fun_K as the argument, and you specify a symbolic variable to integrate with respect to, then int() will integrate with respect to that variable, even if it is not an argument to the function. The effect is as-if you had called the function with the symbolic variables that are the arguments to the function, giving a symbolic expression, which is then integrated with respect to the variable that you specified for integration, and the result is then symfun() with respect to the inputs to the symbolic function.
>> syms r C z x I u_0
k(r,C,z) = (2*sqrt(r*C))/(sqrt(r+C)^2+z^2);
fun_K(r,C,z) = 1/(sqrt(1-k^2*sin(x)^2)); % function K
>> int(fun_K,x,0,pi/2)
ans(r, C, z) =
piecewise(z^2 + C + r ~= 0, ellipticK((4*C*r)/(C + r + z^2)^2))
>> fun_K(r,C,z)
ans =
1/(1 - (4*C*r*sin(x)^2)/(z^2 + C + r)^2)^(1/2)
>> int(fun_K(r,C,z),x,0,pi/2)
ans =
piecewise(z^2 + C + r ~= 0, ellipticK((4*C*r)/(C + r + z^2)^2))

Sign in to comment.

More Answers (1)

Mister Tea
Mister Tea on 23 Dec 2018
Edited: Mister Tea on 23 Dec 2018
syms x Variable
y(Variable) = 2*x * Variable;
Integral(Variable) = int(y,x,0,pi)
Output:
Integral(Variable) =
Variable*pi^2
integrating the function y with respect to x without calling it in the argument.
I will go do other things now, thank you for your first answer

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!