Solving a MATLAB Problem with 3 Variable Integration

In summary, the author is asking for a way to integrate a function over three variables (x,y,z), but only integrate over x and y. They want to solve the equation for "a" using a numerical integration routine, but when they try to pass in "m" as an additional parameter, it gives an error. They're not sure if anything they're doing here is symbolic or not. If you're using MATLAB, you can use a nested function to allow the inner function to access the outer function's variables. Alternatively, you can use an anonymous function to do the same thing.
  • #1
quin
50
0
dear friends

i want to do set of operation in MATLAB but i have a problem:
first of all, i want to take 3variable integration from f which is function of x,y,z,m,a
but i want to integrate only over x,y,z
after that for a special "a" forexample a=0.01 i want to solve the equation.i mean one side of equation is the answer of that 3variable integration and the other side is just a number
in MATLAB my program is:

a=0.01
syms m a x y z
f = inline(vectorize((2/m)-(2*a*(4+a*m))/(-(4+a*m)^2+(cos(0.25*(x-y)))^2+(cos(0.25*(x+y)))^2+(cos(0.5*x)+cos(0.5*y))*cos(0.5*z))),'x','y','z')
S = solve(330.734- triplequad(f,0,2*pi,0,2*pi,0,2*pi), m)

so for a special "a" i must find special "m"

but when i write it in matlab, it give error for "m"

but because i know the answer of "m" from somewhere else , when i put m and a in integration and solve equation it becomes true.

so what should i do?
 
Physics news on Phys.org
  • #2
I don't have the MATLAB symbolic toolbox so I'm not well versed with this. However I'm pretty sure that triplequad is a purely numerical integration routine, so I'm guessing that it is unable to handle the symbolic variable "m".

BTW. I'm not even sure that anything you're doing here is symbolic. "a" is given a constant value and x,y,z will be given numerical values (the function evaluated at numerical values) in the triplequad routine. I'm certain that if "solve" can do anything for you here it will be numeric and not symbolic.
 
Last edited:
  • #4
uart said:
It looks like this might work if you were able to pass "m" as an additional parameter to "f". See "parametrizing functions" here: http://www.mathworks.com.au/help/matlab/math/parameterizing-functions.html

Thanks dear I saw that site but I couldn’t get my purpose
Let me ask my question in a different way. Tell me a way for taking multivariable integration from a function which is in terms of x,y,z,m , ((3variable integration over ' x,y,z')), and give the answer in terms of "m"
Thank you
 
  • #5
quin said:
Thanks dear I saw that site but I couldn’t get my purpose
Let me ask my question in a different way. Tell me a way for taking multivariable integration from a function which is in terms of x,y,z,m , ((3variable integration over ' x,y,z')), and give the answer in terms of "m"
Thank you

The basic idea is that if you write nested functions in matlab, then the inner function gets access to the outer function's variables. I think the website explains it well enough.

Firstly you need to understand that you are working numerically, NOT symbolically. You don't want the "syms" statement. What you do need to do is to write a function to takes the single variable "m" and returns the numerical integral for this given parameter. Use triplequad in a nested function like as follows (type this as a ".m" file ok).

Code:
function myint = getintegral(m)
a = 0.01;
myint = 330.734 - triplequad(@myfunction,0,2*pi,0,2*pi,0,2*pi);

  function myfun = myfunction(x,y,z)
  (2/m)-(2*a*(4+a*m))/(-(4+a*m)^2+(cos(0.25*(x-y)))^2+(cos(0.25*(x+y)))^2+(cos(0.5*x)+cos(0.5*y)) *cos(0.5*z)));
  end %myfunction

end %getintegral

See how the nested function (myfunction) works. It allows us to use parameters "a" and "m" in addition to the parameters x,y and z that are actually passed to the function.

Then from the command line you can just type something like:

> fsolve(@getintegral,m_initialguess)

BTW. I didn't check your syntax, just copied and pasted it ok.
 
Last edited:
  • #6
An addendum for anyone using gnu-Octave as a freeware MATLAB clone. Unfortunately Octave doesn't extend the scope of a functions variables into its nested functions, so the above doesn't work.

An alternative method using "anonymous" functions can achieve the same result. This should work equally well on MATLAB too. Here's an example using a simple one dimensional integral (quad).

