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 - meshgrid and sum.

  1. Mar 25, 2013 #1
    I am asked to evaluate the following expression using functions meshgrid, sum and dot operations in MATLAB:
    y = Ʃ(n=1 to N) xn*[cos(x2 + n2)/xn], where x is the vector of four equally spaced values from 1 to 2, N=10.

    Below is my attempt (I am quite positive it's incorrect, though):

    n = 1:10;
    x = linspace(1,2,4);
    [X,N] = meshgrid(x,n);
    y1 = (X.^N).*((cos(X.^2 + N.^2))./(X.*N));
    y2 = sum(y1);

    I'd appreciate your assistance!
  2. jcsd
  3. Mar 25, 2013 #2


    User Avatar
    Gold Member

    Why are you so sure that it is wrong?

    It looks to me like y2 has 4 values, equal to y(1), y(4/3), y(5/3), y(2).

    Code (Text):

    y2 =

       -0.1036    1.1696  -11.0536  -81.6197
    To check y(1) quickly,

    Code (Text):

    y = @(x,n) (x.^n).*((cos(x.^2+n.^2))./(x.*n));
    for n=1:10
    Y(n) = y(1,n);

    ans =

  4. Mar 25, 2013 #3
    Thank you for your reply.
    You claim that y2 has four values, yet the summation in the original expression (to be evaluated) must yield but one.
    Should I then emend my code thus:
    y2 = sum(y1(1:10))?
    Will that answer the question correctly?
  5. Mar 25, 2013 #4


    User Avatar
    Gold Member

    the summation in the original expression evaluates a different answer for each value of x. n takes on 10 different values, and x takes on 4 different values. So y1 is a 10x4 array where each column represents n=1:10 for a single value of x.

    When you sum the columns of y1, you are summing n=1:10 for y(x), just like your expression asks. This results in four different evaluations of the summation, one for each different value of x.

    If you were to sum BOTH the rows and columns, it would be equivalent to a double summation over x and n, which is not indicated in your expression.

    No; y1 is a 10x4 matrix as we said before, so it has a total of 40 values, 10 in each of 4 columns. sum(y1(1:10)) does not accomplish what you want.

    You know y(1), y(4/3), y(5/3), y(2) from your code already. Perhaps if you provide more information on exactly what question needs to be answered with this code then we can see if this answer is inadequate.
  6. Mar 25, 2013 #5
    The question asks for an explicit evaluation of the original sigma expression, using functions meshgrid and sum.
    That being the case, are you claiming my original code satisfies this question both adequately and correctly?
  7. Mar 25, 2013 #6


    User Avatar
    Gold Member

    Yes. You want to evaluate

    [tex] y(x) = \sum_{n=1)^{10} \frac{x^n \cos(x^2+n^2)}{nx}[/tex]

    for 4 different values of x, and this corresponds to y2 in your code.
  8. Mar 25, 2013 #7
    Thank you very much!
  9. Mar 26, 2013 #8
    Hi Kreil,
    Would you be so kind as to help dispel a few minor doubts I am having regarding the code in the following instances:

    1) v = r*cos(alpha)*[1 + (r*sin(alpha))/(l*sin(beta))] and alpha changes from 0 to 2pi.
    I hence wrote:

    alpha = 0:pi/10:2*pi;
    v = r*cos(alpha).*(1 + (r*sin(alpha))./(l*sin(beta)));

    Should alpha indeed be treated as a vector whilst defining v (I am referring to the use of the dot operations)?

    2) Using function rand, should a matrix of random integers in the interval [55..100] be generated thus:

    M = ceil((rand(5,5)+(11/9))*45);?

    3) Should six concentric circles whose radii vary between 0.5 and 1.75 (with intervals of 0.25) be plotted thus:

    theta = linspace(0, 2*pi, 50);
    [X, Y] = meshgrid(0.5:0.25:1.75, theta);
    plot(a+cos(Y).*X, b+sin(Y).*X);?

    4) May I plot what is called a Mobius Strip, whose equation is
    x = s*cos(t/2)*cos(t) + cos(t); y = s*cos(t/2)*sin(t) + sin(t); z = s*sin(t/2), where -0.4<=s<=0.4 and 0<=t<=2pi, thus:

    s = -0.4: 0.05: 0.4;
    t = 0: pi/50: 2*pi;
    [X, Y] = meshgrid(s, t);
    surf(X.*cos(Y/2).*cos(Y) + cos(Y), X.*cos(Y./2).*sin(Y) + sin(Y), X.*sin(Y./2));?

    5) A swimmer is trying to cross a river 1.1 km wide. The velocity of the swimmer is given as 0.95 km/h in the vertical direction, whilst that of the stream is S = 1.3 km/h in the horizontal direction. I am asked for the swimmer's velocity relative to the ground ("true-velocity" vector T) and the speed. May I implement that thus:
    AB = 1.1;
    V = [0 0.95];
    S = [-1.3 0];
    T = V + S;
    magnitude = norm(T);?

    6) I'd like to write a function which calculates the pressure drop given by: P = (1/2)*rho*omega^2*(L/D)*lambda
    where rho - fluid's density, omega - fluid's velocity, L - pipe's length, D - pipe's diameter, lambda - function of the Reynolds number.
    * For Reynolds<2320, lambda = 64/Reynolds
    * For 2320<Reynolds<10^5, lambda = 0.3164/(Reynolds^0.25)
    * For 10^5<Reynolds<10^6, 1/sqrt(lambda) = 2lg(Reynolds*sqrt(lambda)) - 0.8, which is to be solved via iteration following this algorithm: letting tol = 0.01 and lambda0 = 1, the starting value x0 should be x0 = 1/sqrt(lambda0) whereas the next value is x = 2lg(Reynolds/x0) - 0.8.
    * For Reynolds>10^6, calculations are halted and the program generates a message about that using the error() function.

    May I code it thus:

    function PDROP = ex(rho, omega, length, diameter, Reynolds)
    if (Reynolds<2320)
    lambda = 64/Reynolds;
    elseif (Reynolds>2320 && Reynolds<10^5)
    lambda = 0.3164/(Reynolds^0.25);
    elseif (Reynolds>10^5 && Reynolds<10^6)
    tol = 0.01;
    x0 = 1;
    x = 2*log10(Reynolds) - 0.8;
    while (abs(x - x0) > tol)
    x0 = x;
    x = 2*log10(Reynolds/x0) - 0.8;
    lambda = x^(-2);
    elseif (Reynolds>10^6)
    error('Reynolds number must not be larger than 10^6');
    PDROP = 0.5*rho*(omega^2)*(length/diameter)*lambda;?
  10. Mar 26, 2013 #9


    User Avatar
    Gold Member

    What are your doubts? What have you tried? Have you tried running the code and examining the output?

    Most of your questions are resolved by running the code and looking at what it produces.
  11. Mar 26, 2013 #10
    Well, I have tried executing all the above segments of code, but I am still uncertain the syntax itself is not cumbersome and sufficiently efficient. I have tried to be very clear about the origin and nature of my doubts, in my previous post. I'd gladly try to be even more precise. Would you be willing to answer some of my questions regarding the above code?
  12. Mar 28, 2013 #11
    If it produces the correct output and you don't have to wait an age for any of it to run, it's surely fine... I don't see how you could make it less cumbersome. Matlab is a nice language for writing concise vectorised operations.

    I guess it's worth mentioning a general programming rule: "code first, optimise later". Your code is understandable; if it does what you need it to do and it does it in reasonable time, there is no need for better 'efficiency'. You will lose way too much of your own time for diminishing efficiency returns (especially as these look like problem answers, so they're only going to be run once).
    To be fair, you just asked "may I" or "should I" for most of them... to which the answer is "if it works, yes you may" ;)
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: MATLAB question - meshgrid and sum.
  1. Sum function (MATLAB) (Replies: 1)

  2. Matlab question (Replies: 2)

  3. Matlab question (Replies: 10)