Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab question

  1. Nov 6, 2012 #1
    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:

    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?
  2. jcsd
  3. Nov 6, 2012 #2


    User Avatar
    Science Advisor

    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: Nov 6, 2012
  4. Nov 6, 2012 #3


    User Avatar
    Science Advisor

  5. Nov 7, 2012 #4
    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
  6. Nov 7, 2012 #5


    User Avatar
    Science Advisor

    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 web site 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 (Text):

    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: Nov 7, 2012
  7. Nov 7, 2012 #6


    User Avatar
    Science Advisor

    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 (Text):

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

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

    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 (Text):

    function myint = getintegral(s)

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

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

    > fsolve(@getintegral, 1.0)
    Last edited: Nov 7, 2012
  8. Nov 7, 2012 #7

    Many thanks for your reply but sorry i think that you made a mistake with what i wanted to say. I dont know the value of "m" atall and also i dont 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 wanna 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
  9. Nov 7, 2012 #8


    User Avatar
    Science Advisor

    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.
  10. Nov 15, 2012 #9
    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 wanna 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:
  11. Nov 15, 2012 #10
    Code (Text):

    function myint=getintegral(m)

        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 couldnt 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
  12. Nov 15, 2012 #11
    I forgot to tell you; my answer( I mean values for "m") is around 3.so take m_initial = 3
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook