Solve triple integral of a function that upload its value every step

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?
Please help me.

11 Comments

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...
I mean that Jr4 is the result of a summation of n from 0 to 10.
So I have a for loop (not shown in the previous code) and every iteration its value is updated.
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.
  • what do you mean? is there a Σ button? for the summation I use the for loop.
  • So can I approximate the integral with a summation? Can you tell me how use Σ button?
  • Can you tell me other ways to implement triple integral? In particular integral3 wants an handle function as imput but my handle function depends on another function that I have to define before. This is my problem.
  • Do you know the function 'vpaintegral'? Using this function, the program doesn't stop for error but it stops because the program doesn't manage to complete the integral maybe because it is difficult.
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.
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.
you are very kind, thanks.
But what do you mean with: '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' ?
Do you mean I have to specify the variables x, y,z for example in this way?
g=matlabFunction(r,[y z x])
  • Is it important the order of the variables in the brackets?
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.
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.
thanks a lot guys. How can I help you? Could I give you points for giving me answers?
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.

Sign in to comment.

Answers (0)

Categories

Asked:

on 14 Jul 2020

Commented:

on 17 Jul 2020

Community Treasure Hunt

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

Start Hunting!