MATLAB Solving a MATLAB Problem with 3 Variable Integration

AI Thread Summary
The discussion revolves around solving a MATLAB problem involving three-variable integration and finding a specific variable, "m," based on the result of that integration. The user initially struggles with using the symbolic toolbox and is advised to switch to numerical methods, specifically using nested functions and the `triplequad` function. A key suggestion is to define a function that computes the integral and returns a value based on "m," which can then be solved using `fsolve`. The user later attempts to apply this method to a different function but encounters errors, prompting a request for clarification on resolving these issues. The overall focus is on effectively utilizing MATLAB's numerical integration capabilities to solve for "m" in various contexts.
quin
Messages
50
Reaction score
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
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:
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
 
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:
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:
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
 
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.
 
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
 

Similar threads

Back
Top