1. Write a function for the integrand.

Code:
function myfun = mytestintegrand(x,s)
% called from getintegral.m

  myfun = exp(-(x/s)^2/2)/sqrt(2*pi)/s;

end

2. Write a function to evaluate the integral in terms of the auxiliary parameter ("s") by using the anonymous "@ function" to cast the original function into a function of just "x" (for each given value of parameter "s").

Code:
function myint = getintegral(s)

  myint = 0.25 - quad( @(x) mytestintegrand(x,s), 0,1);

end

3. Call fsolve (for example) from the command line.

> fsolve(@getintegral, 1.0)
 
Last edited:
  • #7
uart said:
The basic idea is that if you write nested functions in matlab, then the inner function gets access to the outer function's variables. I think the website explains it well enough.

Firstly you need to understand that you are working numerically, NOT symbolically. You don't want the "syms" statement. What you do need to do is to write a function to takes the single variable "m" and returns the numerical integral for this given parameter. Use triplequad in a nested function like as follows (type this as a ".m" file ok).

Code:
function myint = getintegral(m)
a = 0.01;
myint = 330.734 - triplequad(@myfunction,0,2*pi,0,2*pi,0,2*pi);

  function myfun = myfunction(x,y,z)
  (2/m)-(2*a*(4+a*m))/(-(4+a*m)^2+(cos(0.25*(x-y)))^2+(cos(0.25*(x+y)))^2+(cos(0.5*x)+cos(0.5*y)) *cos(0.5*z)));
  end %myfunction

end %getintegral

See how the nested function (myfunction) works. It allows us to use parameters "a" and "m" in addition to the parameters x,y and z that are actually passed to the function.

Then from the command line you can just type something like:

> fsolve(@getintegral,m_initialguess)

BTW. I didn't check your syntax, just copied and pasted it ok.


Many thanks for your reply but sorry i think that you made a mistake with what i wanted to say. I don't know the value of "m" atall and also i don't have any m_initialguess . Instead, i want "my int" to be zero
I mean that the term which is in parentheses of solve in my first quote must be zero and with knowing that,i must find "m":
S = solve((330.734- ...)==0, m)
in other words, the functiom which you introduce as "my int" is 0.
I want to solve the equation in which one side of equation is the answer of that 3variable integration and the other side is just a number "330.734"
so for a special "a" i must find special "m"

I would appreciate if you could help me with your excellent guidance
 
  • #8
quin said:
I mean that the term which is in parentheses of solve in my first quote must be zero and with knowing that,i must find "m":

Yes that is what the above does. It solves to find the value of "m" for which,

myint = 330.734 - triplequad(@myfunction,0,2*pi,0,2*pi,0,2*pi)

is zero.
 
  • #9
uart said:
Code:
function myint = getintegral(m)
a = 0.01;
myint = 330.734 - triplequad(@myfunction,0,2*pi,0,2*pi,0,2*pi);

  function myfun = myfunction(x,y,z)
  myfun=(2./m)-(2*a*(4+a*m))./(-(4+a*m).^2+(cos(0.25*(x-y))).^2+(cos(0.25*(x+y))).^2+(cos(0.5*x)+cos(0.5*y))*cos(0.5*z));
  end %myfunction

end %getintegral

> fsolve(@getintegral,m_initialguess)

Hi friend
some days ago I asked 1qestion from you(above explanation)
I test my function( in above code) and found answer for m and it worked

but now I want to use this way for another function but it gives eror.
can you tell me that why those erors are given?
Let me put my code in another post below:
 
  • #10
