Solve triple integral of a function that upload its value every step
Show older comments
I have to solve this TRIPLE integral:
P_4=integral3(fun_r,R1,R2,0,tau,z3,z4);
In order to solve this integral, it is NECESSARY to use a function handle.
So:
fun_r=@(R, theta, z) abs(Jr4)^2*R;
The problem is that fun_r depends on Jr4 but Jr4 uploads its value at each iteration, summing all the previous values (it is a summation).
If I consider Jr4 (computed only for the current iteration) and I put it immediately inside the brackets in fun_r, it works.
But I need to make the summation and in order to do so, I need to define Jr4 outside the function handle. But Jr4 depens on R,theta and z too. So I thought to do so:
syms z theta R
Xr4=A4d.*exp(gamma4.*z)+B4d.*exp(-gamma4.*z);
Jr4=real(Jr4+(real((Xr4.*(1./R).*besselj(np,alfak.*R).*exp(1i.*np.*(theta-OMEGA.*t))))));
fun_r=@(R, theta, z) abs(Jr4)^2*R;
P_4=integral3(fun_r,R1,R2,0,tau,z3,z4);
The error is:
Error using integral2Calc>integral2t/tensor (line 231)
Input function must return 'double' or 'single' values. Found 'sym'.
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral3/innerintegral (line 137)
Q1 = integral2Calc( ...
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral3 (line 121)
Q = integralCalc(@innerintegral,xmin,xmax,integralOptions);
Error in Untitled5 (line 91)
P_4=integral3(fun_r,R1,R2,0,tau,z3,z4);
- Is it possible to overcome this problem?
- Using THREE times the function 'q = integral(fun,xmin,xmax)', can be a solution?
Please help me.
11 Comments
Bjorn Gustavsson
on 14 Jul 2020
You will have to explain what is meant with "Jr4 uploads its value at each iteration". Is it some kind of integral equation definition of the function f? Is it some kind of recursive relation? Before that is explained it will be difficult to help you...
Luigi Stragapede
on 14 Jul 2020
Bjorn Gustavsson
on 14 Jul 2020
Well, that is a first step that will not be enough. I think you better use the Σ-button to insert the integral as a properly typset equation.
Luigi Stragapede
on 15 Jul 2020
Edited: Luigi Stragapede
on 15 Jul 2020
Bjorn Gustavsson
on 15 Jul 2020
In my text-frame there is a row of icons at the top. Third icon in above the "INSERT" there is a \Sigma-icon. That gives you the powers of Latex to write equations - which allows you much more precision to explain what your integral is. Use that.
Walter Roberson
on 15 Jul 2020
Input function must return 'double' or 'single' values. Found 'sym'.
You should use matlabFunction() to turn the symbolic expression into a function handle.
Pay attention to the 'vars' option, as that controls the order of variables and controls how they are grouped together into input arguments. For your purpose you might not need to group them, but you will certainly want to control the order of parameters for the generated function.
Luigi Stragapede
on 15 Jul 2020
Walter Roberson
on 15 Jul 2020
g=matlabFunction(r, 'vars', [y z x])
will create a function, g, with three input arguments, in the order y, z, x. Which is sometimes exactly what you need. If you do not use the 'vars' option then symvar() order is used, which would be x, y, z in this case.
If you were to use
g=matlabFunction(r, 'vars', {[y z x]})
then the output function, g, would expect a single input parameter that was had y, z, x as the columns of a single matrix.
Bjorn Gustavsson
on 16 Jul 2020
If you need to do some more complicated operations to calculate your function, as you kind of indicate that you have to loop to calculate your Jr4 term (if I've understood correctly) then you might be best off by implementing that in a function stored in a regular .m-file. Then calling integral3 will be just as before, but you have a much larger array of functionalities to calculate the function-values to your disposal.
Luigi Stragapede
on 17 Jul 2020
Walter Roberson
on 17 Jul 2020
Right at the moment, there is a problem with the Answers facility and we cannot repost our comments as Answers. That will probably be fixed in a few hours.
Answers (0)
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!