Code:
function myint=getintegral(m)
a=100;
myint=330.734-triplequad(@myfunction,-pi,pi,-pi,pi,-pi,pi);

    function myfun=myfunction(x,y,z)
        myfun=triplequad((4 *((-2* a + m).^2 *(4 *a + m) +4 *a.^2 *(2*(2*a - m)* cos(z) + cos(y) *(4* a - 2* m + 4 *a* cos(z) - m *cos(z)) + cos(x) *(4* a - 2* m + 4 *a* cos(z) - m *cos(z) +  cos(y) *(4 *a - m + 4 *a* cos(z))))))./(m.^4 + 64 *a.^3 *m *(1 + cos(x)) *(1 + cos(y)) *(1 + cos(z)) - 8 *a.^2 *m.^2 *(3 + 2 *cos(z) + cos(y) *(2 + cos(z)) + cos(x) *(2 + cos(y) + cos(z))) + 16 *a.^4 *(-3 + cos(x).^2 *(cos(y) - cos(z)).^2 - 4 *cos(z) + cos(y) *(-4 + cos(z) *(-6 + cos(y) *cos(z))) - 2 *cos(x) *(2 + 3 *cos(z) +  cos(y) *(3 + cos(z)* (6 + cos(y) + cos(z)))))),-pi,pi,-pi,pi,-pi,pi);
    end %myfunction

end %getintegral


and after that use fsolve

also i must tell that I used ./ instead of / and also .^ instead of ^ because if I didnt do that Matlab couldn't solve and said that x y z are matrix
in the last code in above post, I did the same and I reach to answer.



I would appreciate if you could help me with solving this new function too

because I can not understand Matlab's erors
 
  • #11
quin said:
Code:
function myint=getintegral(m)
a=100;
myint=330.734-triplequad(@myfunction,-pi,pi,-pi,pi,-pi,pi);

    function myfun=myfunction(x,y,z)
        myfun=triplequad((4 *((-2* a + m).^2 *(4 *a + m) +4 *a.^2 *(2*(2*a - m)* cos(z) + cos(y) *(4* a - 2* m + 4 *a* cos(z) - m *cos(z)) + cos(x) *(4* a - 2* m + 4 *a* cos(z) - m *cos(z) +  cos(y) *(4 *a - m + 4 *a* cos(z))))))./(m.^4 + 64 *a.^3 *m *(1 + cos(x)) *(1 + cos(y)) *(1 + cos(z)) - 8 *a.^2 *m.^2 *(3 + 2 *cos(z) + cos(y) *(2 + cos(z)) + cos(x) *(2 + cos(y) + cos(z))) + 16 *a.^4 *(-3 + cos(x).^2 *(cos(y) - cos(z)).^2 - 4 *cos(z) + cos(y) *(-4 + cos(z) *(-6 + cos(y) *cos(z))) - 2 *cos(x) *(2 + 3 *cos(z) +  cos(y) *(3 + cos(z)* (6 + cos(y) + cos(z)))))),-pi,pi,-pi,pi,-pi,pi);
    end %myfunction

end %getintegral

because I can not understand Matlab's erors

I forgot to tell you; my answer( I mean values for "m") is around 3.so take m_initial = 3
 

What is MATLAB and how is it used in solving problems with 3 variable integration?

MATLAB is a high-level programming language that is commonly used in scientific and engineering applications. It has built-in functions and tools for solving complex mathematical problems, including integration. In solving problems with 3 variable integration, MATLAB allows for efficient and accurate calculation of triple integrals.

What is the process for solving a MATLAB problem with 3 variable integration?

The process for solving a MATLAB problem with 3 variable integration involves defining the function to be integrated, setting up the limits of integration for each variable, and using the built-in function "triplequad" to perform the integration. The result is the triple integral of the function over the specified limits.

What are the benefits of using MATLAB for solving problems with 3 variable integration?

MATLAB offers many benefits for solving problems with 3 variable integration. It allows for accurate and efficient calculations, and can handle complex functions and limits of integration. It also has built-in functions for visualization and analysis of the results.

What are some common challenges when solving a MATLAB problem with 3 variable integration?

One common challenge when solving a MATLAB problem with 3 variable integration is setting up the correct limits of integration for each variable. It is important to carefully consider the domain of the function and choose appropriate limits to ensure accurate results. Another challenge may be selecting the correct integration method for the given problem.

Are there any alternative methods to solving a MATLAB problem with 3 variable integration?

Yes, there are alternative methods to solving a MATLAB problem with 3 variable integration. Some other programming languages, such as Python, also have built-in functions for integration. Additionally, traditional methods such as using analytical techniques or numerical integration methods can be used. However, MATLAB offers a user-friendly interface and specialized tools that make it a popular choice for solving integration problems.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
983
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
211
Back
Top